Commit 1f192ed48a676f24d3f2bb95982f392d801042cf

Authored by Victor Costa
1 parent 75804df3

Display undo action in points

lib/gamification_plugin/dashboard_helper.rb
@@ -21,6 +21,10 @@ module GamificationPlugin::DashboardHelper @@ -21,6 +21,10 @@ module GamificationPlugin::DashboardHelper
21 url.present? ? link_to(text, url) : text 21 url.present? ? link_to(text, url) : text
22 end 22 end
23 23
  24 + def score_point_action_class(point)
  25 + point.undo_rule? ? 'undo_action':'do_action'
  26 + end
  27 +
24 def ranking(target, from_date=nil, limit=10) 28 def ranking(target, from_date=nil, limit=10)
25 # FIXME move these queries to profile model 29 # FIXME move these queries to profile model
26 ranking = Profile.select('profiles.*, sum(num_points) as gamification_points, ROW_NUMBER() OVER(order by sum(num_points) DESC) as gamification_position').joins(:sash => {:scores => :score_points}).where(:type => target.class).order('sum(num_points) DESC').group('profiles.id') 30 ranking = Profile.select('profiles.*, sum(num_points) as gamification_points, ROW_NUMBER() OVER(order by sum(num_points) DESC) as gamification_position').joins(:sash => {:scores => :score_points}).where(:type => target.class).order('sum(num_points) DESC').group('profiles.id')
lib/merit/point_rules.rb
@@ -173,8 +173,7 @@ module Merit @@ -173,8 +173,7 @@ module Merit
173 end 173 end
174 174
175 def self.target_url(point) 175 def self.target_url(point)
176 - point_type = GamificationPlugin::PointsType.where(id: point.score.category).first  
177 - rule_name = point_type.present? ? point_type.name : point.score.category 176 + rule_name = point.point_type.present? ? point.point_type.name : point.score.category
178 target_url = AVAILABLE_RULES[rule_name.to_sym][:target_url] 177 target_url = AVAILABLE_RULES[rule_name.to_sym][:target_url]
179 return nil if target_url.blank? || point.action.blank? 178 return nil if target_url.blank? || point.action.blank?
180 179
lib/merit_ext.rb
@@ -17,6 +17,15 @@ module Merit @@ -17,6 +17,15 @@ module Merit
17 class Score 17 class Score
18 class Point 18 class Point
19 belongs_to :action 19 belongs_to :action
  20 +
  21 + def point_type
  22 + @point_type ||= GamificationPlugin::PointsType.where(id: score.category).first
  23 + end
  24 +
  25 + def undo_rule?
  26 + rule = Merit::PointRules::AVAILABLE_RULES[point_type.name.to_sym]
  27 + rule[:undo_action] == "#{action.target_model}##{action.action_method}"
  28 + end
20 end 29 end
21 end 30 end
22 31
public/style.css
@@ -237,3 +237,7 @@ @@ -237,3 +237,7 @@
237 float: left; 237 float: left;
238 width: 180px; 238 width: 180px;
239 } 239 }
  240 +
  241 +.gamification-dashboard .points .score.undo_action .category {
  242 + text-decoration: line-through;
  243 +}
test/functional/gamification_plugin_profile_controller_test.rb
@@ -11,12 +11,13 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase @@ -11,12 +11,13 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase
11 attr_accessor :person, :environment 11 attr_accessor :person, :environment
12 12
13 should 'display points in gamification dashboard' do 13 should 'display points in gamification dashboard' do
14 - person.add_points(20, :category => :comment_author)  
15 - person.add_points(30, :category => :article_author) 14 + create_all_point_rules
  15 + article = create(TextArticle, :profile_id => fast_create(Community).id, :author => person)
  16 + create(Comment, :source => article, :author_id => create_user.person.id)
16 get :dashboard, :profile => person.identifier 17 get :dashboard, :profile => person.identifier
17 - assert_tag :div, :attributes => {:class => 'score article_author positive'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '30'}  
18 - assert_tag :div, :attributes => {:class => 'score comment_author positive'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '20'}  
19 - assert_tag :div, :attributes => {:class => 'score total'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '50'} 18 + assert_tag :div, :attributes => {:class => 'score article_author positive do_action'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => default_point_weight(:article_author).to_s}
  19 + assert_tag :div, :attributes => {:class => 'score comment_article_author positive do_action'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => default_point_weight(:comment_article_author).to_s}
  20 + assert_tag :div, :attributes => {:class => 'score total'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => (default_point_weight(:comment_article_author) + default_point_weight(:article_author)).to_s}
20 end 21 end
21 22
22 should 'display level in gamification dashboard' do 23 should 'display level in gamification dashboard' do
test/unit/merit_ext_test.rb 0 → 100644
@@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class MeritExtTest < ActiveSupport::TestCase
  4 +
  5 + should 'check if the point was originated by an undo action' do
  6 + point = Merit::Score::Point.new
  7 + point_type = GamificationPlugin::PointsType.new(name: :comment_author)
  8 + point.expects(:point_type).returns(point_type)
  9 + action = mock
  10 + action.expects(:target_model).returns('comment')
  11 + action.expects(:action_method).returns('destroy')
  12 + point.expects(:action).at_least_once.returns(action)
  13 + assert point.undo_rule?
  14 + end
  15 +
  16 + should 'check if the point was originated by a do action' do
  17 + point = Merit::Score::Point.new
  18 + point_type = GamificationPlugin::PointsType.new(name: :comment_author)
  19 + point.expects(:point_type).returns(point_type)
  20 + action = mock
  21 + action.expects(:target_model).returns('comment')
  22 + action.expects(:action_method).returns('create')
  23 + point.expects(:action).at_least_once.returns(action)
  24 + assert !point.undo_rule?
  25 + end
  26 +
  27 +end
views/gamification/dashboard.html.erb
@@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
22 <div class="scores"> 22 <div class="scores">
23 <h3><%= _('Latest Score Points') %></h3> 23 <h3><%= _('Latest Score Points') %></h3>
24 <% @target.score_points.order('created_at desc').limit(5).each do |point| %> 24 <% @target.score_points.order('created_at desc').limit(5).each do |point| %>
25 - <div class="score <%= point.score.category %> <%= score_point_class(point) %>"> 25 + <div class="score <%= point.point_type.name %> <%= score_point_class(point) %> <%= score_point_action_class(point) %>">
26 <span class="value"><%= point.num_points %></span> 26 <span class="value"><%= point.num_points %></span>
27 <span class="category"><%= score_point_target_link point, _(score_point_category(point)) %></span> 27 <span class="category"><%= score_point_target_link point, _(score_point_category(point)) %></span>
28 <span class="date timeago" title="<%= point.created_at %>"><%= point.created_at %></span> 28 <span class="date timeago" title="<%= point.created_at %>"><%= point.created_at %></span>