Commit 4edfee6d629159727694e2b4b9c9fe89bbb44efa

Authored by Michel Felipe
1 parent a86306b1

Added archived article verification into vote plugin

plugins/vote/lib/ext/vote.rb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +require_dependency 'models/vote'
  2 +
  3 +class Vote
  4 +
  5 + validate :verify_target_archived
  6 +
  7 + def verify_target_archived
  8 +
  9 + if voteable.kind_of?(Article) || voteable.kind_of?(Comment)
  10 + if voteable.archived?
  11 + errors.add(:base, _("The target is achived and can't accept votes"))
  12 + false
  13 + end
  14 + end
  15 +
  16 + end
  17 +
  18 +end
plugins/vote/lib/vote_plugin_helper.rb
@@ -2,8 +2,10 @@ module VotePluginHelper @@ -2,8 +2,10 @@ module VotePluginHelper
2 2
3 def vote_partial(target, like = true, load_voters=false) 3 def vote_partial(target, like = true, load_voters=false)
4 vote = like ? 1 : -1 4 vote = like ? 1 : -1
  5 +
5 like_action = like ? 'like' : 'dislike' 6 like_action = like ? 'like' : 'dislike'
6 type = target.kind_of?(Article) ? 'article' : target.kind_of?(Comment) ? 'comment' : nil 7 type = target.kind_of?(Article) ? 'article' : target.kind_of?(Comment) ? 'comment' : nil
  8 + disable_vote = target.archived? ? true : false
7 9
8 proc do 10 proc do
9 settings = Noosfero::Plugin::Settings.new(environment, VotePlugin) 11 settings = Noosfero::Plugin::Settings.new(environment, VotePlugin)
@@ -14,7 +16,7 @@ module VotePluginHelper @@ -14,7 +16,7 @@ module VotePluginHelper
14 active = user ? (like ? user.voted_for?(target) : user.voted_against?(target)) : false 16 active = user ? (like ? user.voted_for?(target) : user.voted_against?(target)) : false
15 count = like ? target.votes_for : target.votes_against 17 count = like ? target.votes_for : target.votes_against
16 18
17 - render(:partial => 'vote/vote', :locals => {:target => target, :active => active, :action => like_action, :count => count, :voters => voters, :vote => vote, :model => type }) 19 + render(:partial => 'vote/vote', :locals => {:target => target, :active => active, :action => like_action, :count => count, :voters => voters, :vote => vote, :model => type, :disable_vote => disable_vote })
18 else 20 else
19 "" 21 ""
20 end 22 end
plugins/vote/public/style.css
@@ -73,3 +73,12 @@ @@ -73,3 +73,12 @@
73 #article .action .vote-detail img { 73 #article .action .vote-detail img {
74 vertical-align: bottom; 74 vertical-align: bottom;
75 } 75 }
  76 +
  77 +.comments-action-bar span.disabled a,
  78 +.comments-action-bar span.disabled a:hover,
  79 +.vote-actions span.disabled a,
  80 +.vote-actions span.disabled a:hover {
  81 + opacity: 0.5;
  82 + pointer-events: none;
  83 + cursor: default;
  84 +}
plugins/vote/public/vote_actions.js
@@ -16,7 +16,9 @@ function openVotersDialog(div) { @@ -16,7 +16,9 @@ function openVotersDialog(div) {
16 clearTimeout(openEvent); 16 clearTimeout(openEvent);
17 var url = $(div).data('reload_url'); 17 var url = $(div).data('reload_url');
18 hideAllVoteDetail(); 18 hideAllVoteDetail();
19 - $.post(url); 19 + if(url && url != '#'){
  20 + $.post(url);
  21 + }
20 } 22 }
21 23
22 jQuery('body').live('click', function() { hideAllVoteDetail(); }); 24 jQuery('body').live('click', function() { hideAllVoteDetail(); });
plugins/vote/test/functional/vote_plugin_profile_controller_test.rb
@@ -35,6 +35,16 @@ class VotePluginProfileControllerTest < ActionController::TestCase @@ -35,6 +35,16 @@ class VotePluginProfileControllerTest < ActionController::TestCase
35 assert profile.votes.empty? 35 assert profile.votes.empty?
36 end 36 end
37 37
  38 + should 'not vote if a target is archived' do
  39 + article = Article.create!(:profile => profile, :name => 'Archived article', :archived => true)
  40 + comment = Comment.create!(:body => 'Comment test', :source => article, :author => profile)
  41 + xhr :post, :vote, :profile => profile.identifier, :id => article.id, :model => 'article', :vote => 1
  42 + assert profile.votes.empty?
  43 +
  44 + xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
  45 + assert !profile.voted_for?(comment)
  46 + end
  47 +
38 should 'like comment' do 48 should 'like comment' do
39 xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1 49 xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
40 assert profile.voted_for?(comment) 50 assert profile.voted_for?(comment)
plugins/vote/views/vote/_vote.html.erb
1 <% 1 <%
2 -url = url_for(:controller => 'vote_plugin_profile', :profile => profile.identifier, :action => :vote, :id => target.id, :model => model, :vote => vote)  
3 -reload_url = url_for(:controller => 'vote_plugin_profile', :profile => profile.identifier, :action => :reload_vote, :id => target.id, :model => model, :vote => vote) 2 +if disable_vote
  3 + url = reload_url = '#'
  4 + class_action = 'disabled'
  5 +else
  6 + url = url_for(:controller => 'vote_plugin_profile', :profile => profile.identifier, :action => :vote, :id => target.id, :model => model, :vote => vote)
  7 + reload_url = url_for(:controller => 'vote_plugin_profile', :profile => profile.identifier, :action => :reload_vote, :id => target.id, :model => model, :vote => vote)
  8 +end
4 %> 9 %>
5 10
6 -<span id="vote_<%= model %>_<%= target.id %>_<%= vote %>" data-reload_url=<%= reload_url %> class="vote-action action <%= action %>-action"> 11 +<span id="vote_<%= model %>_<%= target.id %>_<%= vote %>" data-reload_url=<%= reload_url %> class="vote-action action <%= action %>-action <%= class_action %>">
7 12
8 <%= link_to content_tag(:span, count, :class=>'like-action-counter') + content_tag(:span, '', :class=>"action-icon #{action}"), url, :class => "#{active ? 'like-action-active':''} #{user ? '':'disabled'} require-login-popup" %> 13 <%= link_to content_tag(:span, count, :class=>'like-action-counter') + content_tag(:span, '', :class=>"action-icon #{action}"), url, :class => "#{active ? 'like-action-active':''} #{user ? '':'disabled'} require-login-popup" %>
9 14