From ef74a65612bb7da52ae2dcb58a8c0005cbb32c6a Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 18 Jul 2012 11:15:11 -0300 Subject: [PATCH] [licenses] Creating Linsese model and its relations --- app/models/article.rb | 2 ++ app/models/environment.rb | 1 + app/models/license.rb | 12 ++++++++++++ db/migrate/20120713073641_create_licenses.rb | 14 ++++++++++++++ db/migrate/20120713082741_add_license_to_article.rb | 11 +++++++++++ test/functional/cms_controller_test.rb | 11 +++++++++++ test/unit/article_test.rb | 6 ++++++ test/unit/environment_test.rb | 14 ++++++++++++++ test/unit/license_test.rb | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 103 insertions(+), 0 deletions(-) create mode 100644 app/models/license.rb create mode 100644 db/migrate/20120713073641_create_licenses.rb create mode 100644 db/migrate/20120713082741_add_license_to_article.rb create mode 100644 test/unit/license_test.rb diff --git a/app/models/article.rb b/app/models/article.rb index f71f7b1..84bc3f8 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -30,6 +30,8 @@ class Article < ActiveRecord::Base belongs_to :reference_article, :class_name => "Article", :foreign_key => 'reference_article_id' + belongs_to :license + has_many :translations, :class_name => 'Article', :foreign_key => :translation_of_id belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id before_destroy :rotate_translations diff --git a/app/models/environment.rb b/app/models/environment.rb index 28cd5e5..f75f4c2 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -158,6 +158,7 @@ class Environment < ActiveRecord::Base has_many :products, :through => :enterprises has_many :people has_many :communities + has_many :licenses has_many :categories has_many :display_categories, :class_name => 'Category', :conditions => 'display_color is not null and parent_id is null', :order => 'display_color' diff --git a/app/models/license.rb b/app/models/license.rb new file mode 100644 index 0000000..7e59ce4 --- /dev/null +++ b/app/models/license.rb @@ -0,0 +1,12 @@ +class License < ActiveRecord::Base + belongs_to :environment + has_many :content, :class_name => 'Article', :foreign_key => 'license_id' + + validates_presence_of :name, :environment + validates_presence_of :slug, :if => lambda {|license| license.name.present?} + validates_uniqueness_of :slug + + before_validation do |license| + license.slug ||= license.name.to_slug if license.name.present? + end +end diff --git a/db/migrate/20120713073641_create_licenses.rb b/db/migrate/20120713073641_create_licenses.rb new file mode 100644 index 0000000..3aeb82d --- /dev/null +++ b/db/migrate/20120713073641_create_licenses.rb @@ -0,0 +1,14 @@ +class CreateLicenses < ActiveRecord::Migration + def self.up + create_table :licenses do |t| + t.string :name, :null => false + t.string :slug, :null => false + t.string :url + t.references :environment, :null => false + end + end + + def self.down + drop_table :licenses + end +end diff --git a/db/migrate/20120713082741_add_license_to_article.rb b/db/migrate/20120713082741_add_license_to_article.rb new file mode 100644 index 0000000..108ef93 --- /dev/null +++ b/db/migrate/20120713082741_add_license_to_article.rb @@ -0,0 +1,11 @@ +class AddLicenseToArticle < ActiveRecord::Migration + def self.up + add_column :articles, :license_id, :integer + add_column :article_versions, :license_id, :integer + end + + def self.down + remove_column :articles, :license_id + remove_column :article_versions, :license_id + end +end diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 32d1a9c..d5cf313 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -1542,6 +1542,17 @@ class CmsControllerTest < ActionController::TestCase assert_includes special_article_types, Float end + should 'be able to define license when updating article' do + article = fast_create(Article, :profile_id => profile.id) + license = License.create!(:name => 'GPLv3', :environment => profile.environment) + login_as(profile.identifier) + + post :edit, :profile => profile.identifier, :id => article.id, :article => { :license_id => license.id } + + article.reload + assert_equal license, article.license + end + protected # FIXME this is to avoid adding an extra dependency for a proper JSON parser. diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 6c07d96..bd248e2 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1671,4 +1671,10 @@ class ArticleTest < ActiveSupport::TestCase assert !a.allow_edit?(nil) end + should 'be able to have a license' do + license = License.create!(:name => 'GPLv3', :environment => Environment.default) + article = Article.new(:license_id => license.id) + assert_equal license, article.license + end + end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index ac84154..dc8e7de 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -1204,4 +1204,18 @@ class EnvironmentTest < ActiveSupport::TestCase should 'have production costs' do assert_respond_to Environment.default, :production_costs end + + should 'be able to have many licenses' do + environment = Environment.default + another_environment = fast_create(Environment) + l1 = License.create!(:name => 'GPLv3', :environment => environment) + l2 = License.create!(:name => 'AGPL', :environment => environment) + l3 = License.create!(:name => 'Apache', :environment => another_environment) + + environment.reload + + assert_includes environment.licenses, l1 + assert_includes environment.licenses, l2 + assert_not_includes environment.licenses, l3 + end end diff --git a/test/unit/license_test.rb b/test/unit/license_test.rb new file mode 100644 index 0000000..c880424 --- /dev/null +++ b/test/unit/license_test.rb @@ -0,0 +1,32 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class LicenseTest < ActiveSupport::TestCase + should 'validate presence of name, slug and enviornment' do + license = License.new + license.valid? + assert license.errors.invalid?(:name) + assert license.errors.invalid?(:slug) + assert license.errors.invalid?(:environment) + + license.name = 'GPLv3' + license.slug = 'gplv3' + license.environment = Environment.default + license.valid? + assert !license.errors.invalid?(:name) + assert !license.errors.invalid?(:slug) + assert !license.errors.invalid?(:environment) + end + + should 'fill in slug before creation' do + license = License.new(:name => 'GPLv3', :environment => Environment.default) + assert license.valid? + assert_equal license.name.to_slug, license.slug + end + + should 'not overwrite slug if it is already fill' do + license = License.new(:name => 'GPLv3', :slug => 'some-slug', :environment => Environment.default) + license.valid? + assert_equal 'some-slug', license.slug + end +end + -- libgit2 0.21.2