Skip to content

Commit

Permalink
Updated README regarding active record validations, fix for setting n…
Browse files Browse the repository at this point in the history
…il values
  • Loading branch information
stex committed Jul 22, 2015
1 parent eb84048 commit 4e320e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,18 @@ setting did not receive a value yet:
:validations => {:custom => [:must_be_42]}
```

There are some built-in validations (see above), but you may also define custom
validations either by passing in anonymous functions or symbols representing
class methods either in the setting class or the assigned model.
There are some built-in validations (see above), but they should be mostly used for globally defined
and globally used settings (`Setting.somethingsomething`).
For model specific settings, you may treat the setting accessors just like normal columns
and define your validations accordingly, e.g.

```ruby
setting_accessor :my_string, :type => :string
setting_accessor :my_number, :type => :integer

validates :my_string, :presence => true
validates :my_number, :numericality => {:only_integer => true}
```

Contributing
------------
Expand Down
6 changes: 5 additions & 1 deletion lib/setting_accessors/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# If the value cannot be parsed in the required type, +nil+ is assigned.
# Please make sure that you specify the correct validations in settings.yml
# to avoid this.
# or assigned model to avoid this.
#
# Currently supported types:
# - Fixnum
Expand All @@ -27,6 +27,10 @@ def convert(new_value)
#If the value is set to be polymorphic, we don't have to convert anything.
return new_value if @value_type == 'polymorphic'

#ActiveRecord only converts non-nil values to their database type
#during assignment
return new_value if new_value.nil?

parse_method = :"parse_#{@value_type}"

if private_methods.include?(parse_method)
Expand Down
2 changes: 1 addition & 1 deletion lib/setting_accessors/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SettingAccessors
VERSION = '0.0.2'
VERSION = '0.0.3'
end

0 comments on commit 4e320e9

Please sign in to comment.