Commit d3082f5df5b83ad7ef6d1aac155ff2d1a3ddfcf6
1 parent
778373b6
Exists in
master
and in
28 other branches
Symbolize keys of settings hash before save
(ActionItem2048)
Showing
4 changed files
with
22 additions
and
3 deletions
Show diff stats
app/models/environment.rb
@@ -236,17 +236,17 @@ class Environment < ActiveRecord::Base | @@ -236,17 +236,17 @@ class Environment < ActiveRecord::Base | ||
236 | 236 | ||
237 | # Enables a feature identified by its name | 237 | # Enables a feature identified by its name |
238 | def enable(feature) | 238 | def enable(feature) |
239 | - self.settings["#{feature}_enabled"] = true | 239 | + self.settings["#{feature}_enabled".to_sym] = true |
240 | end | 240 | end |
241 | 241 | ||
242 | # Disables a feature identified by its name | 242 | # Disables a feature identified by its name |
243 | def disable(feature) | 243 | def disable(feature) |
244 | - self.settings["#{feature}_enabled"] = false | 244 | + self.settings["#{feature}_enabled".to_sym] = false |
245 | end | 245 | end |
246 | 246 | ||
247 | # Tells if a feature, identified by its name, is enabled | 247 | # Tells if a feature, identified by its name, is enabled |
248 | def enabled?(feature) | 248 | def enabled?(feature) |
249 | - self.settings["#{feature}_enabled"] == true | 249 | + self.settings["#{feature}_enabled".to_sym] == true |
250 | end | 250 | end |
251 | 251 | ||
252 | # enables the features identified by <tt>features</tt>, which is expected to | 252 | # enables the features identified by <tt>features</tt>, which is expected to |
lib/acts_as_having_settings.rb
@@ -14,6 +14,11 @@ module ActsAsHavingSettings | @@ -14,6 +14,11 @@ module ActsAsHavingSettings | ||
14 | def #{settings_field} | 14 | def #{settings_field} |
15 | self[:#{settings_field}] ||= Hash.new | 15 | self[:#{settings_field}] ||= Hash.new |
16 | end | 16 | end |
17 | + before_save :symbolize_settings_keys | ||
18 | + private | ||
19 | + def symbolize_settings_keys | ||
20 | + self[:#{settings_field}] && self[:#{settings_field}].symbolize_keys! | ||
21 | + end | ||
17 | CODE | 22 | CODE |
18 | settings_items(*args) | 23 | settings_items(*args) |
19 | end | 24 | end |
test/unit/acts_as_having_settings_test.rb
@@ -74,4 +74,10 @@ class ActsAsHavingSettingsTest < Test::Unit::TestCase | @@ -74,4 +74,10 @@ class ActsAsHavingSettingsTest < Test::Unit::TestCase | ||
74 | assert_equal true, obj.flag | 74 | assert_equal true, obj.flag |
75 | end | 75 | end |
76 | 76 | ||
77 | + should 'symbolize keys when save' do | ||
78 | + obj = TestClass.new | ||
79 | + obj.settings.expects(:symbolize_keys!).once | ||
80 | + assert obj.save | ||
81 | + end | ||
82 | + | ||
77 | end | 83 | end |
test/unit/environment_test.rb
@@ -1157,4 +1157,12 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -1157,4 +1157,12 @@ class EnvironmentTest < Test::Unit::TestCase | ||
1157 | p2.save! | 1157 | p2.save! |
1158 | end | 1158 | end |
1159 | 1159 | ||
1160 | + should 'always store setting keys as symbol' do | ||
1161 | + env = Environment.default | ||
1162 | + env.settings['string_key'] = 'new value' | ||
1163 | + env.save!; env.reload | ||
1164 | + assert_nil env.settings['string_key'] | ||
1165 | + assert_equal env.settings[:string_key], 'new value' | ||
1166 | + end | ||
1167 | + | ||
1160 | end | 1168 | end |