Sha256: fb6169f8b0ca082a7007582c53b124e8358e694f604581bbe651c90194f109f5

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

# Provides an Errors class similar with ActiveRecord ones.
#
#   class ContactForm < SimpleForm
#     attributes :name,  :validate => true
#     attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
#     attributes :message
#     attributes :nickname, :captcha => true
#   end
#
# When validating an attribute name as above, it will search for messages in
# the following order:
#
#   simple_form.messages.name
#   simple_form.messages.blank
#
# When validating email, it will search for:
#
#   simple_form.messages.name
#   simple_form.messages.invalid
#
# If the message is not available, it will output: "can't be blank" in the first
# case and "is invalid" in the second.
#
class SimpleForm
  class Errors < Hash

    def initialize(base, *args)
      @base = base
      super(*args)
    end

    alias :add   :store
    alias :count :size

    def on(attribute)
      attribute = attribute.to_sym
      return nil unless self[attribute]

      generate_message_for(attribute, self[attribute])
    end

    def full_messages
      map do |attribute, message|
        next if message.nil?
        attribute = attribute.to_sym
        "#{@base.class.human_attribute_name(attribute)} #{generate_message_for(attribute, message)}"
      end.compact.reverse
    end

    protected

      def generate_message_for(attribute, message)
        I18n.t(attribute, :default => [ message, DEFAULT_MESSAGES[message] ], :scope => [:simple_form, :messages])
      end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
josevalim-simple_form-0.1.1 lib/simple_form/errors.rb
josevalim-simple_form-0.1 lib/simple_form/errors.rb
josevalim-simple_form-0.2.0 lib/simple_form/errors.rb
josevalim-simple_form-0.2.1 lib/simple_form/errors.rb
josevalim-simple_form-0.3.1 lib/simple_form/errors.rb