Commit fbd4ea85e65f682fa9b87f6ed8cd5e79d2adab36
1 parent
c59a0c49
Exists in
master
and in
1 other branch
Admin page for point rules
Showing
4 changed files
with
77 additions
and
14 deletions
Show diff stats
... | ... | @@ -0,0 +1,16 @@ |
1 | + | |
2 | +class GamificationPluginAdminController < PluginAdminController | |
3 | + | |
4 | + def index | |
5 | + settings = params[:settings] | |
6 | + settings ||= {} | |
7 | + | |
8 | + @settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, settings) | |
9 | + if request.post? | |
10 | + @settings.save! | |
11 | + session[:notice] = 'Settings succefully saved.' | |
12 | + redirect_to :action => 'index' | |
13 | + end | |
14 | + end | |
15 | + | |
16 | +end | ... | ... |
lib/merit/point_rules.rb
... | ... | @@ -17,42 +17,37 @@ module Merit |
17 | 17 | :action => 'comment#create', |
18 | 18 | :undo_action => 'comment#destroy', |
19 | 19 | :to => :author, |
20 | - :value => 1 | |
20 | + :value => 1, | |
21 | + :default_weight => 10 | |
21 | 22 | }, |
22 | 23 | :article_author => { |
23 | 24 | :action => 'article#create', |
24 | 25 | :undo_action => 'article#destroy', |
25 | 26 | :to => :author, |
26 | - :value => 1 | |
27 | + :value => 1, | |
28 | + :default_weight => 50 | |
27 | 29 | }, |
28 | 30 | :vote_voteable_author => { |
29 | 31 | :action => 'vote#create', |
30 | 32 | :undo_action => 'vote#destroy', |
31 | 33 | :to => lambda {|vote| vote.voteable.author}, |
32 | 34 | :profile => lambda {|vote| vote.voteable.profile}, |
33 | - :value => lambda {|vote| vote.vote} | |
35 | + :value => lambda {|vote| vote.vote}, | |
36 | + :default_weight => 5 | |
34 | 37 | }, |
35 | 38 | :vote_voteable => { |
36 | 39 | :action => 'vote#create', |
37 | 40 | :undo_action => 'vote#destroy', |
38 | 41 | :to => lambda {|vote| vote.voteable}, |
39 | 42 | :profile => lambda {|vote| vote.voteable.profile}, |
40 | - :value => lambda {|vote| vote.vote} | |
43 | + :value => lambda {|vote| vote.vote}, | |
44 | + :default_weight => 5 | |
41 | 45 | }, |
42 | 46 | } |
43 | 47 | |
44 | 48 | # FIXME get value from environment |
45 | 49 | def weight(category) |
46 | - case category | |
47 | - when :comment_author | |
48 | - 10 | |
49 | - when :article_author | |
50 | - 50 | |
51 | - when :vote_voteable | |
52 | - 5 | |
53 | - when :vote_voteable_author | |
54 | - 5 | |
55 | - end | |
50 | + AVAILABLE_RULES[category][:default_weight] | |
56 | 51 | end |
57 | 52 | |
58 | 53 | def calculate_score(target, category, value) | ... | ... |
test/functional/gamification_plugin_admin_controller_test.rb
0 → 100644
... | ... | @@ -0,0 +1,35 @@ |
1 | +require_relative '../test_helper' | |
2 | + | |
3 | +class GamificationPluginAdminControllerTest < ActionController::TestCase | |
4 | + | |
5 | + def setup | |
6 | + @environment = Environment.default | |
7 | + @person = create_user_with_permission('profile', 'edit_environment_features', Environment.default) | |
8 | + login_as(@person.identifier) | |
9 | + end | |
10 | + | |
11 | + attr_accessor :person, :environment | |
12 | + | |
13 | + should 'save point rules' do | |
14 | + post :index, :settings => {:point_rules => {'comment_author' => {'weight' => '10'}}} | |
15 | + @settings = Noosfero::Plugin::Settings.new(environment.reload, GamificationPlugin) | |
16 | + assert_equal({:point_rules => {'comment_author' => {'weight' => '10'}}}, @settings.settings) | |
17 | + end | |
18 | + | |
19 | + should 'load default weights for point rules' do | |
20 | + get :index | |
21 | + Merit::PointRules::AVAILABLE_RULES.each do |category, setting| | |
22 | + assert_select 'input[name=?][value=?]', "settings[point_rules][#{category}[weight]]", setting[:default_weight] | |
23 | + end | |
24 | + end | |
25 | + | |
26 | + should 'load saved weights for point rules' do | |
27 | + settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {}) | |
28 | + settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}}) | |
29 | + settings.save! | |
30 | + get :index | |
31 | + assert_select 'input[name=?][value=?]', "settings[point_rules][comment_author[weight]]", 500 | |
32 | + end | |
33 | + | |
34 | +end | |
35 | + | ... | ... |
... | ... | @@ -0,0 +1,17 @@ |
1 | +<h1><%= _('Gamification Settings')%></h1> | |
2 | + | |
3 | +<%= form_for(:settings) do |f| %> | |
4 | + <%= f.fields_for :point_rules do |p| %> | |
5 | + <div class="point-rules"> | |
6 | + <h3><%= _('Point Rules') %></h3> | |
7 | + <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %> | |
8 | + <%= labelled_form_field(_(category), p.text_field("#{category}[weight]", :value => @settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', setting[:default_weight]))) %> | |
9 | + <% end %> | |
10 | + </div> | |
11 | + <% end %> | |
12 | + | |
13 | + <% button_bar do %> | |
14 | + <%= submit_button(:save, c_('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> | |
15 | + <% end %> | |
16 | + | |
17 | +<% end %> | ... | ... |