Commit 4cd649e7d1d25f81711a5f2834768ebac906e940
1 parent
bc5f17aa
Exists in
master
and in
1 other branch
Remove badges when destroy a badge
Showing
5 changed files
with
52 additions
and
2 deletions
Show diff stats
lib/gamification_plugin.rb
| @@ -29,6 +29,10 @@ class GamificationPlugin < Noosfero::Plugin | @@ -29,6 +29,10 @@ class GamificationPlugin < Noosfero::Plugin | ||
| 29 | }] | 29 | }] |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | + def body_ending | ||
| 33 | + proc { render :file => 'gamification/display_achievements' } | ||
| 34 | + end | ||
| 35 | + | ||
| 32 | ActionDispatch::Reloader.to_prepare do | 36 | ActionDispatch::Reloader.to_prepare do |
| 33 | Merit.setup do |config| | 37 | Merit.setup do |config| |
| 34 | config.checks_on_each_request = false | 38 | config.checks_on_each_request = false |
lib/gamification_plugin/badge.rb
| @@ -10,4 +10,10 @@ class GamificationPlugin::Badge < Noosfero::Plugin::ActiveRecord | @@ -10,4 +10,10 @@ class GamificationPlugin::Badge < Noosfero::Plugin::ActiveRecord | ||
| 10 | (custom_fields || {}).fetch(:threshold, '') | 10 | (custom_fields || {}).fetch(:threshold, '') |
| 11 | end | 11 | end |
| 12 | 12 | ||
| 13 | + before_destroy :remove_badges | ||
| 14 | + | ||
| 15 | + def remove_badges | ||
| 16 | + Merit::BadgesSash.where(:badge_id => self.id).destroy_all | ||
| 17 | + end | ||
| 18 | + | ||
| 13 | end | 19 | end |
lib/merit_badge.rb
| @@ -5,8 +5,10 @@ module Merit | @@ -5,8 +5,10 @@ module Merit | ||
| 5 | # Delegate find methods to GamificationPlugin::Badge | 5 | # Delegate find methods to GamificationPlugin::Badge |
| 6 | class Badge | 6 | class Badge |
| 7 | class << self | 7 | class << self |
| 8 | - [:find_by_name_and_level, :find].each do |method| | ||
| 9 | - delegate method, :to => 'GamificationPlugin::Badge' | 8 | + delegate :find_by_name_and_level, :to => 'GamificationPlugin::Badge' |
| 9 | + | ||
| 10 | + def find(id) | ||
| 11 | + GamificationPlugin::Badge.find_by_id(id) | ||
| 10 | end | 12 | end |
| 11 | end | 13 | end |
| 12 | end | 14 | end |
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +require_relative "../test_helper" | ||
| 2 | + | ||
| 3 | +class BadgeTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + @person = create_user('testuser').person | ||
| 7 | + @environment = Environment.default | ||
| 8 | + end | ||
| 9 | + | ||
| 10 | + attr_accessor :person, :environment | ||
| 11 | + | ||
| 12 | + should 'add badge to person' do | ||
| 13 | + badge = GamificationPlugin::Badge.create!(:owner => environment) | ||
| 14 | + person.add_badge(badge.id) | ||
| 15 | + assert_equal [badge], person.badges | ||
| 16 | + end | ||
| 17 | + | ||
| 18 | + should 'remove badge from person when destroy a badge' do | ||
| 19 | + badge = GamificationPlugin::Badge.create!(:owner => environment) | ||
| 20 | + person.add_badge(badge.id) | ||
| 21 | + assert_equal [badge], person.badges | ||
| 22 | + badge.destroy | ||
| 23 | + assert_equal [], person.reload.badges | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + should 'not fail when a person has an undefined badge' do | ||
| 27 | + person.add_badge(1235) | ||
| 28 | + assert_equal [], person.reload.badges.compact | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | +end |