Module: Shoulda::ActiveRecord::Helpers
- Included in:
- Matchers::AllowValueMatcher, Matchers::EnsureLengthOfMatcher, Matchers::ValidateUniquenessOfMatcher, Test::Unit::TestCase
- Defined in:
- lib/shoulda/active_record/helpers.rb
Instance Method Summary collapse
-
#default_error_message(key, values = {}) ⇒ Object
Helper method that determines the default error message used by Active Record.
- #get_instance_of(object_or_klass) ⇒ Object
- #instance_variable_name_for(klass) ⇒ Object
-
#pretty_error_messages(obj) ⇒ Object
:nodoc:.
Instance Method Details
#default_error_message(key, values = {}) ⇒ Object
Helper method that determines the default error message used by Active Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly introduced I18n module used for localization.
(:blank)
(:too_short, :count => 5)
(:too_long, :count => 60)
31 32 33 34 35 36 37 |
# File 'lib/shoulda/active_record/helpers.rb', line 31 def (key, values = {}) if Object.const_defined?(:I18n) # Rails >= 2.2 I18n.translate("activerecord.errors.messages.#{key}", values) else # Rails <= 2.1.x ::ActiveRecord::Errors.[key] % values[:count] end end |
#get_instance_of(object_or_klass) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/shoulda/active_record/helpers.rb', line 11 def get_instance_of(object_or_klass) if object_or_klass.is_a?(Class) klass = object_or_klass instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new else object_or_klass end end |
#instance_variable_name_for(klass) ⇒ Object
20 21 22 |
# File 'lib/shoulda/active_record/helpers.rb', line 20 def instance_variable_name_for(klass) klass.to_s.split('::').last.underscore end |
#pretty_error_messages(obj) ⇒ Object
:nodoc:
4 5 6 7 8 9 |
# File 'lib/shoulda/active_record/helpers.rb', line 4 def (obj) # :nodoc: obj.errors.map do |a, m| msg = "#{a} #{m}" msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base end end |