diff --git a/controllers/gamification_plugin_profile_controller.rb b/controllers/gamification_plugin_profile_controller.rb new file mode 100644 index 0000000..6d58b82 --- /dev/null +++ b/controllers/gamification_plugin_profile_controller.rb @@ -0,0 +1,8 @@ +class GamificationPluginProfileController < ProfileController + + def info + @target = current_person + render 'gamification/info' + end + +end diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index 2e7d3fd..d930925 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -8,6 +8,12 @@ class GamificationPlugin < Noosfero::Plugin _("Gamification Plugin") end + def user_data_extras + proc do + {:points => current_person.points} + end + end + Merit.setup do |config| config.checks_on_each_request = false config.user_model_name = 'Profile' diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index cffda98..677a93f 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -23,20 +23,20 @@ module Merit def initialize grant_on 'comment#create', badge: 'commenter' do |comment| - comment.author.present? && comment.author.comments.count == 5 + comment.author.present? && comment.author.comments.count >= 5 end grant_on 'article#create', badge: 'article-creator', level: 1 do |article| - article.author.present? && article.author.articles.count == 5 + article.author.present? && article.author.articles.count >= 5 end grant_on 'article#create', badge: 'article-creator', level: 2 do |article| - article.author.present? && article.author.articles.count == 10 + article.author.present? && article.author.articles.count >= 10 end grant_on 'vote_plugin_profile#vote', badge: 'relevant-commenter', model_name: 'comment', to: 'author' do |voteable| return false if voteable.nil? || !voteable.kind_of?(Comment) - voteable.votes.count == 2 + voteable.votes.count >= 2 end end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 4431ff3..6afd833 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -42,8 +42,8 @@ module Merit } # FIXME get value from environment - def weight(action) - case action + def weight(category) + case category when :comment_author 10 when :article_author @@ -55,28 +55,18 @@ module Merit end end - def calculate_score(target, action, value) + def calculate_score(target, category, value) value = value.call(target) if value.respond_to?(:call) - weight(action) * value + weight(category) * value end def initialize - AVAILABLE_RULES.each do |key, setting| - score lambda {|target| calculate_score(target, key, setting[:value])}, :on => setting[:action], :to => setting[:to] - if setting[:undo_action].present? - score lambda {|target| -calculate_score(target, key, setting[:value])}, :on => setting[:undo_action], :to => setting[:to] + AVAILABLE_RULES.each do |category, setting| + [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| + score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category end end - - #score lambda {|target| calculate_score(target, :comment_create, 1)}, :on => 'comment#create' - #score lambda {|target| calculate_score(target, :comment_create, -1)}, :on => 'comment#destroy' - - #score lambda {|target| calculate_score(target, :article_create, 1)}, :on => 'article#create' - #score lambda {|target| calculate_score(target, :article_create, -1)}, :on => 'article#destroy' - - #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable.author} - #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable} - #score lambda {|target| calculate_score(target, :vote_create, -target.vote)}, :on => 'vote#destroy', :to => lambda {|vote| vote.voteable.author} end + end end diff --git a/test/functional/gamification_plugin_profile_controller_test.rb b/test/functional/gamification_plugin_profile_controller_test.rb new file mode 100644 index 0000000..932519c --- /dev/null +++ b/test/functional/gamification_plugin_profile_controller_test.rb @@ -0,0 +1,25 @@ +require_relative '../test_helper' + +class GamificationPluginProfileControllerTest < ActionController::TestCase + + def setup + @profile = fast_create(Profile) + @person = create_user('person').person + login_as(@person.identifier) + end + + attr_accessor :profile, :person + + should 'display points in gamification info page' do + Profile.any_instance.expects(:points).returns(125) + get :info, :profile => profile.identifier + assert_tag :div, :attributes => {:class => 'score'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '125'} + end + + should 'display level in gamification info page' do + person.update_attribute(:level, 12) + get :info, :profile => profile.identifier + assert_tag :div, :attributes => {:class => 'level'}, :child => {:tag => 'span', :attributes => {:class => 'value'}, :content => '12'} + end + +end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 0731a86..d594669 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -36,7 +36,7 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit points to article owner when an user like it' do article = create(Article, :name => 'Test', :profile => person, :author => person) - assert_difference 'article.author.points', 5 do + assert_difference 'article.author.points(:category => :vote_voteable_author)', 5 do Vote.create!(:voter => person, :voteable => article, :vote => 1) end end @@ -45,7 +45,7 @@ class ArticleTest < ActiveSupport::TestCase article = create(Article, :name => 'Test', :profile => person, :author => person) article = article.reload - assert_difference 'article.points', 5 do + assert_difference 'article.points(:category => :vote_voteable)', 5 do Vote.create!(:voter => person, :voteable => article, :vote => 1) end end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 9f8af02..c82190b 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -29,7 +29,7 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to comment owner when an user like his comment' do comment = create(Comment, :source => article, :author_id => person.id) - assert_difference 'comment.author.points', 5 do + assert_difference 'comment.author.points(:category => :vote_voteable_author)', 5 do Vote.create!(:voter => person, :voteable => comment, :vote => 1) end end @@ -46,7 +46,7 @@ class CommentTest < ActiveSupport::TestCase should 'subtract merit points from comment owner when an user dislike his comment' do comment = create(Comment, :source => article, :author_id => person.id) - assert_difference 'comment.author.points', -5 do + assert_difference 'comment.author.points(:category => :vote_voteable_author)', -5 do Vote.create!(:voter => person, :voteable => comment, :vote => -1) end end diff --git a/test/unit/gamification_plugin_test.rb b/test/unit/gamification_plugin_test.rb new file mode 100644 index 0000000..30dbe35 --- /dev/null +++ b/test/unit/gamification_plugin_test.rb @@ -0,0 +1,16 @@ +require_relative '../../../../test/test_helper' + +class GamificationPluginTest < ActiveSupport::TestCase + + def setup + @plugin = GamificationPlugin.new + @current_person = create_user('person').person + end + + attr_accessor :plugin, :current_person + + should 'return user points in user_data_extras' do + assert_equal({:points => 0}, instance_eval(&plugin.user_data_extras)) + end + +end diff --git a/views/gamification/info.html.erb b/views/gamification/info.html.erb new file mode 100644 index 0000000..e4405a9 --- /dev/null +++ b/views/gamification/info.html.erb @@ -0,0 +1,24 @@ +