From 4cd649e7d1d25f81711a5f2834768ebac906e940 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 1 Apr 2015 11:07:30 -0300 Subject: [PATCH] Remove badges when destroy a badge --- lib/gamification_plugin.rb | 4 ++++ lib/gamification_plugin/badge.rb | 6 ++++++ lib/merit_badge.rb | 6 ++++-- test/unit/badge_test.rb | 31 +++++++++++++++++++++++++++++++ views/gamification/display_achievements.html.erb | 7 +++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/unit/badge_test.rb create mode 100644 views/gamification/display_achievements.html.erb diff --git a/lib/gamification_plugin.rb b/lib/gamification_plugin.rb index f187f42..d0e22e8 100644 --- a/lib/gamification_plugin.rb +++ b/lib/gamification_plugin.rb @@ -29,6 +29,10 @@ class GamificationPlugin < Noosfero::Plugin }] end + def body_ending + proc { render :file => 'gamification/display_achievements' } + end + ActionDispatch::Reloader.to_prepare do Merit.setup do |config| config.checks_on_each_request = false diff --git a/lib/gamification_plugin/badge.rb b/lib/gamification_plugin/badge.rb index 3da5113..26d2758 100644 --- a/lib/gamification_plugin/badge.rb +++ b/lib/gamification_plugin/badge.rb @@ -10,4 +10,10 @@ class GamificationPlugin::Badge < Noosfero::Plugin::ActiveRecord (custom_fields || {}).fetch(:threshold, '') end + before_destroy :remove_badges + + def remove_badges + Merit::BadgesSash.where(:badge_id => self.id).destroy_all + end + end diff --git a/lib/merit_badge.rb b/lib/merit_badge.rb index 32384ee..02251c9 100644 --- a/lib/merit_badge.rb +++ b/lib/merit_badge.rb @@ -5,8 +5,10 @@ module Merit # Delegate find methods to GamificationPlugin::Badge class Badge class << self - [:find_by_name_and_level, :find].each do |method| - delegate method, :to => 'GamificationPlugin::Badge' + delegate :find_by_name_and_level, :to => 'GamificationPlugin::Badge' + + def find(id) + GamificationPlugin::Badge.find_by_id(id) end end end diff --git a/test/unit/badge_test.rb b/test/unit/badge_test.rb new file mode 100644 index 0000000..4bd17d3 --- /dev/null +++ b/test/unit/badge_test.rb @@ -0,0 +1,31 @@ +require_relative "../test_helper" + +class BadgeTest < ActiveSupport::TestCase + + def setup + @person = create_user('testuser').person + @environment = Environment.default + end + + attr_accessor :person, :environment + + should 'add badge to person' do + badge = GamificationPlugin::Badge.create!(:owner => environment) + person.add_badge(badge.id) + assert_equal [badge], person.badges + end + + should 'remove badge from person when destroy a badge' do + badge = GamificationPlugin::Badge.create!(:owner => environment) + person.add_badge(badge.id) + assert_equal [badge], person.badges + badge.destroy + assert_equal [], person.reload.badges + end + + should 'not fail when a person has an undefined badge' do + person.add_badge(1235) + assert_equal [], person.reload.badges.compact + end + +end diff --git a/views/gamification/display_achievements.html.erb b/views/gamification/display_achievements.html.erb new file mode 100644 index 0000000..e44699b --- /dev/null +++ b/views/gamification/display_achievements.html.erb @@ -0,0 +1,7 @@ + -- libgit2 0.21.2