Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow custom error messages

  • Loading branch information...
commit cbc9d225c0621024f0ffcd7abc05a8f0cc0828b6 1 parent 2ef33ed
Henning Koch henning-koch authored
1  README.md
View
@@ -23,6 +23,7 @@ The assigned value is checked during validation:
Song.new(:genre => 'elephant').valid? # => false
The validation error message is the same as the one from `validates_inclusion_of` (`errors.messages.inclusion` in your I18n dictionary).
+You can also set a custom error message with the `:message` option.
### Listing assignable values
6 lib/assignable_values/active_record/restriction/base.rb
View
@@ -34,7 +34,11 @@ def error_property
end
def not_included_error_message
- I18n.t('errors.messages.inclusion', :default => 'is not included in the list')
+ if @options[:message]
+ @options[:message]
+ else
+ I18n.t('errors.messages.inclusion', :default => 'is not included in the list')
+ end
end
def assignable_value?(record, value)
2  lib/assignable_values/version.rb
View
@@ -1,3 +1,3 @@
module AssignableValues
- VERSION = '0.7.0'
+ VERSION = '0.7.1'
end
2  spec/rails-2.3/Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
- assignable_values (0.6.1)
+ assignable_values (0.7.1)
activerecord
GEM
2  spec/rails-3.0/Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
- assignable_values (0.6.1)
+ assignable_values (0.7.1)
activerecord
GEM
2  spec/rails-3.2/Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
- assignable_values (0.6.1)
+ assignable_values (0.7.1)
activerecord
GEM
21 spec/shared/assignable_values/active_record_spec.rb
View
@@ -112,7 +112,6 @@
context 'if the :allow_blank option is set to a lambda ' do
-
before :each do
@klass = Song.disposable_copy do
@@ -132,6 +131,26 @@
end
+ context 'if the :message option is set to a string' do
+
+ before :each do
+ @klass = Song.disposable_copy do
+ assignable_values_for :genre, :message => 'should be something different' do
+ %w[pop rock]
+ end
+ end
+ end
+
+ it 'should use this string as a custom error message' do
+ record = @klass.new(:genre => 'disallowed value')
+ record.valid?
+ errors = record.errors[:genre]
+ error = errors.is_a?(Array) ? errors.first : errors # the return value sometimes was a string, sometimes an Array in Rails
+ error.should == 'should be something different'
+ end
+
+ end
+
end
context 'when validating belongs_to associations' do
Please sign in to comment.
Something went wrong with that request. Please try again.