From 00a9e2a0cd527e9d46454f824734fc80f990e8fd Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 6 Apr 2015 17:27:48 -0300 Subject: [PATCH] Improve settings management --- controllers/gamification_plugin_admin_controller.rb | 36 ++++++++++++++++++------------------ lib/merit/point_rules.rb | 4 ++++ test/functional/gamification_plugin_admin_controller_test.rb | 6 +++--- views/gamification_plugin_admin/index.html.erb | 24 ++++++++++-------------- views/gamification_plugin_admin/points.html.erb | 18 ++++++++++++++++++ views/gamification_plugin_badges/edit.html.erb | 2 +- views/gamification_plugin_badges/index.html.erb | 2 +- views/gamification_plugin_badges/new.html.erb | 2 +- views/gamification_plugin_badges/show.html.erb | 2 +- 9 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 views/gamification_plugin_admin/points.html.erb diff --git a/controllers/gamification_plugin_admin_controller.rb b/controllers/gamification_plugin_admin_controller.rb index e3a725a..dffacfa 100644 --- a/controllers/gamification_plugin_admin_controller.rb +++ b/controllers/gamification_plugin_admin_controller.rb @@ -1,28 +1,28 @@ class GamificationPluginAdminController < PluginAdminController - def index - settings = params[:settings] - settings ||= {} + before_filter :load_settings - @settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, settings) - if request.post? - @settings.save! - session[:notice] = 'Settings succefully saved.' - redirect_to :action => 'index' + def points + if save_settings + render :file => 'gamification_plugin_admin/index' + else + render :file => 'gamification_plugin_admin/points' end end - def new_badge - if request.post? - badge = GamificationPlugin::Badge.new(params[:badge]) - badge.owner = environment - badge.save! - session[:notice] = 'Settings succefully saved.' - redirect_to :action => 'index' - else - render :file => 'gamification_plugin_admin/new_badge' - end + protected + + def save_settings + return false unless request.post? + @settings.save! + session[:notice] = 'Settings succefully saved.' + true + end + + def load_settings + settings = params[:settings] || {} + @settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, settings) end end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index cb82ba9..b9e4e70 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -8,6 +8,7 @@ module Merit :undo_action => 'comment#destroy', :to => :author, :value => 1, + :description => _('Point weight for comment author'), :default_weight => 10 }, :article_author => { @@ -15,6 +16,7 @@ module Merit :undo_action => 'article#destroy', :to => :author, :value => 1, + :description => _('Point weight for article author'), :default_weight => 50 }, :vote_voteable_author => { @@ -23,6 +25,7 @@ module Merit :to => lambda {|vote| vote.voteable.author}, :profile => lambda {|vote| vote.voteable.profile}, :value => lambda {|vote| vote.vote}, + :description => _('Point weight for the author of a voted content'), :default_weight => 5 }, :vote_voteable => { @@ -31,6 +34,7 @@ module Merit :to => lambda {|vote| vote.voteable}, :profile => lambda {|vote| vote.voteable.profile}, :value => lambda {|vote| vote.vote}, + :description => _('Point weight for a voted content'), :default_weight => 5 }, # TODO comment_voter and article_voter diff --git a/test/functional/gamification_plugin_admin_controller_test.rb b/test/functional/gamification_plugin_admin_controller_test.rb index e789c2b..0c84c7c 100644 --- a/test/functional/gamification_plugin_admin_controller_test.rb +++ b/test/functional/gamification_plugin_admin_controller_test.rb @@ -11,13 +11,13 @@ class GamificationPluginAdminControllerTest < ActionController::TestCase attr_accessor :person, :environment should 'save point rules' do - post :index, :settings => {:point_rules => {'comment_author' => {'weight' => '10'}}} + post :points, :settings => {:point_rules => {'comment_author' => {'weight' => '10'}}} @settings = Noosfero::Plugin::Settings.new(environment.reload, GamificationPlugin) assert_equal({:point_rules => {'comment_author' => {'weight' => '10'}}}, @settings.settings) end should 'load default weights for point rules' do - get :index + get :points Merit::PointRules::AVAILABLE_RULES.each do |category, setting| assert_select 'input[name=?][value=?]', "settings[point_rules][#{category}[weight]]", setting[:default_weight] end @@ -27,7 +27,7 @@ class GamificationPluginAdminControllerTest < ActionController::TestCase settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {}) settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}}) settings.save! - get :index + get :points assert_select 'input[name=?][value=?]', "settings[point_rules][comment_author[weight]]", 500 end diff --git a/views/gamification_plugin_admin/index.html.erb b/views/gamification_plugin_admin/index.html.erb index 130f4ce..23cf9f2 100644 --- a/views/gamification_plugin_admin/index.html.erb +++ b/views/gamification_plugin_admin/index.html.erb @@ -1,19 +1,15 @@

