diff --git a/lib/gamification_plugin/dashboard_helper.rb b/lib/gamification_plugin/dashboard_helper.rb index ecbb632..b482284 100644 --- a/lib/gamification_plugin/dashboard_helper.rb +++ b/lib/gamification_plugin/dashboard_helper.rb @@ -21,6 +21,10 @@ module GamificationPlugin::DashboardHelper url.present? ? link_to(text, url) : text end + def score_point_action_class(point) + point.undo_rule? ? 'undo_action':'do_action' + end + def ranking(target, from_date=nil, limit=10) # FIXME move these queries to profile model 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') diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 9611c7b..602afc1 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -173,8 +173,7 @@ module Merit end def self.target_url(point) - point_type = GamificationPlugin::PointsType.where(id: point.score.category).first - rule_name = point_type.present? ? point_type.name : point.score.category + rule_name = point.point_type.present? ? point.point_type.name : point.score.category target_url = AVAILABLE_RULES[rule_name.to_sym][:target_url] return nil if target_url.blank? || point.action.blank? diff --git a/lib/merit_ext.rb b/lib/merit_ext.rb index 4aceeed..8e9a447 100644 --- a/lib/merit_ext.rb +++ b/lib/merit_ext.rb @@ -17,6 +17,15 @@ module Merit class Score class Point belongs_to :action + + def point_type + @point_type ||= GamificationPlugin::PointsType.where(id: score.category).first + end + + def undo_rule? + rule = Merit::PointRules::AVAILABLE_RULES[point_type.name.to_sym] + rule[:undo_action] == "#{action.target_model}##{action.action_method}" + end end end diff --git a/public/style.css b/public/style.css index f0d03f0..9e2afee 100644 --- a/public/style.css +++ b/public/style.css @@ -237,3 +237,7 @@ float: left; width: 180px; } + +.gamification-dashboard .points .score.undo_action .category { + text-decoration: line-through; +} diff --git a/test/functional/gamification_plugin_profile_controller_test.rb b/test/functional/gamification_plugin_profile_controller_test.rb index 3bbfd25..4a47686 100644 --- a/test/functional/gamification_plugin_profile_controller_test.rb +++ b/test/functional/gamification_plugin_profile_controller_test.rb @@ -11,12 +11,13 @@ class GamificationPluginProfileControllerTest < ActionController::TestCase attr_accessor :person, :environment should 'display points in gamification dashboard' do - person.add_points(20, :category => :comment_author) - person.add_points(30, :category => :article_author) + create_all_point_rules + article = create(TextArticle, :profile_id => fast_create(Community).id, :author => person) + create(Comment, :source => article, :author_id => create_user.person.id) get :dashboard, :profile => person.identifier - assert_tag :div, :attributes => {:class => 'score article_author positive'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '30'} - assert_tag :div, :attributes => {:class => 'score comment_author positive'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '20'} - assert_tag :div, :attributes => {:class => 'score total'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '50'} + 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} + 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} + 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} end should 'display level in gamification dashboard' do diff --git a/test/unit/merit_ext_test.rb b/test/unit/merit_ext_test.rb new file mode 100644 index 0000000..0278089 --- /dev/null +++ b/test/unit/merit_ext_test.rb @@ -0,0 +1,27 @@ +require_relative "../test_helper" + +class MeritExtTest < ActiveSupport::TestCase + + should 'check if the point was originated by an undo action' do + point = Merit::Score::Point.new + point_type = GamificationPlugin::PointsType.new(name: :comment_author) + point.expects(:point_type).returns(point_type) + action = mock + action.expects(:target_model).returns('comment') + action.expects(:action_method).returns('destroy') + point.expects(:action).at_least_once.returns(action) + assert point.undo_rule? + end + + should 'check if the point was originated by a do action' do + point = Merit::Score::Point.new + point_type = GamificationPlugin::PointsType.new(name: :comment_author) + point.expects(:point_type).returns(point_type) + action = mock + action.expects(:target_model).returns('comment') + action.expects(:action_method).returns('create') + point.expects(:action).at_least_once.returns(action) + assert !point.undo_rule? + end + +end diff --git a/views/gamification/dashboard.html.erb b/views/gamification/dashboard.html.erb index 82061d8..13b726a 100644 --- a/views/gamification/dashboard.html.erb +++ b/views/gamification/dashboard.html.erb @@ -22,7 +22,7 @@