Sha256: cfeefd2cc5224c9fc6b25356a5216d8d0088bc332eb5a019f2e8550ad54c1e65

Contents?: true

Size: 1.31 KB

Versions: 24

Compression:

Stored size: 1.31 KB

Contents

module Shoulda # :nodoc:
  module ActiveRecord # :nodoc:
    module Helpers
      def pretty_error_messages(obj) # :nodoc:
        obj.errors.map do |a, m| 
          msg = "#{a} #{m}" 
          msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
        end
      end

      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

      def instance_variable_name_for(klass)
        klass.to_s.split('::').last.underscore
      end

      # 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.
      #
      #   default_error_message(:blank)
      #   default_error_message(:too_short, :count => 5)
      #   default_error_message(:too_long, :count => 60)
      def default_error_message(key, values = {})
        if Object.const_defined?(:I18n) # Rails >= 2.2
          I18n.translate("activerecord.errors.messages.#{key}", values)
        else # Rails <= 2.1.x
          ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 9 rubygems

Version Path
Flamefork-shoulda-2.10.1 lib/shoulda/active_record/helpers.rb
Flamefork-shoulda-2.10.2 lib/shoulda/active_record/helpers.rb
francois-shoulda-2.10.1 lib/shoulda/active_record/helpers.rb
gnip-gnip-1.1.1 test/lib/shoulda/active_record/helpers.rb
ratnikov-shoulda-2.9.0.1 lib/shoulda/active_record/helpers.rb
ratnikov-shoulda-2.9.0.2 lib/shoulda/active_record/helpers.rb
ratnikov-shoulda-2.9.0.3 lib/shoulda/active_record/helpers.rb
ratnikov-shoulda-2.9.0 lib/shoulda/active_record/helpers.rb
rmm5t-shoulda-2.9.1 lib/shoulda/active_record/helpers.rb
technicalpickles-shoulda-2.10.0 lib/shoulda/active_record/helpers.rb
thoughtbot-shoulda-2.10.0 lib/shoulda/active_record/helpers.rb
thoughtbot-shoulda-2.10.1 lib/shoulda/active_record/helpers.rb
thoughtbot-shoulda-2.9.0 lib/shoulda/active_record/helpers.rb
thoughtbot-shoulda-2.9.1 lib/shoulda/active_record/helpers.rb
thoughtbot-shoulda-2.9.2 lib/shoulda/active_record/helpers.rb
shoulda-2.9.0 lib/shoulda/active_record/helpers.rb
shoulda-2.9.1 lib/shoulda/active_record/helpers.rb
shoulda-2.9.2 lib/shoulda/active_record/helpers.rb
shoulda-2.10.0 lib/shoulda/active_record/helpers.rb
shoulda-2.10.1 lib/shoulda/active_record/helpers.rb