diff --git a/controllers/gamification_plugin_admin_controller.rb b/controllers/gamification_plugin_admin_controller.rb index d7ce01e..0f1edbf 100644 --- a/controllers/gamification_plugin_admin_controller.rb +++ b/controllers/gamification_plugin_admin_controller.rb @@ -3,14 +3,6 @@ class GamificationPluginAdminController < PluginAdminController before_filter :load_settings - def points - if save_settings - render :file => 'gamification_plugin_admin/index' - else - render :file => 'gamification_plugin_admin/points' - end - end - def levels if save_settings render :file => 'gamification_plugin_admin/index' diff --git a/db/migrate/20150930132305_move_points_category_to_categorization_tables.rb b/db/migrate/20150930132305_move_points_category_to_categorization_tables.rb index a85a730..794969d 100644 --- a/db/migrate/20150930132305_move_points_category_to_categorization_tables.rb +++ b/db/migrate/20150930132305_move_points_category_to_categorization_tables.rb @@ -1,7 +1,7 @@ class MovePointsCategoryToCategorizationTables < ActiveRecord::Migration def up Merit::PointRules::AVAILABLE_RULES.each do |name, setting| - type = GamificationPlugin::PointsType.create(name: name.to_s) + type = GamificationPlugin::PointsType.create(name: name.to_s, description: setting[:description]) env = Environment.default settings = Noosfero::Plugin::Settings.new(env, GamificationPlugin) weight = settings.settings.fetch(:point_rules, {}).fetch(name.to_s, {}).fetch('weight', setting[:default_weight]).to_i diff --git a/lib/ext/person.rb b/lib/ext/person.rb index f246da2..c2144d4 100644 --- a/lib/ext/person.rb +++ b/lib/ext/person.rb @@ -14,12 +14,12 @@ class Person end def points_by_type type - categorizations = GamificationPlugin::PointsCategorization.by_type(type) + categorizations = GamificationPlugin::PointsCategorization.for_type(type) categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } end def points_by_profile profile - categorizations = GamificationPlugin::PointsCategorization.by_profile(profile) + categorizations = GamificationPlugin::PointsCategorization.for_profile(profile) categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 8db691d..e454380 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -55,7 +55,7 @@ module Merit value: 1, description: _('Article community'), default_weight: 10, - condition: lambda {|article, profile| article.profile.community? and article.profile == profile } + condition: lambda {|article, profile| article.profile.present? and article.profile.community? and article.profile == profile } }, vote_voteable_author: { action: 'vote#create', @@ -181,7 +181,7 @@ module Merit @environment = environment AVAILABLE_RULES.each do |point_type, setting| - GamificationPlugin::PointsCategorization.by_type(point_type).includes(:profile).each do |categorization| + GamificationPlugin::PointsCategorization.for_type(point_type).includes(:profile).each do |categorization| [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| score lambda {|target| signal * calculate_score(target, categorization.weight, setting[:value])}, on: action, to: setting[:to], category: categorization.id.to_s do |target| condition(setting, target, categorization.profile) diff --git a/models/gamification_plugin/points_categorization.rb b/models/gamification_plugin/points_categorization.rb index 2963296..a97add2 100644 --- a/models/gamification_plugin/points_categorization.rb +++ b/models/gamification_plugin/points_categorization.rb @@ -1,10 +1,12 @@ class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord belongs_to :profile - belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id, dependent: :destroy + belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id attr_accessible :profile_id, :profile, :point_type_id, :weight validates_presence_of :point_type_id, :weight - scope :by_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } - scope :by_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } + scope :for_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } + scope :for_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } + + scope :grouped_profiles, select(:profile_id).group(:profile_id).includes(:profile) end diff --git a/public/style.css b/public/style.css index fc4fc63..f0d03f0 100644 --- a/public/style.css +++ b/public/style.css @@ -219,3 +219,21 @@ left: 0px; margin: 0 -12px; } + +/* *** *** ADMIN *** *** */ +.gamification_plugin_activate_profile label { + display: inline-block; + font-weight: bold; + margin-right: 10px; +} + +.gamification_plugin_activate_profile .formfield, .gamification_plugin_activate_profile .formfieldline { + display: inline-block; + margin-top: 10px; + margin-bottom: 10px; +} + +.gamification_plugin_admin_points .point-rules .formfieldline label { + float: left; + width: 180px; +} diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 598833a..9b5e052 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -47,7 +47,7 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit points to community article owner when an user like it' do article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: @community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: @community.id).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) end @@ -57,7 +57,7 @@ class ArticleTest < ActiveSupport::TestCase article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) article = article.reload - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable).where(profile_id: @community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).where(profile_id: @community.id).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) end @@ -72,7 +72,7 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit points to voter when he likes an article' do article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: @community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) end @@ -81,7 +81,7 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit points to voter when he dislikes an article' do article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: @community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => -1) end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 9872bf4..3d68a19 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -68,7 +68,7 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to comment owner when an user like his comment' do comment = create(Comment, :source => article, :author_id => person.id) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: article.profile.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) end @@ -86,7 +86,7 @@ class CommentTest < ActiveSupport::TestCase should 'subtract merit points from comment owner when an user dislike his comment' do comment = create(Comment, :source => article, :author_id => person.id) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: article.profile.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) end @@ -110,7 +110,7 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to voter when he likes a comment' do comment = create(Comment, :source => article, :author_id => person.id) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: community.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) end @@ -119,14 +119,14 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to voter when he dislikes a comment' do comment = create(Comment, :source => article, :author_id => person.id) - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: community.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) end end should 'add merit points to source article when create a comment' do - c = GamificationPlugin::PointsCategorization.by_type(:comment_article).where(profile_id: community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:comment_article).where(profile_id: community.id).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) end @@ -135,7 +135,7 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to source community when create a comment' do article = create(TextileArticle, :profile_id => community.id, :author_id => author.id) - c = GamificationPlugin::PointsCategorization.by_type(:comment_community).where(profile_id: community.id).first + c = GamificationPlugin::PointsCategorization.for_type(:comment_community).where(profile_id: community.id).first assert_difference 'community.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index a149bf3..b865ad1 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -31,7 +31,7 @@ class PersonTest < ActiveSupport::TestCase should 'add points when add someone as a friendly' do other_person = create_user("testuserfriend").person person.add_friend(other_person) - c = GamificationPlugin::PointsCategorization.by_type(:friends).first + c = GamificationPlugin::PointsCategorization.for_type(:friends).first assert_equal 5, person.score_points(:category => c.id.to_s).sum(:num_points) end diff --git a/views/gamification_plugin_admin/points.html.erb b/views/gamification_plugin_admin/points.html.erb index ff541be..c404f3a 100644 --- a/views/gamification_plugin_admin/points.html.erb +++ b/views/gamification_plugin_admin/points.html.erb @@ -1,14 +1,16 @@

<%= _('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]))) %> +<%= form_for('points_categorizations') do |f| %> +
+

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

+ <% GamificationPlugin::PointsCategorization.grouped_profiles.each do |categorization| %> + <% title = categorization.profile.nil? ? _('general points') : _('points for %{name}') % {name: categorization.profile.name} %> +

<%= title %>

+ <% GamificationPlugin::PointsCategorization.where(profile_id: categorization.profile).each do |c| %> + <%= labelled_form_field(_(c.point_type.description), f.text_field("[][weight]", :value => c.weight)) %> <% end %> -
- <% end %> + <% end %> +
<% button_bar do %> <%= submit_button(:save, c_('Save'), :cancel => {:action => 'index'}) %> -- libgit2 0.21.2