Commit 00a9e2a0cd527e9d46454f824734fc80f990e8fd

Authored by Victor Costa
1 parent 1811287f

Improve settings management

controllers/gamification_plugin_admin_controller.rb
1 1
2 class GamificationPluginAdminController < PluginAdminController 2 class GamificationPluginAdminController < PluginAdminController
3 3
4 - def index  
5 - settings = params[:settings]  
6 - settings ||= {} 4 + before_filter :load_settings
7 5
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' 6 + def points
  7 + if save_settings
  8 + render :file => 'gamification_plugin_admin/index'
  9 + else
  10 + render :file => 'gamification_plugin_admin/points'
13 end 11 end
14 end 12 end
15 13
16 - def new_badge  
17 - if request.post?  
18 - badge = GamificationPlugin::Badge.new(params[:badge])  
19 - badge.owner = environment  
20 - badge.save!  
21 - session[:notice] = 'Settings succefully saved.'  
22 - redirect_to :action => 'index'  
23 - else  
24 - render :file => 'gamification_plugin_admin/new_badge'  
25 - end 14 + protected
  15 +
  16 + def save_settings
  17 + return false unless request.post?
  18 + @settings.save!
  19 + session[:notice] = 'Settings succefully saved.'
  20 + true
  21 + end
  22 +
  23 + def load_settings
  24 + settings = params[:settings] || {}
  25 + @settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, settings)
26 end 26 end
27 27
28 end 28 end
lib/merit/point_rules.rb
@@ -8,6 +8,7 @@ module Merit @@ -8,6 +8,7 @@ module Merit
8 :undo_action => 'comment#destroy', 8 :undo_action => 'comment#destroy',
9 :to => :author, 9 :to => :author,
10 :value => 1, 10 :value => 1,
  11 + :description => _('Point weight for comment author'),
11 :default_weight => 10 12 :default_weight => 10
12 }, 13 },
13 :article_author => { 14 :article_author => {
@@ -15,6 +16,7 @@ module Merit @@ -15,6 +16,7 @@ module Merit
15 :undo_action => 'article#destroy', 16 :undo_action => 'article#destroy',
16 :to => :author, 17 :to => :author,
17 :value => 1, 18 :value => 1,
  19 + :description => _('Point weight for article author'),
18 :default_weight => 50 20 :default_weight => 50
19 }, 21 },
20 :vote_voteable_author => { 22 :vote_voteable_author => {
@@ -23,6 +25,7 @@ module Merit @@ -23,6 +25,7 @@ module Merit
23 :to => lambda {|vote| vote.voteable.author}, 25 :to => lambda {|vote| vote.voteable.author},
24 :profile => lambda {|vote| vote.voteable.profile}, 26 :profile => lambda {|vote| vote.voteable.profile},
25 :value => lambda {|vote| vote.vote}, 27 :value => lambda {|vote| vote.vote},
  28 + :description => _('Point weight for the author of a voted content'),
26 :default_weight => 5 29 :default_weight => 5
27 }, 30 },
28 :vote_voteable => { 31 :vote_voteable => {
@@ -31,6 +34,7 @@ module Merit @@ -31,6 +34,7 @@ module Merit
31 :to => lambda {|vote| vote.voteable}, 34 :to => lambda {|vote| vote.voteable},
32 :profile => lambda {|vote| vote.voteable.profile}, 35 :profile => lambda {|vote| vote.voteable.profile},
33 :value => lambda {|vote| vote.vote}, 36 :value => lambda {|vote| vote.vote},
  37 + :description => _('Point weight for a voted content'),
34 :default_weight => 5 38 :default_weight => 5
35 }, 39 },
36 # TODO comment_voter and article_voter 40 # TODO comment_voter and article_voter
test/functional/gamification_plugin_admin_controller_test.rb
@@ -11,13 +11,13 @@ class GamificationPluginAdminControllerTest &lt; ActionController::TestCase @@ -11,13 +11,13 @@ class GamificationPluginAdminControllerTest &lt; ActionController::TestCase
11 attr_accessor :person, :environment 11 attr_accessor :person, :environment
12 12
13 should 'save point rules' do 13 should 'save point rules' do
14 - post :index, :settings => {:point_rules => {'comment_author' => {'weight' => '10'}}} 14 + post :points, :settings => {:point_rules => {'comment_author' => {'weight' => '10'}}}
15 @settings = Noosfero::Plugin::Settings.new(environment.reload, GamificationPlugin) 15 @settings = Noosfero::Plugin::Settings.new(environment.reload, GamificationPlugin)
16 assert_equal({:point_rules => {'comment_author' => {'weight' => '10'}}}, @settings.settings) 16 assert_equal({:point_rules => {'comment_author' => {'weight' => '10'}}}, @settings.settings)
17 end 17 end
18 18
19 should 'load default weights for point rules' do 19 should 'load default weights for point rules' do
20 - get :index 20 + get :points
21 Merit::PointRules::AVAILABLE_RULES.each do |category, setting| 21 Merit::PointRules::AVAILABLE_RULES.each do |category, setting|
22 assert_select 'input[name=?][value=?]', "settings[point_rules][#{category}[weight]]", setting[:default_weight] 22 assert_select 'input[name=?][value=?]', "settings[point_rules][#{category}[weight]]", setting[:default_weight]
23 end 23 end
@@ -27,7 +27,7 @@ class GamificationPluginAdminControllerTest &lt; ActionController::TestCase @@ -27,7 +27,7 @@ class GamificationPluginAdminControllerTest &lt; ActionController::TestCase
27 settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {}) 27 settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {})
28 settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}}) 28 settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}})
29 settings.save! 29 settings.save!
30 - get :index 30 + get :points
31 assert_select 'input[name=?][value=?]', "settings[point_rules][comment_author[weight]]", 500 31 assert_select 'input[name=?][value=?]', "settings[point_rules][comment_author[weight]]", 500
32 end 32 end
33 33
views/gamification_plugin_admin/index.html.erb
1 <h1><%= _('Gamification Settings')%></h1> 1 <h1><%= _('Gamification Settings')%></h1>
2 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 %> 3 +<div>
  4 + <%= link_to _('Manage Point Rules'), :controller => 'gamification_plugin_admin', :action => :points %>
  5 +</div>
