Commit 4cd649e7d1d25f81711a5f2834768ebac906e940

Authored by Victor Costa
1 parent bc5f17aa

Remove badges when destroy a badge

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
test/unit/badge_test.rb 0 → 100644
@@ -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
views/gamification/display_achievements.html.erb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +<script>
  2 +<% if current_person.present? %>
  3 + <% current_person.badges.each do |badge| %>
  4 + <%= "console.log(#{badge.name})" %>
  5 + <% end %>
  6 +<% end %>
  7 +</script>