Commit ef74a65612bb7da52ae2dcb58a8c0005cbb32c6a
1 parent
93103d85
Exists in
master
and in
29 other branches
[licenses] Creating Linsese model and its relations
(ActionItem2379)
Showing
9 changed files
with
103 additions
and
0 deletions
Show diff stats
app/models/article.rb
@@ -30,6 +30,8 @@ class Article < ActiveRecord::Base | @@ -30,6 +30,8 @@ class Article < ActiveRecord::Base | ||
30 | 30 | ||
31 | belongs_to :reference_article, :class_name => "Article", :foreign_key => 'reference_article_id' | 31 | belongs_to :reference_article, :class_name => "Article", :foreign_key => 'reference_article_id' |
32 | 32 | ||
33 | + belongs_to :license | ||
34 | + | ||
33 | has_many :translations, :class_name => 'Article', :foreign_key => :translation_of_id | 35 | has_many :translations, :class_name => 'Article', :foreign_key => :translation_of_id |
34 | belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id | 36 | belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id |
35 | before_destroy :rotate_translations | 37 | before_destroy :rotate_translations |
app/models/environment.rb
@@ -158,6 +158,7 @@ class Environment < ActiveRecord::Base | @@ -158,6 +158,7 @@ class Environment < ActiveRecord::Base | ||
158 | has_many :products, :through => :enterprises | 158 | has_many :products, :through => :enterprises |
159 | has_many :people | 159 | has_many :people |
160 | has_many :communities | 160 | has_many :communities |
161 | + has_many :licenses | ||
161 | 162 | ||
162 | has_many :categories | 163 | has_many :categories |
163 | has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' | 164 | has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +class License < ActiveRecord::Base | ||
2 | + belongs_to :environment | ||
3 | + has_many :content, :class_name => 'Article', :foreign_key => 'license_id' | ||
4 | + | ||
5 | + validates_presence_of :name, :environment | ||
6 | + validates_presence_of :slug, :if => lambda {|license| license.name.present?} | ||
7 | + validates_uniqueness_of :slug | ||
8 | + | ||
9 | + before_validation do |license| | ||
10 | + license.slug ||= license.name.to_slug if license.name.present? | ||
11 | + end | ||
12 | +end |
@@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
1 | +class CreateLicenses < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + create_table :licenses do |t| | ||
4 | + t.string :name, :null => false | ||
5 | + t.string :slug, :null => false | ||
6 | + t.string :url | ||
7 | + t.references :environment, :null => false | ||
8 | + end | ||
9 | + end | ||
10 | + | ||
11 | + def self.down | ||
12 | + drop_table :licenses | ||
13 | + end | ||
14 | +end |
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +class AddLicenseToArticle < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + add_column :articles, :license_id, :integer | ||
4 | + add_column :article_versions, :license_id, :integer | ||
5 | + end | ||
6 | + | ||
7 | + def self.down | ||
8 | + remove_column :articles, :license_id | ||
9 | + remove_column :article_versions, :license_id | ||
10 | + end | ||
11 | +end |
test/functional/cms_controller_test.rb
@@ -1542,6 +1542,17 @@ class CmsControllerTest < ActionController::TestCase | @@ -1542,6 +1542,17 @@ class CmsControllerTest < ActionController::TestCase | ||
1542 | assert_includes special_article_types, Float | 1542 | assert_includes special_article_types, Float |
1543 | end | 1543 | end |
1544 | 1544 | ||
1545 | + should 'be able to define license when updating article' do | ||
1546 | + article = fast_create(Article, :profile_id => profile.id) | ||
1547 | + license = License.create!(:name => 'GPLv3', :environment => profile.environment) | ||
1548 | + login_as(profile.identifier) | ||
1549 | + | ||
1550 | + post :edit, :profile => profile.identifier, :id => article.id, :article => { :license_id => license.id } | ||
1551 | + | ||
1552 | + article.reload | ||
1553 | + assert_equal license, article.license | ||
1554 | + end | ||
1555 | + | ||
1545 | protected | 1556 | protected |
1546 | 1557 | ||
1547 | # FIXME this is to avoid adding an extra dependency for a proper JSON parser. | 1558 | # FIXME this is to avoid adding an extra dependency for a proper JSON parser. |
test/unit/article_test.rb
@@ -1671,4 +1671,10 @@ class ArticleTest < ActiveSupport::TestCase | @@ -1671,4 +1671,10 @@ class ArticleTest < ActiveSupport::TestCase | ||
1671 | assert !a.allow_edit?(nil) | 1671 | assert !a.allow_edit?(nil) |
1672 | end | 1672 | end |
1673 | 1673 | ||
1674 | + should 'be able to have a license' do | ||
1675 | + license = License.create!(:name => 'GPLv3', :environment => Environment.default) | ||
1676 | + article = Article.new(:license_id => license.id) | ||
1677 | + assert_equal license, article.license | ||
1678 | + end | ||
1679 | + | ||
1674 | end | 1680 | end |
test/unit/environment_test.rb
@@ -1204,4 +1204,18 @@ class EnvironmentTest < ActiveSupport::TestCase | @@ -1204,4 +1204,18 @@ class EnvironmentTest < ActiveSupport::TestCase | ||
1204 | should 'have production costs' do | 1204 | should 'have production costs' do |
1205 | assert_respond_to Environment.default, :production_costs | 1205 | assert_respond_to Environment.default, :production_costs |
1206 | end | 1206 | end |
1207 | + | ||
1208 | + should 'be able to have many licenses' do | ||
1209 | + environment = Environment.default | ||
1210 | + another_environment = fast_create(Environment) | ||
1211 | + l1 = License.create!(:name => 'GPLv3', :environment => environment) | ||
1212 | + l2 = License.create!(:name => 'AGPL', :environment => environment) | ||
1213 | + l3 = License.create!(:name => 'Apache', :environment => another_environment) | ||
1214 | + | ||
1215 | + environment.reload | ||
1216 | + | ||
1217 | + assert_includes environment.licenses, l1 | ||
1218 | + assert_includes environment.licenses, l2 | ||
1219 | + assert_not_includes environment.licenses, l3 | ||
1220 | + end | ||
1207 | end | 1221 | end |
@@ -0,0 +1,32 @@ | @@ -0,0 +1,32 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class LicenseTest < ActiveSupport::TestCase | ||
4 | + should 'validate presence of name, slug and enviornment' do | ||
5 | + license = License.new | ||
6 | + license.valid? | ||
7 | + assert license.errors.invalid?(:name) | ||
8 | + assert license.errors.invalid?(:slug) | ||
9 | + assert license.errors.invalid?(:environment) | ||
10 | + | ||
11 | + license.name = 'GPLv3' | ||
12 | + license.slug = 'gplv3' | ||
13 | + license.environment = Environment.default | ||
14 | + license.valid? | ||
15 | + assert !license.errors.invalid?(:name) | ||
16 | + assert !license.errors.invalid?(:slug) | ||
17 | + assert !license.errors.invalid?(:environment) | ||
18 | + end | ||
19 | + | ||
20 | + should 'fill in slug before creation' do | ||
21 | + license = License.new(:name => 'GPLv3', :environment => Environment.default) | ||
22 | + assert license.valid? | ||
23 | + assert_equal license.name.to_slug, license.slug | ||
24 | + end | ||
25 | + | ||
26 | + should 'not overwrite slug if it is already fill' do | ||
27 | + license = License.new(:name => 'GPLv3', :slug => 'some-slug', :environment => Environment.default) | ||
28 | + license.valid? | ||
29 | + assert_equal 'some-slug', license.slug | ||
30 | + end | ||
31 | +end | ||
32 | + |