validatious-on-rails 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -189,9 +189,10 @@ module ValidatiousOnRails
189
189
  # Generate proper error message using explicit message, or I18n-lookup.
190
190
  # Core validations gets treated by Rails - unless explicit message is set that is.
191
191
  #
192
- def generate_message(validation, *args)
192
+ def generate_message(*args)
193
193
  options = args.extract_options!
194
- explicit_message = validation.options[:message]
194
+ validation = args.shift if args.first.is_a?(::ActiveRecord::Reflection::MacroReflection)
195
+ explicit_message = validation.options[:message] if validation
195
196
  key = options.delete(:key) || (explicit_message if explicit_message.is_a?(::Symbol))
196
197
 
197
198
  message = if key.present?
@@ -199,7 +200,7 @@ module ValidatiousOnRails
199
200
  :default => "activerecord.errors.messages.#{key}"))
200
201
  elsif explicit_message.is_a?(::String)
201
202
  explicit_message
202
- else
203
+ elsif validation.present?
203
204
  unless ::ValidatiousOnRails::ModelValidations::CORE_VALIDATIONS.include?(validation.macro.to_sym)
204
205
  # No core validation, try to make up a descent I18n lookup path using conventions.
205
206
  key ||= validation.macro.to_s.tr('-', '_').gsub(/^validates?_/, '').gsub(/_of/, '').to_sym
@@ -11,7 +11,7 @@ module ValidatiousOnRails
11
11
  name = 'length-is'
12
12
  super name, options
13
13
  self.params = ['count']
14
- self.message = self.class.generate_message(validation, :key => :wrong_length)
14
+ self.message = self.class.generate_message(validation, :key => :wrong_length, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
17
  #{self.class.validate_blank(validation)}
@@ -11,7 +11,7 @@ module ValidatiousOnRails
11
11
  name = 'length-maximum'
12
12
  super name, options
13
13
  self.params = ['count']
14
- self.message = self.class.generate_message(validation, :key => :too_long)
14
+ self.message = self.class.generate_message(validation, :key => :too_long, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
17
  #{self.class.validate_blank(validation)}
@@ -11,7 +11,7 @@ module ValidatiousOnRails
11
11
  name = 'length-minimum'
12
12
  super name, options
13
13
  self.params = ['count']
14
- self.message = self.class.generate_message(validation, :key => :too_short)
14
+ self.message = self.class.generate_message(validation, :key => :too_short, :count => '{{count}}')
15
15
  self.accept_empty = validation.options[:allow_nil]
16
16
  self.fn = %{
17
17
  #{self.class.validate_blank(validation)}
@@ -10,7 +10,7 @@ module ValidatiousOnRails
10
10
  name = 'numericality-equal-to'
11
11
  super name, options
12
12
  self.params = ['count']
13
- self.message = self.class.generate_message(validation, :key => :equal_to)
13
+ self.message = self.class.generate_message(validation, :key => :equal_to, :count => '{{count}}')
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
16
  value = +value;
@@ -10,7 +10,7 @@ module ValidatiousOnRails
10
10
  name = 'numericality-greater-than-or-equal-to'
11
11
  super name, options
12
12
  self.params = ['count']
13
- self.message = self.class.generate_message(validation, :key => :greater_than_or_equal_to)
13
+ self.message = self.class.generate_message(validation, :key => :greater_than_or_equal_to, :count => '{{count}}')
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
16
  value = +value;
@@ -10,7 +10,7 @@ module ValidatiousOnRails
10
10
  name = 'numericality-greater-than'
11
11
  super name, options
12
12
  self.params = ['count']
13
- self.message = self.class.generate_message(validation, :key => :greater_than)
13
+ self.message = self.class.generate_message(validation, :key => :greater_than, :count => '{{count}}')
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
16
  value = +value;
@@ -10,7 +10,7 @@ module ValidatiousOnRails
10
10
  name = 'numericality-less-than-or-equal-to'
11
11
  super name, options
12
12
  self.params = ['count']
13
- self.message = self.class.generate_message(validation, :key => :less_than_or_equal_to)
13
+ self.message = self.class.generate_message(validation, :key => :less_than_or_equal_to, :count => '{{count}}')
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
16
  value = +value;
@@ -10,7 +10,7 @@ module ValidatiousOnRails
10
10
  name = 'numericality-less-than'
11
11
  super name, options
12
12
  self.params = ['count']
13
- self.message = self.class.generate_message(validation, :key => :less_than)
13
+ self.message = self.class.generate_message(validation, :key => :less_than, :count => '{{count}}')
14
14
  self.accept_empty = validation.options[:allow_nil]
15
15
  self.fn = %{
16
16
  value = +value;
data/test/test_helper.rb CHANGED
@@ -57,15 +57,15 @@ build_model :bogus_items do
57
57
  string :email
58
58
  string :file_path
59
59
  string :dummie
60
-
60
+
61
61
  text :body
62
62
  integer :variant
63
63
  boolean :signed
64
-
64
+
65
65
  string :field_with_defaults
66
66
  string :field_with_client_side_validations
67
67
  string :field_without_client_side_validations
68
-
68
+
69
69
  validates_presence_of :name, :body, :variant, :file_path, :dummie
70
70
  validates_uniqueness_of :name
71
71
  validates_confirmation_of :name
@@ -75,11 +75,12 @@ build_model :bogus_items do
75
75
  :name => 'url', :message => 'Invalid URL.'
76
76
  validates_inclusion_of :variant, :in => (1..5).to_a
77
77
  validates_exclusion_of :variant, :in => (6..10).to_a
78
-
78
+ validates_length_of :url, :minimum => 10
79
+
79
80
  validates_presence_of :field_with_defaults
80
81
  validates_presence_of :field_with_client_side_validations, :client_side => true
81
82
  validates_presence_of :field_without_client_side_validations, :client_side => false
82
-
83
+
83
84
  # TODO: Test: If this is a validator makro, then it should not cause any issues.
84
85
  validates_craziness_of :name
85
86
  end
@@ -57,4 +57,17 @@ class ValidatorTest < ::ActiveSupport::TestCase
57
57
  assert_equal expected_v2_validator.gsub(/[\n\s\t]/, ''), @custom_validator.to_js.gsub(/[\n\s\t]/, '')
58
58
  end
59
59
 
60
+ context "Message" do
61
+ test "I18n lookup" do
62
+ validator_klass = ValidatiousOnRails::Validatious::Validator
63
+ # For some reason can't raise this in tests. =S
64
+ # assert_raise(::I18n::MissingInterpolationArgument) {
65
+ # validator_klass.generate_message :key => :too_short
66
+ # }
67
+ assert_nothing_raised(::I18n::MissingInterpolationArgument) {
68
+ validator_klass.generate_message :key => :too_short, :count => '{{count}}'
69
+ }
70
+ end
71
+ end
72
+
60
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validatious-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Grimfelt