Commit cda000186107d91bd0a4ef9330b74afb8293fc7d
1 parent
42340c3b
Exists in
master
and in
23 other branches
ActionItem41: adding the possibility of giving a default value for settings
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1330 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
11 additions
and
1 deletions
Show diff stats
lib/acts_as_having_settings.rb
| @@ -19,10 +19,14 @@ module ActsAsHavingSettings | @@ -19,10 +19,14 @@ module ActsAsHavingSettings | ||
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def settings_items(*names) | 21 | def settings_items(*names) |
| 22 | + | ||
| 23 | + options = names.last.is_a?(Hash) ? names.pop : {} | ||
| 24 | + default = options[:default] ? "|| #{options[:default].inspect}" : "" | ||
| 25 | + | ||
| 22 | names.each do |setting| | 26 | names.each do |setting| |
| 23 | class_eval <<-CODE | 27 | class_eval <<-CODE |
| 24 | def #{setting} | 28 | def #{setting} |
| 25 | - send(self.class.settings_field)[:#{setting}] | 29 | + send(self.class.settings_field)[:#{setting}] #{default} |
| 26 | end | 30 | end |
| 27 | def #{setting}=(value) | 31 | def #{setting}=(value) |
| 28 | send(self.class.settings_field)[:#{setting}] = value | 32 | send(self.class.settings_field)[:#{setting}] = value |
test/unit/acts_as_having_settings_test.rb
| @@ -42,4 +42,10 @@ class ActsAsHavingSettingsTest < Test::Unit::TestCase | @@ -42,4 +42,10 @@ class ActsAsHavingSettingsTest < Test::Unit::TestCase | ||
| 42 | assert_equal 15, Block.find(block.id).limit | 42 | assert_equal 15, Block.find(block.id).limit |
| 43 | end | 43 | end |
| 44 | 44 | ||
| 45 | + should 'be able to specify default values' do | ||
| 46 | + block_class = Class.new(Block) | ||
| 47 | + block_class.settings_items :some_setting, :default => 10 | ||
| 48 | + assert_equal 10, block_class.new.some_setting | ||
| 49 | + end | ||
| 50 | + | ||
| 45 | end | 51 | end |