Commit 72253b6a2f4e42e74912ed8465d8dbbe1de9397a
Exists in
master
and in
29 other branches
Merge commit 'refs/merge-requests/22' of http://git.gitorious.org/noosfero/noosf…
…ero into merge-requests/22
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
@@ -1167,4 +1167,12 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -1167,4 +1167,12 @@ class EnvironmentTest < Test::Unit::TestCase | ||
1167 | p2.save! | 1167 | p2.save! |
1168 | end | 1168 | end |
1169 | 1169 | ||
1170 | + should 'always store setting keys as symbol' do | ||
1171 | + env = Environment.default | ||
1172 | + env.settings['string_key'] = 'new value' | ||
1173 | + env.save!; env.reload | ||
1174 | + assert_nil env.settings['string_key'] | ||
1175 | + assert_equal env.settings[:string_key], 'new value' | ||
1176 | + end | ||
1177 | + | ||
1170 | end | 1178 | end |