12 6
13 - <% button_bar do %>  
14 - <%= submit_button(:save, c_('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>  
15 - <% end %> 7 +<div>
  8 + <%= link_to _('Manage Badges'), :controller => 'gamification_plugin_badges' %>
  9 +</div>
16 10
17 -<% end %> 11 +<div>
  12 + <%#= link_to _('Manage Levels'), :controller => 'gamification_plugin_admin', :action => :levels %>
  13 +</div>
18 14
19 -<%= link_to _('Badges'), :controller => 'gamification_plugin_badges' %> 15 +<%= button :cancel, _('Cancel'), :controller => :plugins, :action => :index %>
views/gamification_plugin_admin/points.html.erb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +<h1><%= _('Gamification Settings: Point Rules')%></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(_(setting[:description]), 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 => {:action => 'index'}) %>
  15 + <% end %>
  16 +
  17 +<% end %>
  18 +
views/gamification_plugin_badges/edit.html.erb
1 -<h1><%= _('Gamification Plugin: Editing Badge') %></h1> 1 +<h1><%= _('Gamification Settings: Editing Badge') %></h1>
2 2
3 <%= render 'form' %> 3 <%= render 'form' %>
4 4
views/gamification_plugin_badges/index.html.erb
1 -<h1><%= _('Gamification Plugin: Listing Badges') %></h1> 1 +<h1><%= _('Gamification Settings: Listing Badges') %></h1>
2 2
3 <table> 3 <table>
4 <tr> 4 <tr>
views/gamification_plugin_badges/new.html.erb
1 -<h1><%= _('Gamification Plugin: New Badge') %></h1> 1 +<h1><%= _('Gamification Settings: New Badge') %></h1>
2 2
3 <%= render 'form' %> 3 <%= render 'form' %>
4 4
views/gamification_plugin_badges/show.html.erb
1 -<h1><%= _('Gamification Plugin: Show Badge') %></h1> 1 +<h1><%= _('Gamification Settings: Show Badge') %></h1>
2 2
3 <p id="notice"><%= notice %></p> 3 <p id="notice"><%= notice %></p>
4 4