Commit ce884bd92c3cdcc01db0c76f4cc11a8a90ee245f

Authored by Victor Costa
1 parent fbd4ea85

Load point rules by environment

lib/gamification_plugin.rb
@@ -14,6 +14,19 @@ class GamificationPlugin < Noosfero::Plugin @@ -14,6 +14,19 @@ class GamificationPlugin < Noosfero::Plugin
14 end 14 end
15 end 15 end
16 16
  17 + # Override initial rules with environment specific rules
  18 + def self.gamification_set_rules(environment)
  19 + Merit::AppPointRules.clear
  20 + Merit::AppPointRules.merge!(Merit::PointRules.new(environment).defined_rules)
  21 + end
  22 +
  23 + def application_controller_filters
  24 + [{
  25 + :type => 'before_filter', :method_name => 'gamification_set_rules',
  26 + :options => {}, :block => proc { GamificationPlugin.gamification_set_rules(environment) }
  27 + }]
  28 + end
  29 +
17 Merit.setup do |config| 30 Merit.setup do |config|
18 config.checks_on_each_request = false 31 config.checks_on_each_request = false
19 config.user_model_name = 'Profile' 32 config.user_model_name = 'Profile'
lib/merit/point_rules.rb
1 -# Be sure to restart your server when you modify this file.  
2 -#  
3 -# Points are a simple integer value which are given to "meritable" resources  
4 -# according to rules in +app/models/merit/point_rules.rb+. They are given on  
5 -# actions-triggered, either to the action user or to the method (or array of  
6 -# methods) defined in the +:to+ option.  
7 -#  
8 -# 'score' method may accept a block which evaluates to boolean  
9 -# (recieves the object as parameter)  
10 -  
11 module Merit 1 module Merit
12 class PointRules 2 class PointRules
13 include Merit::PointRulesMethods 3 include Merit::PointRulesMethods
@@ -47,7 +37,8 @@ module Merit @@ -47,7 +37,8 @@ module Merit
47 37
48 # FIXME get value from environment 38 # FIXME get value from environment
49 def weight(category) 39 def weight(category)
50 - AVAILABLE_RULES[category][:default_weight] 40 + settings = Noosfero::Plugin::Settings.new(@environment, GamificationPlugin)
  41 + settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', AVAILABLE_RULES[category][:default_weight]).to_i
51 end 42 end
52 43
53 def calculate_score(target, category, value) 44 def calculate_score(target, category, value)
@@ -55,8 +46,10 @@ module Merit @@ -55,8 +46,10 @@ module Merit
55 weight(category) * value 46 weight(category) * value
56 end 47 end
57 48
58 - # TODO receive environment parameter  
59 - def initialize 49 + def initialize(environment=nil)
  50 + return if environment.nil?
  51 + @environment = environment
  52 +
60 AVAILABLE_RULES.each do |category, setting| 53 AVAILABLE_RULES.each do |category, setting|
61 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| 54 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal|
62 score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category 55 score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category
test/functional/application_controller_test.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +require_relative '../test_helper'
  2 +require 'test_controller'
  3 +
  4 +class TestController; def rescue_action(e) raise e end; end
  5 +
  6 +class ApplicationControllerTest < ActionController::TestCase
  7 +
  8 + def setup
  9 + @environment = Environment.default
  10 + @environment.enable_plugin(GamificationPlugin)
  11 + @controller = TestController.new
  12 + @controller.stubs(:environment).returns(@environment)
  13 + end
  14 +
  15 + should 'redefine rules in before filter' do
  16 + get :index
  17 + assert Merit::AppPointRules.present?
  18 + end
  19 +
  20 +end
test/unit/article_test.rb
@@ -4,6 +4,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -4,6 +4,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
4 4
5 def setup 5 def setup
6 @person = create_user('testuser').person 6 @person = create_user('testuser').person
  7 + GamificationPlugin.gamification_set_rules(Environment.default)
7 end 8 end
8 9
9 attr_accessor :person 10 attr_accessor :person
test/unit/comment_test.rb
@@ -5,6 +5,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -5,6 +5,7 @@ class CommentTest &lt; ActiveSupport::TestCase
5 def setup 5 def setup
6 @person = create_user('testuser').person 6 @person = create_user('testuser').person
7 @article = create(TextileArticle, :profile_id => person.id) 7 @article = create(TextileArticle, :profile_id => person.id)
  8 + GamificationPlugin.gamification_set_rules(Environment.default)
8 end 9 end
9 attr_accessor :person, :article 10 attr_accessor :person, :article
10 11
test/unit/point_rules_test.rb 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class PointRulesTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @environment = Environment.default
  7 + @point_rules = Merit::PointRules.new(@environment)
  8 + end
  9 +
  10 + attr_accessor :environment, :point_rules
  11 +
  12 + should 'not define rules when environment is nil' do
  13 + point_rules = Merit::PointRules.new
  14 + assert point_rules.defined_rules.blank?
  15 + end
  16 +
  17 + should 'define rules when environment is present' do
  18 + assert point_rules.defined_rules.present?
  19 + end
  20 +
  21 + should 'weight returns the default value when value is not setted in environment' do
  22 + Merit::PointRules::AVAILABLE_RULES.each do |category, setting|
  23 + assert_equal setting[:default_weight], point_rules.weight(category)
  24 + end
  25 + end
  26 +
  27 + should 'weight returns value from environment when it is setted' do
  28 + settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {})
  29 + settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}})
  30 + settings.save!
  31 + assert_equal 500, point_rules.weight(:comment_author)
  32 + end
  33 +
  34 +end