Commit 8e5fdb7f264a34f855312083f2f1aa857f9a3016

Authored by Rodrigo Souto
1 parent 836970b6

Removing author feature from article

app/models/article.rb
... ... @@ -13,9 +13,9 @@ class Article < ActiveRecord::Base
13 13 # xss_terminate plugin can't sanitize array fields
14 14 before_save :sanitize_tag_list
15 15  
16   - before_save do |article|
17   - if article.author_id
18   - article.author_name = Person.find(article.author_id).name
  16 + before_create do |article|
  17 + if article.last_changed_by_id
  18 + article.author_name = Person.find(article.last_changed_by_id).name
19 19 end
20 20 end
21 21  
... ... @@ -26,7 +26,6 @@ class Article < ActiveRecord::Base
26 26 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? }
27 27  
28 28 belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id'
29   - belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id'
30 29  
31 30 has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc'
32 31  
... ... @@ -545,6 +544,10 @@ class Article < ActiveRecord::Base
545 544 false
546 545 end
547 546  
  547 + def author
  548 + versions.empty? ? last_changed_by : versions.first.last_changed_by
  549 + end
  550 +
548 551 def author_name
549 552 author ? author.name : (setting[:author_name] || _('Unknown'))
550 553 end
... ...
db/migrate/20121024220349_add_author_id_to_article.rb
... ... @@ -1,11 +0,0 @@
1   -class AddAuthorIdToArticle < ActiveRecord::Migration
2   - def self.up
3   - add_column :articles, :author_id, :integer
4   - add_column :article_versions, :author_id, :integer
5   - end
6   -
7   - def self.down
8   - remove_column :articles, :author_id
9   - remove_column :article_versions, :author_id, :integer
10   - end
11   -end
db/migrate/20121024222938_set_authors_based_on_article_versions.rb
... ... @@ -1,9 +0,0 @@
1   -class SetAuthorsBasedOnArticleVersions < ActiveRecord::Migration
2   - def self.up
3   - 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)')
4   - end
5   -
6   - def self.down
7   - say "Can not be revesed!"
8   - end
9   -end
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20121024222938) do
  12 +ActiveRecord::Schema.define(:version => 20121022190819) do
13 13  
14 14 create_table "abuse_reports", :force => true do |t|
15 15 t.integer "reporter_id"
... ... @@ -86,7 +86,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121024222938) do
86 86 t.string "language"
87 87 t.string "source_name"
88 88 t.integer "license_id"
89   - t.integer "author_id"
90 89 end
91 90  
92 91 create_table "articles", :force => true do |t|
... ... @@ -128,7 +127,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121024222938) do
128 127 t.string "language"
129 128 t.string "source_name"
130 129 t.integer "license_id"
131   - t.integer "author_id"
132 130 end
133 131  
134 132 add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id"
... ...
test/unit/article_test.rb
... ... @@ -786,7 +786,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
786 786 should 'allow author to edit if is publisher' do
787 787 c = fast_create(Community)
788 788 p = create_user_with_permission('test_user', 'publish_content', c)
789   - a = c.articles.create!(:name => 'a test article', :author => p)
  789 + a = c.articles.create!(:name => 'a test article', :last_changed_by => p)
790 790  
791 791 assert a.allow_post_content?(p)
792 792 end
... ... @@ -1358,16 +1358,16 @@ class ArticleTest &lt; ActiveSupport::TestCase
1358 1358  
1359 1359 should "the author_name returns the name of the article's author" do
1360 1360 author = fast_create(Person)
1361   - a = Article.new(:author => author)
  1361 + a = profile.articles.create!(:name => 'a test article', :last_changed_by => author)
1362 1362 assert_equal author.name, a.author_name
1363   - a.author = nil
  1363 + author.destroy
1364 1364 a.author_name = 'some name'
1365 1365 assert_equal 'some name', a.author_name
1366 1366 end
1367 1367  
1368 1368 should 'retrieve latest info from topic when has no comments' do
1369 1369 forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id)
1370   - post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now, :author_id => profile.id)
  1370 + 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)
1371 1371 assert_equal post.updated_at, post.info_from_last_update[:date]
1372 1372 assert_equal profile.name, post.info_from_last_update[:author_name]
1373 1373 assert_equal profile.url, post.info_from_last_update[:author_url]
... ... @@ -1801,19 +1801,9 @@ class ArticleTest &lt; ActiveSupport::TestCase
1801 1801 assert a1.errors.invalid?(:parent_id)
1802 1802 end
1803 1803  
1804   - should 'be able to have an author' do
1805   - author = fast_create(Person)
1806   - article = Article.new
1807   - assert_nothing_raised do
1808   - article.author = author
1809   - end
1810   - end
1811   -
1812   - should 'set author_name before saving article if there is an author' do
  1804 + should 'set author_name before creating article if there is an author' do
1813 1805 author = fast_create(Person)
1814   - article = fast_create(Article, :profile_id => fast_create(Profile).id)
1815   - article.author = author
1816   - article.save!
  1806 + article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author)
1817 1807 assert_equal author.name, article.author_name
1818 1808  
1819 1809 author_name = author.name
... ...