From 8e5fdb7f264a34f855312083f2f1aa857f9a3016 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 6 Nov 2012 17:12:28 +0000 Subject: [PATCH] Removing author feature from article --- app/models/article.rb | 11 +++++++---- db/migrate/20121024220349_add_author_id_to_article.rb | 11 ----------- db/migrate/20121024222938_set_authors_based_on_article_versions.rb | 9 --------- db/schema.rb | 4 +--- test/unit/article_test.rb | 22 ++++++---------------- 5 files changed, 14 insertions(+), 43 deletions(-) delete mode 100644 db/migrate/20121024220349_add_author_id_to_article.rb delete mode 100644 db/migrate/20121024222938_set_authors_based_on_article_versions.rb diff --git a/app/models/article.rb b/app/models/article.rb index e4f9fd9..1b427d7 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -13,9 +13,9 @@ class Article < ActiveRecord::Base # xss_terminate plugin can't sanitize array fields before_save :sanitize_tag_list - before_save do |article| - if article.author_id - article.author_name = Person.find(article.author_id).name + before_create do |article| + if article.last_changed_by_id + article.author_name = Person.find(article.last_changed_by_id).name end end @@ -26,7 +26,6 @@ class Article < ActiveRecord::Base validates_uniqueness_of :slug, :scope => ['profile_id', 'parent_id'], :message => N_('The title (article name) is already being used by another article, please use another title.'), :if => lambda { |article| !article.slug.blank? } belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' - belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' @@ -545,6 +544,10 @@ class Article < ActiveRecord::Base false end + def author + versions.empty? ? last_changed_by : versions.first.last_changed_by + end + def author_name author ? author.name : (setting[:author_name] || _('Unknown')) end diff --git a/db/migrate/20121024220349_add_author_id_to_article.rb b/db/migrate/20121024220349_add_author_id_to_article.rb deleted file mode 100644 index ac10d9a..0000000 --- a/db/migrate/20121024220349_add_author_id_to_article.rb +++ /dev/null @@ -1,11 +0,0 @@ -class AddAuthorIdToArticle < ActiveRecord::Migration - def self.up - add_column :articles, :author_id, :integer - add_column :article_versions, :author_id, :integer - end - - def self.down - remove_column :articles, :author_id - remove_column :article_versions, :author_id, :integer - end -end diff --git a/db/migrate/20121024222938_set_authors_based_on_article_versions.rb b/db/migrate/20121024222938_set_authors_based_on_article_versions.rb deleted file mode 100644 index e63100d..0000000 --- a/db/migrate/20121024222938_set_authors_based_on_article_versions.rb +++ /dev/null @@ -1,9 +0,0 @@ -class SetAuthorsBasedOnArticleVersions < ActiveRecord::Migration - def self.up - update('UPDATE articles SET author_id = (SELECT profiles.id FROM articles as a INNER JOIN article_versions ON a.id = article_versions.article_id INNER JOIN profiles ON profiles.id = article_versions.last_changed_by_id WHERE article_versions.version = 1 AND articles.id = a.id)') - end - - def self.down - say "Can not be revesed!" - end -end diff --git a/db/schema.rb b/db/schema.rb index e4eae42..41a9922 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20121024222938) do +ActiveRecord::Schema.define(:version => 20121022190819) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -86,7 +86,6 @@ ActiveRecord::Schema.define(:version => 20121024222938) do t.string "language" t.string "source_name" t.integer "license_id" - t.integer "author_id" end create_table "articles", :force => true do |t| @@ -128,7 +127,6 @@ ActiveRecord::Schema.define(:version => 20121024222938) do t.string "language" t.string "source_name" t.integer "license_id" - t.integer "author_id" end add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id" diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index d661b94..7480304 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -786,7 +786,7 @@ class ArticleTest < ActiveSupport::TestCase should 'allow author to edit if is publisher' do c = fast_create(Community) p = create_user_with_permission('test_user', 'publish_content', c) - a = c.articles.create!(:name => 'a test article', :author => p) + a = c.articles.create!(:name => 'a test article', :last_changed_by => p) assert a.allow_post_content?(p) end @@ -1358,16 +1358,16 @@ class ArticleTest < ActiveSupport::TestCase should "the author_name returns the name of the article's author" do author = fast_create(Person) - a = Article.new(:author => author) + a = profile.articles.create!(:name => 'a test article', :last_changed_by => author) assert_equal author.name, a.author_name - a.author = nil + author.destroy a.author_name = 'some name' assert_equal 'some name', a.author_name end should 'retrieve latest info from topic when has no comments' do forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) - post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now, :author_id => profile.id) + post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now, :last_changed_by_id => profile.id) assert_equal post.updated_at, post.info_from_last_update[:date] assert_equal profile.name, post.info_from_last_update[:author_name] assert_equal profile.url, post.info_from_last_update[:author_url] @@ -1801,19 +1801,9 @@ class ArticleTest < ActiveSupport::TestCase assert a1.errors.invalid?(:parent_id) end - should 'be able to have an author' do - author = fast_create(Person) - article = Article.new - assert_nothing_raised do - article.author = author - end - end - - should 'set author_name before saving article if there is an author' do + should 'set author_name before creating article if there is an author' do author = fast_create(Person) - article = fast_create(Article, :profile_id => fast_create(Profile).id) - article.author = author - article.save! + article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author) assert_equal author.name, article.author_name author_name = author.name -- libgit2 0.21.2