Commit ce884bd92c3cdcc01db0c76f4cc11a8a90ee245f
1 parent
fbd4ea85
Exists in
master
and in
1 other branch
Load point rules by environment
Showing
6 changed files
with
75 additions
and
13 deletions
Show diff stats
lib/gamification_plugin.rb
... | ... | @@ -14,6 +14,19 @@ class GamificationPlugin < Noosfero::Plugin |
14 | 14 | end |
15 | 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 | 30 | Merit.setup do |config| |
18 | 31 | config.checks_on_each_request = false |
19 | 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 | 1 | module Merit |
12 | 2 | class PointRules |
13 | 3 | include Merit::PointRulesMethods |
... | ... | @@ -47,7 +37,8 @@ module Merit |
47 | 37 | |
48 | 38 | # FIXME get value from environment |
49 | 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 | 42 | end |
52 | 43 | |
53 | 44 | def calculate_score(target, category, value) |
... | ... | @@ -55,8 +46,10 @@ module Merit |
55 | 46 | weight(category) * value |
56 | 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 | 53 | AVAILABLE_RULES.each do |category, setting| |
61 | 54 | [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| |
62 | 55 | score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category | ... | ... |
... | ... | @@ -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
test/unit/comment_test.rb
... | ... | @@ -5,6 +5,7 @@ class CommentTest < ActiveSupport::TestCase |
5 | 5 | def setup |
6 | 6 | @person = create_user('testuser').person |
7 | 7 | @article = create(TextileArticle, :profile_id => person.id) |
8 | + GamificationPlugin.gamification_set_rules(Environment.default) | |
8 | 9 | end |
9 | 10 | attr_accessor :person, :article |
10 | 11 | ... | ... |
... | ... | @@ -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 | ... | ... |