Commit 77c756f9e1070dee2df13e3553493ec14c2ad2b6
1 parent
c7bd59e3
Exists in
master
and in
1 other branch
Add remaining files for point categories admin
Showing
10 changed files
with
49 additions
and
35 deletions
Show diff stats
controllers/gamification_plugin_admin_controller.rb
| ... | ... | @@ -3,14 +3,6 @@ class GamificationPluginAdminController < PluginAdminController |
| 3 | 3 | |
| 4 | 4 | before_filter :load_settings |
| 5 | 5 | |
| 6 | - def points | |
| 7 | - if save_settings | |
| 8 | - render :file => 'gamification_plugin_admin/index' | |
| 9 | - else | |
| 10 | - render :file => 'gamification_plugin_admin/points' | |
| 11 | - end | |
| 12 | - end | |
| 13 | - | |
| 14 | 6 | def levels |
| 15 | 7 | if save_settings |
| 16 | 8 | render :file => 'gamification_plugin_admin/index' | ... | ... |
db/migrate/20150930132305_move_points_category_to_categorization_tables.rb
| 1 | 1 | class MovePointsCategoryToCategorizationTables < ActiveRecord::Migration |
| 2 | 2 | def up |
| 3 | 3 | Merit::PointRules::AVAILABLE_RULES.each do |name, setting| |
| 4 | - type = GamificationPlugin::PointsType.create(name: name.to_s) | |
| 4 | + type = GamificationPlugin::PointsType.create(name: name.to_s, description: setting[:description]) | |
| 5 | 5 | env = Environment.default |
| 6 | 6 | settings = Noosfero::Plugin::Settings.new(env, GamificationPlugin) |
| 7 | 7 | weight = settings.settings.fetch(:point_rules, {}).fetch(name.to_s, {}).fetch('weight', setting[:default_weight]).to_i | ... | ... |
lib/ext/person.rb
| ... | ... | @@ -14,12 +14,12 @@ class Person |
| 14 | 14 | end |
| 15 | 15 | |
| 16 | 16 | def points_by_type type |
| 17 | - categorizations = GamificationPlugin::PointsCategorization.by_type(type) | |
| 17 | + categorizations = GamificationPlugin::PointsCategorization.for_type(type) | |
| 18 | 18 | categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | 21 | def points_by_profile profile |
| 22 | - categorizations = GamificationPlugin::PointsCategorization.by_profile(profile) | |
| 22 | + categorizations = GamificationPlugin::PointsCategorization.for_profile(profile) | |
| 23 | 23 | categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } |
| 24 | 24 | end |
| 25 | 25 | ... | ... |
lib/merit/point_rules.rb
| ... | ... | @@ -55,7 +55,7 @@ module Merit |
| 55 | 55 | value: 1, |
| 56 | 56 | description: _('Article community'), |
| 57 | 57 | default_weight: 10, |
| 58 | - condition: lambda {|article, profile| article.profile.community? and article.profile == profile } | |
| 58 | + condition: lambda {|article, profile| article.profile.present? and article.profile.community? and article.profile == profile } | |
| 59 | 59 | }, |
| 60 | 60 | vote_voteable_author: { |
| 61 | 61 | action: 'vote#create', |
| ... | ... | @@ -181,7 +181,7 @@ module Merit |
| 181 | 181 | @environment = environment |
| 182 | 182 | |
| 183 | 183 | AVAILABLE_RULES.each do |point_type, setting| |
| 184 | - GamificationPlugin::PointsCategorization.by_type(point_type).includes(:profile).each do |categorization| | |
| 184 | + GamificationPlugin::PointsCategorization.for_type(point_type).includes(:profile).each do |categorization| | |
| 185 | 185 | [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| |
| 186 | 186 | score lambda {|target| signal * calculate_score(target, categorization.weight, setting[:value])}, on: action, to: setting[:to], category: categorization.id.to_s do |target| |
| 187 | 187 | condition(setting, target, categorization.profile) | ... | ... |
models/gamification_plugin/points_categorization.rb
| 1 | 1 | class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord |
| 2 | 2 | belongs_to :profile |
| 3 | - belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id, dependent: :destroy | |
| 3 | + belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id | |
| 4 | 4 | attr_accessible :profile_id, :profile, :point_type_id, :weight |
| 5 | 5 | |
| 6 | 6 | validates_presence_of :point_type_id, :weight |
| 7 | 7 | |
| 8 | - scope :by_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } | |
| 9 | - scope :by_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } | |
| 8 | + scope :for_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } | |
| 9 | + scope :for_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } | |
| 10 | + | |
| 11 | + scope :grouped_profiles, select(:profile_id).group(:profile_id).includes(:profile) | |
| 10 | 12 | end | ... | ... |
public/style.css
| ... | ... | @@ -219,3 +219,21 @@ |
| 219 | 219 | left: 0px; |
| 220 | 220 | margin: 0 -12px; |
| 221 | 221 | } |
| 222 | + | |
| 223 | +/* *** *** ADMIN *** *** */ | |
| 224 | +.gamification_plugin_activate_profile label { | |
| 225 | + display: inline-block; | |
| 226 | + font-weight: bold; | |
| 227 | + margin-right: 10px; | |
| 228 | +} | |
| 229 | + | |
| 230 | +.gamification_plugin_activate_profile .formfield, .gamification_plugin_activate_profile .formfieldline { | |
| 231 | + display: inline-block; | |
| 232 | + margin-top: 10px; | |
| 233 | + margin-bottom: 10px; | |
| 234 | +} | |
| 235 | + | |
| 236 | +.gamification_plugin_admin_points .point-rules .formfieldline label { | |
| 237 | + float: left; | |
| 238 | + width: 180px; | |
| 239 | +} | ... | ... |
test/unit/article_test.rb
| ... | ... | @@ -47,7 +47,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 47 | 47 | should 'add merit points to community article owner when an user like it' do |
| 48 | 48 | article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) |
| 49 | 49 | |
| 50 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: @community.id).first | |
| 50 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: @community.id).first | |
| 51 | 51 | assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do |
| 52 | 52 | Vote.create!(:voter => person, :voteable => article, :vote => 1) |
| 53 | 53 | end |
| ... | ... | @@ -57,7 +57,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 57 | 57 | article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) |
| 58 | 58 | article = article.reload |
| 59 | 59 | |
| 60 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable).where(profile_id: @community.id).first | |
| 60 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).where(profile_id: @community.id).first | |
| 61 | 61 | assert_difference 'article.points(:category => c.id.to_s)', c.weight do |
| 62 | 62 | Vote.create!(:voter => person, :voteable => article, :vote => 1) |
| 63 | 63 | end |
| ... | ... | @@ -72,7 +72,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 72 | 72 | should 'add merit points to voter when he likes an article' do |
| 73 | 73 | article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) |
| 74 | 74 | |
| 75 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: @community.id).first | |
| 75 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first | |
| 76 | 76 | assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do |
| 77 | 77 | Vote.create!(:voter => person, :voteable => article, :vote => 1) |
| 78 | 78 | end |
| ... | ... | @@ -81,7 +81,7 @@ class ArticleTest < ActiveSupport::TestCase |
| 81 | 81 | should 'add merit points to voter when he dislikes an article' do |
| 82 | 82 | article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) |
| 83 | 83 | |
| 84 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: @community.id).first | |
| 84 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first | |
| 85 | 85 | assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do |
| 86 | 86 | Vote.create!(:voter => person, :voteable => article, :vote => -1) |
| 87 | 87 | end | ... | ... |
test/unit/comment_test.rb
| ... | ... | @@ -68,7 +68,7 @@ class CommentTest < ActiveSupport::TestCase |
| 68 | 68 | should 'add merit points to comment owner when an user like his comment' do |
| 69 | 69 | comment = create(Comment, :source => article, :author_id => person.id) |
| 70 | 70 | |
| 71 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: article.profile.id).first | |
| 71 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first | |
| 72 | 72 | assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do |
| 73 | 73 | Vote.create!(:voter => person, :voteable => comment, :vote => 1) |
| 74 | 74 | end |
| ... | ... | @@ -86,7 +86,7 @@ class CommentTest < ActiveSupport::TestCase |
| 86 | 86 | should 'subtract merit points from comment owner when an user dislike his comment' do |
| 87 | 87 | comment = create(Comment, :source => article, :author_id => person.id) |
| 88 | 88 | |
| 89 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voteable_author).where(profile_id: article.profile.id).first | |
| 89 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first | |
| 90 | 90 | assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do |
| 91 | 91 | Vote.create!(:voter => person, :voteable => comment, :vote => -1) |
| 92 | 92 | end |
| ... | ... | @@ -110,7 +110,7 @@ class CommentTest < ActiveSupport::TestCase |
| 110 | 110 | should 'add merit points to voter when he likes a comment' do |
| 111 | 111 | comment = create(Comment, :source => article, :author_id => person.id) |
| 112 | 112 | |
| 113 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: community.id).first | |
| 113 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: community.id).first | |
| 114 | 114 | assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do |
| 115 | 115 | Vote.create!(:voter => person, :voteable => comment, :vote => 1) |
| 116 | 116 | end |
| ... | ... | @@ -119,14 +119,14 @@ class CommentTest < ActiveSupport::TestCase |
| 119 | 119 | should 'add merit points to voter when he dislikes a comment' do |
| 120 | 120 | comment = create(Comment, :source => article, :author_id => person.id) |
| 121 | 121 | |
| 122 | - c = GamificationPlugin::PointsCategorization.by_type(:vote_voter).where(profile_id: community.id).first | |
| 122 | + c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: community.id).first | |
| 123 | 123 | assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do |
| 124 | 124 | Vote.create!(:voter => person, :voteable => comment, :vote => -1) |
| 125 | 125 | end |
| 126 | 126 | end |
| 127 | 127 | |
| 128 | 128 | should 'add merit points to source article when create a comment' do |
| 129 | - c = GamificationPlugin::PointsCategorization.by_type(:comment_article).where(profile_id: community.id).first | |
| 129 | + c = GamificationPlugin::PointsCategorization.for_type(:comment_article).where(profile_id: community.id).first | |
| 130 | 130 | assert_difference 'article.points(:category => c.id.to_s)', c.weight do |
| 131 | 131 | create(Comment, :source => article, :author_id => person.id) |
| 132 | 132 | end |
| ... | ... | @@ -135,7 +135,7 @@ class CommentTest < ActiveSupport::TestCase |
| 135 | 135 | should 'add merit points to source community when create a comment' do |
| 136 | 136 | article = create(TextileArticle, :profile_id => community.id, :author_id => author.id) |
| 137 | 137 | |
| 138 | - c = GamificationPlugin::PointsCategorization.by_type(:comment_community).where(profile_id: community.id).first | |
| 138 | + c = GamificationPlugin::PointsCategorization.for_type(:comment_community).where(profile_id: community.id).first | |
| 139 | 139 | assert_difference 'community.points(:category => c.id.to_s)', c.weight do |
| 140 | 140 | create(Comment, :source => article, :author_id => person.id) |
| 141 | 141 | end | ... | ... |
test/unit/person_test.rb
| ... | ... | @@ -31,7 +31,7 @@ class PersonTest < ActiveSupport::TestCase |
| 31 | 31 | should 'add points when add someone as a friendly' do |
| 32 | 32 | other_person = create_user("testuserfriend").person |
| 33 | 33 | person.add_friend(other_person) |
| 34 | - c = GamificationPlugin::PointsCategorization.by_type(:friends).first | |
| 34 | + c = GamificationPlugin::PointsCategorization.for_type(:friends).first | |
| 35 | 35 | assert_equal 5, person.score_points(:category => c.id.to_s).sum(:num_points) |
| 36 | 36 | end |
| 37 | 37 | ... | ... |
views/gamification_plugin_admin/points.html.erb
| 1 | 1 | <h1><%= _('Gamification Settings: Point Rules')%></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(_(setting[:description]), p.text_field("#{category}[weight]", :value => @settings.settings.fetch(:point_rules, {}).fetch(category.to_s, {}).fetch('weight', setting[:default_weight]))) %> | |
| 3 | +<%= form_for('points_categorizations') do |f| %> | |
| 4 | + <div class="point-rules"> | |
| 5 | + <h3><%= _('Point Rules') %></h3> | |
| 6 | + <% GamificationPlugin::PointsCategorization.grouped_profiles.each do |categorization| %> | |
| 7 | + <% title = categorization.profile.nil? ? _('general points') : _('points for %{name}') % {name: categorization.profile.name} %> | |
| 8 | + <h4><%= title %></h4> | |
| 9 | + <% GamificationPlugin::PointsCategorization.where(profile_id: categorization.profile).each do |c| %> | |
| 10 | + <%= labelled_form_field(_(c.point_type.description), f.text_field("[][weight]", :value => c.weight)) %> | |
| 9 | 11 | <% end %> |
| 10 | - </div> | |
| 11 | - <% end %> | |
| 12 | + <% end %> | |
| 13 | + </div> | |
| 12 | 14 | |
| 13 | 15 | <% button_bar do %> |
| 14 | 16 | <%= submit_button(:save, c_('Save'), :cancel => {:action => 'index'}) %> | ... | ... |