Commit e01af1ea8b363866fb976ec0aee5fc27c749b1f5
1 parent
ab14f92a
Exists in
master
and in
1 other branch
Add owner field in badge form
Showing
6 changed files
with
61 additions
and
4 deletions
Show diff stats
controllers/admin/gamification_plugin_badges_controller.rb
| @@ -17,8 +17,13 @@ class GamificationPluginBadgesController < PluginAdminController | @@ -17,8 +17,13 @@ class GamificationPluginBadgesController < PluginAdminController | ||
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | def create | 19 | def create |
| 20 | + owner_id = params[:gamification_plugin_badge].delete(:owner_id) | ||
| 20 | @gamification_plugin_badge = GamificationPlugin::Badge.new(params[:gamification_plugin_badge]) | 21 | @gamification_plugin_badge = GamificationPlugin::Badge.new(params[:gamification_plugin_badge]) |
| 21 | - @gamification_plugin_badge.owner = environment | 22 | + if owner_id.present? |
| 23 | + @gamification_plugin_badge.owner = environment.organizations.find(owner_id) | ||
| 24 | + else | ||
| 25 | + @gamification_plugin_badge.owner = environment | ||
| 26 | + end | ||
| 22 | 27 | ||
| 23 | if @gamification_plugin_badge.save | 28 | if @gamification_plugin_badge.save |
| 24 | session[:notice] = _('Badge was successfully created.') | 29 | session[:notice] = _('Badge was successfully created.') |
| @@ -31,6 +36,14 @@ class GamificationPluginBadgesController < PluginAdminController | @@ -31,6 +36,14 @@ class GamificationPluginBadgesController < PluginAdminController | ||
| 31 | def update | 36 | def update |
| 32 | @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id]) | 37 | @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id]) |
| 33 | 38 | ||
| 39 | + # FIXME avoid code duplication | ||
| 40 | + owner_id = params[:gamification_plugin_badge].delete(:owner_id) | ||
| 41 | + if owner_id.present? | ||
| 42 | + @gamification_plugin_badge.owner = environment.organizations.find(owner_id) | ||
| 43 | + else | ||
| 44 | + @gamification_plugin_badge.owner = environment | ||
| 45 | + end | ||
| 46 | + | ||
| 34 | if @gamification_plugin_badge.update_attributes(params[:gamification_plugin_badge]) | 47 | if @gamification_plugin_badge.update_attributes(params[:gamification_plugin_badge]) |
| 35 | session[:notice] = _('Badge was successfully updated.') | 48 | session[:notice] = _('Badge was successfully updated.') |
| 36 | redirect_to :action => :index | 49 | redirect_to :action => :index |
| @@ -39,6 +52,10 @@ class GamificationPluginBadgesController < PluginAdminController | @@ -39,6 +52,10 @@ class GamificationPluginBadgesController < PluginAdminController | ||
| 39 | end | 52 | end |
| 40 | end | 53 | end |
| 41 | 54 | ||
| 55 | + def search_owners | ||
| 56 | + render :text => prepare_to_token_input(environment.organizations).to_json | ||
| 57 | + end | ||
| 58 | + | ||
| 42 | def destroy | 59 | def destroy |
| 43 | @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id]) | 60 | @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id]) |
| 44 | @gamification_plugin_badge.destroy | 61 | @gamification_plugin_badge.destroy |
lib/ext/environment.rb
| @@ -2,7 +2,11 @@ require_dependency 'environment' | @@ -2,7 +2,11 @@ require_dependency 'environment' | ||
| 2 | 2 | ||
| 3 | class Environment | 3 | class Environment |
| 4 | 4 | ||
| 5 | - has_many :gamification_plugin_badges, :class_name => 'GamificationPlugin::Badge', :foreign_key => 'owner_id', :source => :owner | 5 | + has_many :gamification_plugin_environment_badges, :class_name => 'GamificationPlugin::Badge', :foreign_key => 'owner_id', :source => :owner |
| 6 | has_many :gamification_plugin_organization_badges, :through => :organizations | 6 | has_many :gamification_plugin_organization_badges, :through => :organizations |
| 7 | 7 | ||
| 8 | + def gamification_plugin_badges | ||
| 9 | + GamificationPlugin::Badge.from("#{gamification_plugin_organization_badges.union(gamification_plugin_environment_badges).to_sql} as #{GamificationPlugin::Badge.table_name}") | ||
| 10 | + end | ||
| 11 | + | ||
| 8 | end | 12 | end |
lib/merit/badge_rules.rb
| @@ -173,8 +173,7 @@ module Merit | @@ -173,8 +173,7 @@ module Merit | ||
| 173 | rules = AVAILABLE_RULES | 173 | rules = AVAILABLE_RULES |
| 174 | rules.merge! CONFERENCE_RULES if defined? CONFERENCE_RULES | 174 | rules.merge! CONFERENCE_RULES if defined? CONFERENCE_RULES |
| 175 | 175 | ||
| 176 | - gamification_plugin_badges = environment.gamification_plugin_badges + environment.gamification_plugin_organization_badges | ||
| 177 | - gamification_plugin_badges.each do |badge| | 176 | + environment.gamification_plugin_badges.each do |badge| |
| 178 | next if rules[badge.name.to_sym].nil? | 177 | next if rules[badge.name.to_sym].nil? |
| 179 | rules[badge.name.to_sym].each do |setting| | 178 | rules[badge.name.to_sym].each do |setting| |
| 180 | options = {badge: badge.name, level: badge.level, to: setting[:to]} | 179 | options = {badge: badge.name, level: badge.level, to: setting[:to]} |
test/functional/gamification_plugin_badges_controller_test.rb
| @@ -25,6 +25,14 @@ class GamificationPluginBadgesControllerTest < ActionController::TestCase | @@ -25,6 +25,14 @@ class GamificationPluginBadgesControllerTest < ActionController::TestCase | ||
| 25 | end | 25 | end |
| 26 | end | 26 | end |
| 27 | 27 | ||
| 28 | + should "should create gamification_plugin_badge with organization as owner" do | ||
| 29 | + organization = fast_create(Organization) | ||
| 30 | + assert_difference('GamificationPlugin::Badge.count') do | ||
| 31 | + post :create, gamification_plugin_badge: { description: @gamification_plugin_badge.description, level: @gamification_plugin_badge.level, name: @gamification_plugin_badge.name, custom_fields: {threshold: @gamification_plugin_badge.threshold}, owner_id: organization.id } | ||
| 32 | + assert_equal organization, GamificationPlugin::Badge.last.owner | ||
| 33 | + end | ||
| 34 | + end | ||
| 35 | + | ||
| 28 | should "should show gamification_plugin_badge" do | 36 | should "should show gamification_plugin_badge" do |
| 29 | get :show, id: @gamification_plugin_badge | 37 | get :show, id: @gamification_plugin_badge |
| 30 | assert_response :success | 38 | assert_response :success |
| @@ -40,6 +48,23 @@ class GamificationPluginBadgesControllerTest < ActionController::TestCase | @@ -40,6 +48,23 @@ class GamificationPluginBadgesControllerTest < ActionController::TestCase | ||
| 40 | assert assigns(:gamification_plugin_badge) | 48 | assert assigns(:gamification_plugin_badge) |
| 41 | end | 49 | end |
| 42 | 50 | ||
| 51 | + should "should change badge owner" do | ||
| 52 | + organization = fast_create(Organization) | ||
| 53 | + put :update, id: @gamification_plugin_badge, gamification_plugin_badge: { description: @gamification_plugin_badge.description, level: @gamification_plugin_badge.level, name: @gamification_plugin_badge.name, custom_fields: {threshold: @gamification_plugin_badge.threshold}, owner_id: organization.id } | ||
| 54 | + assert assigns(:gamification_plugin_badge) | ||
| 55 | + assert_equal organization, @gamification_plugin_badge.reload.owner | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + should "should keep badge owner when update" do | ||
| 59 | + organization = fast_create(Organization) | ||
| 60 | + @gamification_plugin_badge.owner = organization | ||
| 61 | + @gamification_plugin_badge.save! | ||
| 62 | + | ||
| 63 | + put :update, id: @gamification_plugin_badge, gamification_plugin_badge: { description: @gamification_plugin_badge.description, level: @gamification_plugin_badge.level, name: @gamification_plugin_badge.name, custom_fields: {threshold: @gamification_plugin_badge.threshold}, owner_id: organization.id } | ||
| 64 | + assert assigns(:gamification_plugin_badge) | ||
| 65 | + assert_equal organization, @gamification_plugin_badge.reload.owner | ||
| 66 | + end | ||
| 67 | + | ||
| 43 | should "should destroy gamification_plugin_badge" do | 68 | should "should destroy gamification_plugin_badge" do |
| 44 | assert_difference('GamificationPlugin::Badge.count', -1) do | 69 | assert_difference('GamificationPlugin::Badge.count', -1) do |
| 45 | delete :destroy, id: @gamification_plugin_badge | 70 | delete :destroy, id: @gamification_plugin_badge |
views/gamification_plugin_badges/_form.html.erb
| @@ -29,6 +29,11 @@ | @@ -29,6 +29,11 @@ | ||
| 29 | <%= f.label :level %><br /> | 29 | <%= f.label :level %><br /> |
| 30 | <%= f.text_field :level %> | 30 | <%= f.text_field :level %> |
| 31 | </div> | 31 | </div> |
| 32 | + <div class="field"> | ||
| 33 | + <%= f.label :profile_owner %><br /> | ||
| 34 | + <% tokenized_owner = @gamification_plugin_badge.owner.present? && @gamification_plugin_badge.owner.kind_of?(Organization) ? prepare_to_token_input([@gamification_plugin_badge.owner]) : nil %> | ||
| 35 | + <%= token_input_field_tag('gamification_plugin_badge[owner_id]', 'badge-owner', {:action => 'search_owners'}, {:focus => false, :hint_text => _('Choose a profile or leave it blank for a global badge'), :token_limit => 1, :pre_populate => tokenized_owner}) %> | ||
| 36 | + </div> | ||
| 32 | <h4><%= _('Actions') %></h4> | 37 | <h4><%= _('Actions') %></h4> |
| 33 | <%= f.fields_for :custom_fields do |c| %> | 38 | <%= f.fields_for :custom_fields do |c| %> |
| 34 | <% rules.each do |name, settings| %> | 39 | <% rules.each do |name, settings| %> |
views/gamification_plugin_badges/show.html.erb
| @@ -22,6 +22,13 @@ | @@ -22,6 +22,13 @@ | ||
| 22 | <%= @gamification_plugin_badge.level %> | 22 | <%= @gamification_plugin_badge.level %> |
| 23 | </p> | 23 | </p> |
| 24 | 24 | ||
| 25 | +<% if @gamification_plugin_badge.owner.kind_of?(Organization) %> | ||
| 26 | +<p> | ||
| 27 | + <b><%= _('Profile owner:') %></b> | ||
| 28 | + <%= @gamification_plugin_badge.owner.name %> | ||
| 29 | + </p> | ||
| 30 | +<% end %> | ||
| 31 | + | ||
| 25 | <p> | 32 | <p> |
| 26 | <b>Threshold:</b> | 33 | <b>Threshold:</b> |
| 27 | <% if @gamification_plugin_badge.custom_fields.is_a? Hash %> | 34 | <% if @gamification_plugin_badge.custom_fields.is_a? Hash %> |