Commit 5125e5a82739f0cf9351febe65745495ff3f92cc

Authored by Hugo Melo
1 parent c2e3429a

Fix badge migration

db/migrate/20151003000212_move_badge_threshold_to_action_key.rb
1 1 class MoveBadgeThresholdToActionKey < ActiveRecord::Migration
2 2 def up
  3 + change_table :gamification_plugin_badges do |t|
  4 + t.change :custom_fields, :text
  5 + end
3 6 GamificationPlugin::Badge.all.each do |badge|
4 7 next if Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].nil?
5   - Merit::BadgeRules::AVAILABLE_RULES.each do |name, settings|
6   - setting = settings.first
  8 + Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].each do |setting|
7 9 badge.custom_fields[setting[:action]] = {threshold: badge.custom_fields[:threshold]} unless badge.custom_fields[:threshold].nil?
8 10 badge.save
9 11 end
... ... @@ -13,11 +15,12 @@ class MoveBadgeThresholdToActionKey &lt; ActiveRecord::Migration
13 15 def down
14 16 GamificationPlugin::Badge.all.each do |badge|
15 17 next if Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].nil?
16   - Merit::BadgeRules::AVAILABLE_RULES.each do |name, settings|
17   - setting = settings.first
18   - badge.custom_fields[:threshold] = badge.custom_fields[setting[:action]][:threshold] unless badge.custom_fields[setting[:action]][:threshold].nil?
19   - badge.save
20   - end
  18 + setting = Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].first
  19 + badge.custom_fields = {threshold: badge.custom_fields.fetch(setting[:action], {}).fetch(:threshold, "")}
  20 + badge.save
  21 + end
  22 + change_table :gamification_plugin_badges do |t|
  23 + t.change :custom_fields, :string
21 24 end
22 25 end
23 26 end
... ...