Commit 5125e5a82739f0cf9351febe65745495ff3f92cc
1 parent
c2e3429a
Exists in
master
and in
1 other branch
Fix badge migration
Showing
1 changed file
with
10 additions
and
7 deletions
Show diff stats
db/migrate/20151003000212_move_badge_threshold_to_action_key.rb
1 | class MoveBadgeThresholdToActionKey < ActiveRecord::Migration | 1 | class MoveBadgeThresholdToActionKey < ActiveRecord::Migration |
2 | def up | 2 | def up |
3 | + change_table :gamification_plugin_badges do |t| | ||
4 | + t.change :custom_fields, :text | ||
5 | + end | ||
3 | GamificationPlugin::Badge.all.each do |badge| | 6 | GamificationPlugin::Badge.all.each do |badge| |
4 | next if Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].nil? | 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 | badge.custom_fields[setting[:action]] = {threshold: badge.custom_fields[:threshold]} unless badge.custom_fields[:threshold].nil? | 9 | badge.custom_fields[setting[:action]] = {threshold: badge.custom_fields[:threshold]} unless badge.custom_fields[:threshold].nil? |
8 | badge.save | 10 | badge.save |
9 | end | 11 | end |
@@ -13,11 +15,12 @@ class MoveBadgeThresholdToActionKey < ActiveRecord::Migration | @@ -13,11 +15,12 @@ class MoveBadgeThresholdToActionKey < ActiveRecord::Migration | ||
13 | def down | 15 | def down |
14 | GamificationPlugin::Badge.all.each do |badge| | 16 | GamificationPlugin::Badge.all.each do |badge| |
15 | next if Merit::BadgeRules::AVAILABLE_RULES[badge.name.to_sym].nil? | 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 | end | 24 | end |
22 | end | 25 | end |
23 | end | 26 | end |