<%= _('Gamification Settings')%>

-<%= form_for(:settings) do |f| %> - <%= f.fields_for :point_rules do |p| %> -
-

<%= _('Point Rules') %>

- <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %> - <%= labelled_form_field(_(category), p.text_field("#{category}[weight]", :value => @settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', setting[:default_weight]))) %> - <% end %> -
- <% end %> +
+ <%= link_to _('Manage Point Rules'), :controller => 'gamification_plugin_admin', :action => :points %> +
- <% button_bar do %> - <%= submit_button(:save, c_('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> - <% end %> +
+ <%= link_to _('Manage Badges'), :controller => 'gamification_plugin_badges' %> +
-<% end %> +
+ <%#= link_to _('Manage Levels'), :controller => 'gamification_plugin_admin', :action => :levels %> +
-<%= link_to _('Badges'), :controller => 'gamification_plugin_badges' %> +<%= button :cancel, _('Cancel'), :controller => :plugins, :action => :index %> diff --git a/views/gamification_plugin_admin/points.html.erb b/views/gamification_plugin_admin/points.html.erb new file mode 100644 index 0000000..ff541be --- /dev/null +++ b/views/gamification_plugin_admin/points.html.erb @@ -0,0 +1,18 @@ +

<%= _('Gamification Settings: Point Rules')%>

+ +<%= form_for(:settings) do |f| %> + <%= f.fields_for :point_rules do |p| %> +
+

<%= _('Point Rules') %>

+ <% Merit::PointRules::AVAILABLE_RULES.each do |category, setting| %> + <%= 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]))) %> + <% end %> +
+ <% end %> + + <% button_bar do %> + <%= submit_button(:save, c_('Save'), :cancel => {:action => 'index'}) %> + <% end %> + +<% end %> + diff --git a/views/gamification_plugin_badges/edit.html.erb b/views/gamification_plugin_badges/edit.html.erb index 8f14c82..e2d2d03 100644 --- a/views/gamification_plugin_badges/edit.html.erb +++ b/views/gamification_plugin_badges/edit.html.erb @@ -1,4 +1,4 @@ -

<%= _('Gamification Plugin: Editing Badge') %>

+

<%= _('Gamification Settings: Editing Badge') %>

<%= render 'form' %> diff --git a/views/gamification_plugin_badges/index.html.erb b/views/gamification_plugin_badges/index.html.erb index 6ff5ea6..2cababf 100644 --- a/views/gamification_plugin_badges/index.html.erb +++ b/views/gamification_plugin_badges/index.html.erb @@ -1,4 +1,4 @@ -

<%= _('Gamification Plugin: Listing Badges') %>

+

<%= _('Gamification Settings: Listing Badges') %>

diff --git a/views/gamification_plugin_badges/new.html.erb b/views/gamification_plugin_badges/new.html.erb index 4351db8..f31abf0 100644 --- a/views/gamification_plugin_badges/new.html.erb +++ b/views/gamification_plugin_badges/new.html.erb @@ -1,4 +1,4 @@ -

<%= _('Gamification Plugin: New Badge') %>

+

<%= _('Gamification Settings: New Badge') %>

<%= render 'form' %> diff --git a/views/gamification_plugin_badges/show.html.erb b/views/gamification_plugin_badges/show.html.erb index 65c84a6..70ec21d 100644 --- a/views/gamification_plugin_badges/show.html.erb +++ b/views/gamification_plugin_badges/show.html.erb @@ -1,4 +1,4 @@ -

<%= _('Gamification Plugin: Show Badge') %>

+

<%= _('Gamification Settings: Show Badge') %>

<%= notice %>

-- libgit2 0.21.2