diff --git a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb index 1d257f1..5e5ac8b 100644 --- a/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +++ b/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb @@ -115,29 +115,28 @@ module Spec pass end - def error_message_for(option) - msg = @validator.error_messages[option] - restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[option], @type, @record) + def error_message_for(message) + restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[message], @type, @record) - if restriction - restriction = [restriction] unless restriction.is_a?(Array) - restriction.map! {|r| @validator.class.send(:type_cast_value, r, @type) } - interpolate = @validator.send(:interpolation_values, option, restriction ) + if restriction + restriction = @validator.class.send(:type_cast_value, restriction, @type) + interpolate = @validator.send(:interpolation_values, message, restriction) + end - # get I18n message if defined and has interpolation keys in msg - if defined?(I18n) && !@validator.send(:custom_error_messages).include?(option) - msg = if defined?(ActiveRecord::Error) - ActiveRecord::Error.new(@record, @expected, option, interpolate).message - else - @record.errors.generate_message(@expected, option, interpolate) - end + if defined?(I18n) + interpolate ||= {} + options = interpolate.merge(:default => @validator.send(:custom_error_messages)[message]) + if defined?(ActiveRecord::Error) + ActiveRecord::Error.new(@record, @expected, message, options).message else - msg = msg % interpolate + @record.errors.generate_message(@expected, message, options) end - end - msg + else + interpolate ||= nil + @validator.error_messages[message] % interpolate + end end - + def format_value(value) return value if value.is_a?(String) value.strftime(@validator.class.error_value_formats[@type]) diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index b4de3ee..e17ee6b 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -46,7 +46,7 @@ module ValidatesTimeliness end def error_messages - @error_messages ||= self.class.default_error_messages.merge(custom_error_messages) + @error_messages ||= ::ActiveRecord::Errors.default_error_messages.merge(custom_error_messages) end private @@ -87,10 +87,9 @@ module ValidatesTimeliness restriction = [restriction] unless restriction.is_a?(Array) if defined?(I18n) - message = I18n.t('activerecord.errors.messages')[option] - subs = message.scan(/\{\{([^\}]*)\}\}/) interpolations = {} - subs.each_with_index {|s, i| interpolations[s[0].to_sym] = restriction[i].strftime(format) } + keys = restriction.size == 1 ? [:restriction] : [:earliest, :latest] + keys.each_with_index {|key, i| interpolations[key] = restriction[i].strftime(format) } interpolations else restriction.map {|r| r.strftime(format) } @@ -154,14 +153,6 @@ module ValidatesTimeliness # class methods class << self - def default_error_messages - if defined?(I18n) - I18n.t('activerecord.errors.messages') - else - ::ActiveRecord::Errors.default_error_messages - end - end - def error_value_format_for(type) if defined?(I18n) # work around for syntax check in vendored I18n for Rails <= 2.3.3