From fbd4ea85e65f682fa9b87f6ed8cd5e79d2adab36 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 27 Mar 2015 15:03:10 -0300 Subject: [PATCH] Admin page for point rules --- controllers/gamification_plugin_admin_controller.rb | 16 ++++++++++++++++ lib/merit/point_rules.rb | 23 +++++++++-------------- test/functional/gamification_plugin_admin_controller_test.rb | 35 +++++++++++++++++++++++++++++++++++ views/gamification_plugin_admin/index.html.erb | 17 +++++++++++++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 controllers/gamification_plugin_admin_controller.rb create mode 100644 test/functional/gamification_plugin_admin_controller_test.rb create mode 100644 views/gamification_plugin_admin/index.html.erb diff --git a/controllers/gamification_plugin_admin_controller.rb b/controllers/gamification_plugin_admin_controller.rb new file mode 100644 index 0000000..4acd339 --- /dev/null +++ b/controllers/gamification_plugin_admin_controller.rb @@ -0,0 +1,16 @@ + +class GamificationPluginAdminController < PluginAdminController + + def index + settings = params[:settings] + settings ||= {} + + @settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, settings) + if request.post? + @settings.save! + session[:notice] = 'Settings succefully saved.' + redirect_to :action => 'index' + end + end + +end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 8b242b4..d940faa 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -17,42 +17,37 @@ module Merit :action => 'comment#create', :undo_action => 'comment#destroy', :to => :author, - :value => 1 + :value => 1, + :default_weight => 10 }, :article_author => { :action => 'article#create', :undo_action => 'article#destroy', :to => :author, - :value => 1 + :value => 1, + :default_weight => 50 }, :vote_voteable_author => { :action => 'vote#create', :undo_action => 'vote#destroy', :to => lambda {|vote| vote.voteable.author}, :profile => lambda {|vote| vote.voteable.profile}, - :value => lambda {|vote| vote.vote} + :value => lambda {|vote| vote.vote}, + :default_weight => 5 }, :vote_voteable => { :action => 'vote#create', :undo_action => 'vote#destroy', :to => lambda {|vote| vote.voteable}, :profile => lambda {|vote| vote.voteable.profile}, - :value => lambda {|vote| vote.vote} + :value => lambda {|vote| vote.vote}, + :default_weight => 5 }, } # FIXME get value from environment def weight(category) - case category - when :comment_author - 10 - when :article_author - 50 - when :vote_voteable - 5 - when :vote_voteable_author - 5 - end + AVAILABLE_RULES[category][:default_weight] end def calculate_score(target, category, value) diff --git a/test/functional/gamification_plugin_admin_controller_test.rb b/test/functional/gamification_plugin_admin_controller_test.rb new file mode 100644 index 0000000..e789c2b --- /dev/null +++ b/test/functional/gamification_plugin_admin_controller_test.rb @@ -0,0 +1,35 @@ +require_relative '../test_helper' + +class GamificationPluginAdminControllerTest < ActionController::TestCase + + def setup + @environment = Environment.default + @person = create_user_with_permission('profile', 'edit_environment_features', Environment.default) + login_as(@person.identifier) + end + + attr_accessor :person, :environment + + should 'save point rules' do + post :index, :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 + Merit::PointRules::AVAILABLE_RULES.each do |category, setting| + assert_select 'input[name=?][value=?]', "settings[point_rules][#{category}[weight]]", setting[:default_weight] + end + end + + should 'load saved weights for point rules' do + settings = Noosfero::Plugin::Settings.new(environment, GamificationPlugin, {}) + settings.set_setting(:point_rules, {'comment_author' => {'weight' => '500'}}) + settings.save! + get :index + assert_select 'input[name=?][value=?]', "settings[point_rules][comment_author[weight]]", 500 + end + +end + diff --git a/views/gamification_plugin_admin/index.html.erb b/views/gamification_plugin_admin/index.html.erb new file mode 100644 index 0000000..51de870 --- /dev/null +++ b/views/gamification_plugin_admin/index.html.erb @@ -0,0 +1,17 @@ +

<%= _('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 %> + + <% button_bar do %> + <%= submit_button(:save, c_('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> + <% end %> + +<% end %> -- libgit2 0.21.2