Commit 77c756f9e1070dee2df13e3553493ec14c2ad2b6

Authored by Hugo Melo
1 parent c7bd59e3

Add remaining files for point categories admin

controllers/gamification_plugin_admin_controller.rb
@@ -3,14 +3,6 @@ class GamificationPluginAdminController < PluginAdminController @@ -3,14 +3,6 @@ class GamificationPluginAdminController < PluginAdminController
3 3
4 before_filter :load_settings 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 def levels 6 def levels
15 if save_settings 7 if save_settings
16 render :file => 'gamification_plugin_admin/index' 8 render :file => 'gamification_plugin_admin/index'
db/migrate/20150930132305_move_points_category_to_categorization_tables.rb
1 class MovePointsCategoryToCategorizationTables < ActiveRecord::Migration 1 class MovePointsCategoryToCategorizationTables < ActiveRecord::Migration
2 def up 2 def up
3 Merit::PointRules::AVAILABLE_RULES.each do |name, setting| 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 env = Environment.default 5 env = Environment.default
6 settings = Noosfero::Plugin::Settings.new(env, GamificationPlugin) 6 settings = Noosfero::Plugin::Settings.new(env, GamificationPlugin)
7 weight = settings.settings.fetch(:point_rules, {}).fetch(name.to_s, {}).fetch('weight', setting[:default_weight]).to_i 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,12 +14,12 @@ class Person
14 end 14 end
15 15
16 def points_by_type type 16 def points_by_type type
17 - categorizations = GamificationPlugin::PointsCategorization.by_type(type) 17 + categorizations = GamificationPlugin::PointsCategorization.for_type(type)
18 categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } 18 categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) }
19 end 19 end
20 20
21 def points_by_profile profile 21 def points_by_profile profile
22 - categorizations = GamificationPlugin::PointsCategorization.by_profile(profile) 22 + categorizations = GamificationPlugin::PointsCategorization.for_profile(profile)
23 categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) } 23 categorizations.inject(0) {|sum, c| sum += self.points(category: c.id.to_s) }
24 end 24 end
25 25
lib/merit/point_rules.rb
@@ -55,7 +55,7 @@ module Merit @@ -55,7 +55,7 @@ module Merit
55 value: 1, 55 value: 1,
56 description: _('Article community'), 56 description: _('Article community'),
57 default_weight: 10, 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 vote_voteable_author: { 60 vote_voteable_author: {
61 action: 'vote#create', 61 action: 'vote#create',
@@ -181,7 +181,7 @@ module Merit @@ -181,7 +181,7 @@ module Merit
181 @environment = environment 181 @environment = environment
182 182
183 AVAILABLE_RULES.each do |point_type, setting| 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 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| 185 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal|
186 score lambda {|target| signal * calculate_score(target, categorization.weight, setting[:value])}, on: action, to: setting[:to], category: categorization.id.to_s do |target| 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 condition(setting, target, categorization.profile) 187 condition(setting, target, categorization.profile)
models/gamification_plugin/points_categorization.rb
1 class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord 1 class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord
2 belongs_to :profile 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 attr_accessible :profile_id, :profile, :point_type_id, :weight 4 attr_accessible :profile_id, :profile, :point_type_id, :weight
5 5
6 validates_presence_of :point_type_id, :weight 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 end 12 end
public/style.css
@@ -219,3 +219,21 @@ @@ -219,3 +219,21 @@
219 left: 0px; 219 left: 0px;
220 margin: 0 -12px; 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 &lt; ActiveSupport::TestCase @@ -47,7 +47,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
47 should 'add merit points to community article owner when an user like it' do 47 should 'add merit points to community article owner when an user like it' do
48 article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) 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 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do 51 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
52 Vote.create!(:voter => person, :voteable => article, :vote => 1) 52 Vote.create!(:voter => person, :voteable => article, :vote => 1)
53 end 53 end
@@ -57,7 +57,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -57,7 +57,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
57 article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) 57 article = create(TextArticle, :name => 'Test', :profile => @community, :author => person)
58 article = article.reload 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 assert_difference 'article.points(:category => c.id.to_s)', c.weight do 61 assert_difference 'article.points(:category => c.id.to_s)', c.weight do
62 Vote.create!(:voter => person, :voteable => article, :vote => 1) 62 Vote.create!(:voter => person, :voteable => article, :vote => 1)
63 end 63 end
@@ -72,7 +72,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -72,7 +72,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
72 should 'add merit points to voter when he likes an article' do 72 should 'add merit points to voter when he likes an article' do
73 article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) 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 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do 76 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
77 Vote.create!(:voter => person, :voteable => article, :vote => 1) 77 Vote.create!(:voter => person, :voteable => article, :vote => 1)
78 end 78 end
@@ -81,7 +81,7 @@ class ArticleTest &lt; ActiveSupport::TestCase @@ -81,7 +81,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
81 should 'add merit points to voter when he dislikes an article' do 81 should 'add merit points to voter when he dislikes an article' do
82 article = create(TextArticle, :name => 'Test', :profile => @community, :author => person) 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 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do 85 assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
86 Vote.create!(:voter => person, :voteable => article, :vote => -1) 86 Vote.create!(:voter => person, :voteable => article, :vote => -1)
87 end 87 end
test/unit/comment_test.rb
@@ -68,7 +68,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -68,7 +68,7 @@ class CommentTest &lt; ActiveSupport::TestCase
68 should 'add merit points to comment owner when an user like his comment' do 68 should 'add merit points to comment owner when an user like his comment' do
69 comment = create(Comment, :source => article, :author_id => person.id) 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 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do 72 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do
73 Vote.create!(:voter => person, :voteable => comment, :vote => 1) 73 Vote.create!(:voter => person, :voteable => comment, :vote => 1)
74 end 74 end
@@ -86,7 +86,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -86,7 +86,7 @@ class CommentTest &lt; ActiveSupport::TestCase
86 should 'subtract merit points from comment owner when an user dislike his comment' do 86 should 'subtract merit points from comment owner when an user dislike his comment' do
87 comment = create(Comment, :source => article, :author_id => person.id) 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 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do 90 assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do
91 Vote.create!(:voter => person, :voteable => comment, :vote => -1) 91 Vote.create!(:voter => person, :voteable => comment, :vote => -1)
92 end 92 end
@@ -110,7 +110,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -110,7 +110,7 @@ class CommentTest &lt; ActiveSupport::TestCase
110 should 'add merit points to voter when he likes a comment' do 110 should 'add merit points to voter when he likes a comment' do
111 comment = create(Comment, :source => article, :author_id => person.id) 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 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do 114 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do
115 Vote.create!(:voter => person, :voteable => comment, :vote => 1) 115 Vote.create!(:voter => person, :voteable => comment, :vote => 1)
116 end 116 end
@@ -119,14 +119,14 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -119,14 +119,14 @@ class CommentTest &lt; ActiveSupport::TestCase
119 should 'add merit points to voter when he dislikes a comment' do 119 should 'add merit points to voter when he dislikes a comment' do
120 comment = create(Comment, :source => article, :author_id => person.id) 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 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do 123 assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do
124 Vote.create!(:voter => person, :voteable => comment, :vote => -1) 124 Vote.create!(:voter => person, :voteable => comment, :vote => -1)
125 end 125 end
126 end 126 end
127 127
128 should 'add merit points to source article when create a comment' do 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 assert_difference 'article.points(:category => c.id.to_s)', c.weight do 130 assert_difference 'article.points(:category => c.id.to_s)', c.weight do
131 create(Comment, :source => article, :author_id => person.id) 131 create(Comment, :source => article, :author_id => person.id)
132 end 132 end
@@ -135,7 +135,7 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -135,7 +135,7 @@ class CommentTest &lt; ActiveSupport::TestCase
135 should 'add merit points to source community when create a comment' do 135 should 'add merit points to source community when create a comment' do
136 article = create(TextileArticle, :profile_id => community.id, :author_id => author.id) 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 assert_difference 'community.points(:category => c.id.to_s)', c.weight do 139 assert_difference 'community.points(:category => c.id.to_s)', c.weight do
140 create(Comment, :source => article, :author_id => person.id) 140 create(Comment, :source => article, :author_id => person.id)
141 end 141 end
test/unit/person_test.rb
@@ -31,7 +31,7 @@ class PersonTest &lt; ActiveSupport::TestCase @@ -31,7 +31,7 @@ class PersonTest &lt; ActiveSupport::TestCase
31 should 'add points when add someone as a friendly' do 31 should 'add points when add someone as a friendly' do
32 other_person = create_user("testuserfriend").person 32 other_person = create_user("testuserfriend").person
33 person.add_friend(other_person) 33 person.add_friend(other_person)
34 - c = GamificationPlugin::PointsCategorization.by_type(:friends).first 34 + c = GamificationPlugin::PointsCategorization.for_type(:friends).first
35 assert_equal 5, person.score_points(:category => c.id.to_s).sum(:num_points) 35 assert_equal 5, person.score_points(:category => c.id.to_s).sum(:num_points)
36 end 36 end
37 37
views/gamification_plugin_admin/points.html.erb
1 <h1><%= _('Gamification Settings: Point Rules')%></h1> 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 <% end %> 11 <% end %>
10 - </div>  
11 - <% end %> 12 + <% end %>
  13 + </div>
12 14
13 <% button_bar do %> 15 <% button_bar do %>
14 <%= submit_button(:save, c_('Save'), :cancel => {:action => 'index'}) %> 16 <%= submit_button(:save, c_('Save'), :cancel => {:action => 'index'}) %>