Commit 9fc96fa97ec1e9473b6f160493eccfc5c100a4e2
Committed by
Daniela Feitosa
1 parent
aa47fe88
Exists in
staging
and in
42 other branches
load article's author only when necessary
(ActionItem3097)
Showing
5 changed files
with
69 additions
and
28 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
app/models/article.rb
| ... | ... | @@ -40,12 +40,6 @@ class Article < ActiveRecord::Base |
| 40 | 40 | # xss_terminate plugin can't sanitize array fields |
| 41 | 41 | before_save :sanitize_tag_list |
| 42 | 42 | |
| 43 | - before_create do |article| | |
| 44 | - if article.last_changed_by_id | |
| 45 | - article.author_name = Person.find(article.last_changed_by_id).name | |
| 46 | - end | |
| 47 | - end | |
| 48 | - | |
| 49 | 43 | belongs_to :profile |
| 50 | 44 | validates_presence_of :profile_id, :name |
| 51 | 45 | validates_presence_of :slug, :path, :if => lambda { |article| !article.name.blank? } |
| ... | ... | @@ -55,6 +49,7 @@ class Article < ActiveRecord::Base |
| 55 | 49 | 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? } |
| 56 | 50 | |
| 57 | 51 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' |
| 52 | + belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' | |
| 58 | 53 | |
| 59 | 54 | has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' |
| 60 | 55 | |
| ... | ... | @@ -89,6 +84,11 @@ class Article < ActiveRecord::Base |
| 89 | 84 | article.parent = article.profile.blog |
| 90 | 85 | end |
| 91 | 86 | end |
| 87 | + | |
| 88 | + if article.created_by | |
| 89 | + article.author_name = article.created_by.name | |
| 90 | + end | |
| 91 | + | |
| 92 | 92 | end |
| 93 | 93 | |
| 94 | 94 | after_destroy :destroy_activity |
| ... | ... | @@ -630,18 +630,20 @@ class Article < ActiveRecord::Base |
| 630 | 630 | |
| 631 | 631 | def author(version_number = nil) |
| 632 | 632 | if version_number |
| 633 | - version = versions.find_by_version(version_number) | |
| 633 | + version = self.versions.find_by_version(version_number) | |
| 634 | 634 | author_id = version.last_changed_by_id if version |
| 635 | - Person.exists?(author_id) ? Person.find(author_id) : nil | |
| 636 | 635 | else |
| 637 | - if versions.empty? | |
| 638 | - last_changed_by | |
| 639 | - else | |
| 640 | - author_id = versions.first.last_changed_by_id | |
| 641 | - Person.exists?(author_id) ? Person.find(author_id) : nil | |
| 642 | - end | |
| 636 | + author_id = self.created_by_id | |
| 643 | 637 | end |
| 644 | - end | |
| 638 | + | |
| 639 | + begin | |
| 640 | + @author = Person.find(author_id) if !@author || @author.id != author_id | |
| 641 | + rescue | |
| 642 | + @author = nil | |
| 643 | + end | |
| 644 | + | |
| 645 | + @author | |
| 646 | + end | |
| 645 | 647 | |
| 646 | 648 | def author_name(version_number = nil) |
| 647 | 649 | person = author(version_number) | ... | ... |
test/functional/cms_controller_test.rb
| ... | ... | @@ -1228,7 +1228,7 @@ class CmsControllerTest < ActionController::TestCase |
| 1228 | 1228 | should 'allow user edit article if he is owner and has publish permission' do |
| 1229 | 1229 | c = Community.create!(:name => 'test_comm', :identifier => 'test_comm') |
| 1230 | 1230 | u = create_user_with_permission('test_user', 'publish_content', c) |
| 1231 | - a = create(Article, :profile => c, :name => 'test_article', :last_changed_by => u) | |
| 1231 | + a = create(Article, :profile => c, :name => 'test_article', :created_by => u) | |
| 1232 | 1232 | login_as :test_user |
| 1233 | 1233 | @controller.stubs(:user).returns(u) |
| 1234 | 1234 | |
| ... | ... | @@ -1774,6 +1774,31 @@ class CmsControllerTest < ActionController::TestCase |
| 1774 | 1774 | assert_equal 'first version', Article.find(article.id).name |
| 1775 | 1775 | end |
| 1776 | 1776 | |
| 1777 | + should 'set created_by when creating article' do | |
| 1778 | + login_as(profile.identifier) | |
| 1779 | + | |
| 1780 | + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' } | |
| 1781 | + | |
| 1782 | + a = profile.articles.find_by_path('changed-by-me') | |
| 1783 | + assert_not_nil a | |
| 1784 | + assert_equal profile, a.created_by | |
| 1785 | + end | |
| 1786 | + | |
| 1787 | + should 'not change created_by when updating article' do | |
| 1788 | + other_person = create_user('otherperson').person | |
| 1789 | + | |
| 1790 | + a = profile.articles.build(:name => 'my article') | |
| 1791 | + a.created_by = other_person | |
| 1792 | + a.save! | |
| 1793 | + | |
| 1794 | + login_as(profile.identifier) | |
| 1795 | + post :edit, :profile => profile.identifier, :id => a.id, :article => { :body => 'new content for this article' } | |
| 1796 | + | |
| 1797 | + a.reload | |
| 1798 | + | |
| 1799 | + assert_equal other_person, a.created_by | |
| 1800 | + end | |
| 1801 | + | |
| 1777 | 1802 | protected |
| 1778 | 1803 | |
| 1779 | 1804 | # FIXME this is to avoid adding an extra dependency for a proper JSON parser. | ... | ... |
test/functional/content_viewer_controller_test.rb
| ... | ... | @@ -743,7 +743,7 @@ class ContentViewerControllerTest < ActionController::TestCase |
| 743 | 743 | c = Community.create!(:name => 'test_com') |
| 744 | 744 | u = create_user_with_permission('test_user', 'publish_content', c) |
| 745 | 745 | login_as u.identifier |
| 746 | - a = create(Article, :profile => c, :name => 'test-article', :last_changed_by => u, :published => false) | |
| 746 | + a = create(Article, :profile => c, :name => 'test-article', :created_by => u, :published => false) | |
| 747 | 747 | |
| 748 | 748 | get :view_page, :profile => c.identifier, :page => a.path |
| 749 | 749 | ... | ... |
test/unit/article_test.rb
| ... | ... | @@ -154,7 +154,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 154 | 154 | assert a4.errors[:slug.to_s].present? |
| 155 | 155 | end |
| 156 | 156 | |
| 157 | - should 'record who did the last change' do | |
| 157 | + should 'last_changed_by be a person' do | |
| 158 | 158 | a = profile.articles.build(:name => 'test') |
| 159 | 159 | |
| 160 | 160 | # must be a person |
| ... | ... | @@ -167,6 +167,19 @@ class ArticleTest < ActiveSupport::TestCase |
| 167 | 167 | end |
| 168 | 168 | end |
| 169 | 169 | |
| 170 | + should 'created_by be a person' do | |
| 171 | + a = profile.articles.build(:name => 'test') | |
| 172 | + | |
| 173 | + # must be a person | |
| 174 | + assert_raise ActiveRecord::AssociationTypeMismatch do | |
| 175 | + a.created_by = Profile.new | |
| 176 | + end | |
| 177 | + assert_nothing_raised do | |
| 178 | + a.created_by = Person.new | |
| 179 | + a.save! | |
| 180 | + end | |
| 181 | + end | |
| 182 | + | |
| 170 | 183 | should 'not show private documents as recent' do |
| 171 | 184 | p = create_user('usr1').person |
| 172 | 185 | Article.destroy_all |
| ... | ... | @@ -802,7 +815,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 802 | 815 | should 'allow author to edit if is publisher' do |
| 803 | 816 | c = fast_create(Community) |
| 804 | 817 | p = create_user_with_permission('test_user', 'publish_content', c) |
| 805 | - a = create(Article, :name => 'a test article', :last_changed_by => p, :profile_id => c.id) | |
| 818 | + a = create(Article, :name => 'a test article', :created_by => p, :profile_id => c.id) | |
| 806 | 819 | |
| 807 | 820 | assert a.allow_post_content?(p) |
| 808 | 821 | end |
| ... | ... | @@ -1380,17 +1393,17 @@ class ArticleTest < ActiveSupport::TestCase |
| 1380 | 1393 | |
| 1381 | 1394 | should "the author_name returns the name of the article's author" do |
| 1382 | 1395 | author = fast_create(Person) |
| 1383 | - a = create(Article, :name => 'a test article', :last_changed_by => author, :profile_id => profile.id) | |
| 1396 | + a = create(Article, :name => 'a test article', :created_by => author, :profile_id => profile.id) | |
| 1384 | 1397 | assert_equal author.name, a.author_name |
| 1385 | 1398 | author.destroy |
| 1386 | - a.reload | |
| 1399 | + a = Article.find(a.id) | |
| 1387 | 1400 | a.author_name = 'some name' |
| 1388 | 1401 | assert_equal 'some name', a.author_name |
| 1389 | 1402 | end |
| 1390 | 1403 | |
| 1391 | 1404 | should 'retrieve latest info from topic when has no comments' do |
| 1392 | 1405 | forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id) |
| 1393 | - 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) | |
| 1406 | + 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, :created_by_id => profile.id) | |
| 1394 | 1407 | assert_equal post.updated_at, post.info_from_last_update[:date] |
| 1395 | 1408 | assert_equal profile.name, post.info_from_last_update[:author_name] |
| 1396 | 1409 | assert_equal profile.url, post.info_from_last_update[:author_url] |
| ... | ... | @@ -1744,30 +1757,30 @@ class ArticleTest < ActiveSupport::TestCase |
| 1744 | 1757 | |
| 1745 | 1758 | should 'set author_name before creating article if there is an author' do |
| 1746 | 1759 | author = fast_create(Person) |
| 1747 | - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => author) | |
| 1760 | + article = create(Article, :name => 'Test', :profile => profile, :created_by => author) | |
| 1748 | 1761 | assert_equal author.name, article.author_name |
| 1749 | 1762 | |
| 1750 | 1763 | author_name = author.name |
| 1751 | 1764 | author.destroy |
| 1752 | - article.reload | |
| 1765 | + article = Article.find(article.id) | |
| 1753 | 1766 | assert_equal author_name, article.author_name |
| 1754 | 1767 | end |
| 1755 | 1768 | |
| 1756 | 1769 | should "author_id return the author id of the article's author" do |
| 1757 | 1770 | author = fast_create(Person) |
| 1758 | - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => author) | |
| 1771 | + article = create(Article, :name => 'Test', :profile => profile, :created_by => author) | |
| 1759 | 1772 | assert_equal author.id, article.author_id |
| 1760 | 1773 | end |
| 1761 | 1774 | |
| 1762 | 1775 | should "author_id return nil if there is no article's author" do |
| 1763 | - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) | |
| 1776 | + article = create(Article, :name => 'Test', :profile => profile, :created_by => nil) | |
| 1764 | 1777 | assert_nil article.author_id |
| 1765 | 1778 | end |
| 1766 | 1779 | |
| 1767 | 1780 | should "return the author of a specific version" do |
| 1768 | 1781 | author1 = fast_create(Person) |
| 1769 | 1782 | author2 = fast_create(Person) |
| 1770 | - article = create(Article, :name => 'first version', :profile => profile, :last_changed_by => author1) | |
| 1783 | + article = create(Article, :name => 'first version', :profile => profile, :created_by => author1, :last_changed_by => author1) | |
| 1771 | 1784 | article.name = 'second version' |
| 1772 | 1785 | article.last_changed_by = author2 |
| 1773 | 1786 | article.save |
| ... | ... | @@ -1778,7 +1791,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 1778 | 1791 | should "return the author_name of a specific version" do |
| 1779 | 1792 | author1 = fast_create(Person) |
| 1780 | 1793 | author2 = fast_create(Person) |
| 1781 | - article = create(Article, :name => 'first version', :profile => profile, :last_changed_by => author1) | |
| 1794 | + article = create(Article, :name => 'first version', :profile => profile, :created_by => author1) | |
| 1782 | 1795 | article.name = 'second version' |
| 1783 | 1796 | article.last_changed_by = author2 |
| 1784 | 1797 | article.save | ... | ... |