From 5b9232378ea22313997d4a22626c17226da12f90 Mon Sep 17 00:00:00 2001 From: Michel Felipe de Oliveira Ferreira Date: Thu, 19 Nov 2015 08:53:14 -0300 Subject: [PATCH] Avoid count hits and add comments in archived articles. Added to new unit tests and translated msg --- app/controllers/my_profile/cms_controller.rb | 1 - app/models/article.rb | 18 +++++++++++++----- app/models/comment.rb | 8 ++++++++ app/views/content_viewer/_publishing_info.html.erb | 2 +- plugins/vote/test/functional/vote_plugin_profile_controller_test.rb | 6 ++++-- po/noosfero.pot | 4 ++++ po/pt/noosfero.po | 8 ++++++-- test/unit/article_test.rb | 32 ++++++++++++++++++++++++++++++++ test/unit/comment_test.rb | 11 +++++++++++ 9 files changed, 79 insertions(+), 11 deletions(-) diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 334d872..701a2dd 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -99,7 +99,6 @@ class CmsController < MyProfileController @article.image.save! end @article.last_changed_by = user - @article.old_parent_id = @article.parent_id if @article.update_attributes(params[:article]) if !continue if @article.content_type.nil? || @article.image? diff --git a/app/models/article.rb b/app/models/article.rb index da75d53..f84ebc3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -638,13 +638,21 @@ class Article < ActiveRecord::Base end def hit - self.class.connection.execute('update articles set hits = hits + 1 where id = %d' % self.id.to_i) - self.hits += 1 + if !archived? + self.class.connection.execute('update articles set hits = hits + 1 where id = %d' % self.id.to_i) + self.hits += 1 + end end def self.hit(articles) - Article.where(:id => articles.map(&:id)).update_all('hits = hits + 1') - articles.each { |a| a.hits += 1 } + ids = [] + articles.each do |article| + if !article.archived? + ids << article.id + article.hits += 1 + end + end + Article.where(:id => ids).update_all('hits = hits + 1') if !ids.empty? end def can_display_hits? @@ -851,7 +859,7 @@ class Article < ActiveRecord::Base end def parent_archived? - if (self.old_parent_id != self.parent_id) && self.parent && self.parent.archived? + if self.parent_id_changed? && self.parent && self.parent.archived? errors.add(:parent_folder, N_('is archived!!')) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 487d861..b6fe41e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -35,6 +35,8 @@ class Comment < ActiveRecord::Base end end + validate :article_archived? + acts_as_having_settings xss_terminate :only => [ :body, :title, :name ], :on => 'validation' @@ -214,4 +216,10 @@ class Comment < ActiveRecord::Base self.article.archived? end + protected + + def article_archived? + errors.add(:article, N_('associated with this comment is achived!')) if archived? + end + end diff --git a/app/views/content_viewer/_publishing_info.html.erb b/app/views/content_viewer/_publishing_info.html.erb index 4ad7e71..6447b10 100644 --- a/app/views/content_viewer/_publishing_info.html.erb +++ b/app/views/content_viewer/_publishing_info.html.erb @@ -16,7 +16,7 @@
<% if @page.display_hits? %>
- <%= n_('Viewed one time', 'Viewed %{num} times', @page.hits) % { :num => @page.hits } %> + <%= n_('Viewed one time %{desc}', 'Viewed %{num} times %{desc}', @page.hits) % { :num => @page.hits, :desc => @page.archived? ? ''+_('(Not countable anymore)')+'' : '' } %>
<% end %> diff --git a/plugins/vote/test/functional/vote_plugin_profile_controller_test.rb b/plugins/vote/test/functional/vote_plugin_profile_controller_test.rb index db30150..0b2af13 100644 --- a/plugins/vote/test/functional/vote_plugin_profile_controller_test.rb +++ b/plugins/vote/test/functional/vote_plugin_profile_controller_test.rb @@ -36,12 +36,14 @@ class VotePluginProfileControllerTest < ActionController::TestCase end should 'not vote if a target is archived' do - article = Article.create!(:profile => profile, :name => 'Archived article', :archived => true) + article = Article.create!(:profile => profile, :name => 'Archived article', :archived => false) comment = Comment.create!(:body => 'Comment test', :source => article, :author => profile) xhr :post, :vote, :profile => profile.identifier, :id => article.id, :model => 'article', :vote => 1 - assert profile.votes.empty? + assert !profile.votes.empty? + article.update_attributes(:archived => true) xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1 + assert !profile.voted_for?(comment) end diff --git a/po/noosfero.pot b/po/noosfero.pot index 5178dc8..f90c7ef 100644 --- a/po/noosfero.pot +++ b/po/noosfero.pot @@ -5073,6 +5073,10 @@ msgid_plural "Viewed %{num} times" msgstr[0] "" msgstr[1] "" +#: app/views/content_viewer/_publishing_info.html.erb:19 +msgid "(Not countable anymore)" +msgstr "" + #: app/views/content_viewer/versions_diff.html.erb:5 msgid "Changes on \"%s\"" msgstr "" diff --git a/po/pt/noosfero.po b/po/pt/noosfero.po index dc7a024..8e7ab13 100644 --- a/po/pt/noosfero.po +++ b/po/pt/noosfero.po @@ -5231,11 +5231,15 @@ msgid "Reply" msgstr "Responder" #: app/views/content_viewer/_publishing_info.html.erb:19 -msgid "Viewed one time" -msgid_plural "Viewed %{num} times" +msgid "Viewed one time %{desc}" +msgid_plural "Viewed %{num} times %{desc}" msgstr[0] "Visualizado uma vez" msgstr[1] "Visualizado %{num} vezes" +#: app/views/content_viewer/_publishing_info.html.erb:19 +msgid "(Not countable anymore)" +msgstr "(Não mais contabilizado)" + #: app/views/content_viewer/versions_diff.html.erb:5 msgid "Changes on \"%s\"" msgstr "Mudanças em \"%s\"" diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 0787cb3..30593b5 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -2177,10 +2177,21 @@ class ArticleTest < ActiveSupport::TestCase a3 = fast_create(Article) Article.hit([a1, a2, a3]) Article.hit([a2, a3]) + assert_equal [1, 2, 2], [a1.hits, a2.hits, a3.hits] assert_equal [1, 2, 2], [a1.reload.hits, a2.reload.hits, a3.reload.hits] end + should 'not update hit attribute of archiveds articles' do + a1 = fast_create(Article) + a2 = fast_create(Article, :archived => true) + a3 = fast_create(Article, :archived => true) + Article.hit([a1, a2, a3]) + + assert_equal [1, 0, 0], [a1.hits, a2.hits, a3.hits] + assert_equal [1, 0, 0], [a1.reload.hits, a2.reload.hits, a3.reload.hits] + end + should 'vote in a article' do article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) profile.vote(article, 5) @@ -2222,4 +2233,25 @@ class ArticleTest < ActiveSupport::TestCase assert !a.display_preview? end + should 'check if a article is archived' do + folder = Folder.create!(:name => 'Parent Archived', :profile => profile) + a1 = Article.create!(:name => 'Test', :profile => profile, :parent_id => folder.id, :archived => false) + a2 = Article.create!(:name => 'Test 2', :profile => profile, :archived => true) + folder.update_attributes(:archived => true) + a1.reload + + assert a1.archived? + assert a2.archived? + end + + should 'try add a child article to a archived folder' do + folder = Folder.create!(:name => 'Parent Archived', :profile => profile, :archived => true) + + err = assert_raises ActiveRecord::RecordInvalid do + a1 = Article.create!(:name => 'Test', :profile => profile, :parent_id => folder.id, :archived => false) + end + + assert_match 'Parent folder is archived', err.message + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index cdd63f0..4030228 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -94,6 +94,17 @@ class CommentTest < ActiveSupport::TestCase assert_equal cc + 1, ActionTracker::Record.find(activity.id).comments_count end + should 'try add a comment to a archived article' do + person = fast_create(Person) + article = Article.create!(:name => 'Test', :profile => person, :archived => true) + + err = assert_raises ActiveRecord::RecordInvalid do + comment = create(Comment, :source => article, :author_id => person.id) + end + + assert_match 'Article associated with this comment is achived', err.message + end + should 'provide author name for authenticated authors' do owner = create_user('testuser').person assert_equal 'testuser', build(Comment, :author => owner).author_name -- libgit2 0.21.2