Sha256: d47590761719c91a1d1250dae7535322b9664fac3514e4ebbff6ea2f79e3cda5

Contents?: true

Size: 1.83 KB

Versions: 57

Compression:

Stored size: 1.83 KB

Contents

# Ohm error helpers.
#
# == Usage
#
#   # Only for those who use Ohm and HAML
#   require 'ohm'
#   require 'haml'
#
#   require 'sinatra/support/ohmerrorhelpers'
#
#   class Main < Sinatra::Base
#     helpers Sinatra::OhmErrorHelpers
#   end
#
# == Common usage
#   
#   - errors_on @user do |e|
#     - e.on [:email, :not_present], "We need your email address."
#     - e.on [:password, :not_present], "You must specify a password."
#
#   # produces the following:
#   # <div class="errors">
#   #   <ul>
#   #     <li>We need your email address</li>
#   #     <li>You must specify a password.</li>
#   #   </ul>
#   # </div>
#
module Sinatra::OhmErrorHelpers
  # Presents errors on your form. Takes the explicit approach and assumes
  # that for every form you have, the copy for the errors are important,
  # instead of producing canned responses.
  #
  # @param [#errors] object An object responding to #errors. This validation
  #                  also checks for the presence of a #full_messages method
  #                  in the errors object for compatibility with 
  #                  ActiveRecord style objects.
  # @param [Hash] options a hash of HTML attributes to place on the
  #               containing div.
  # @option options [#to_s] :class (defaults to errors) The css class to put 
  #                                in the div.
  #
  # @yield [Sinatra::Support::HamlErrorPresenter] an object responding to #on.
  #
  def errors_on(object, options = { :class => 'errors' }, &block)
    return if object.errors.empty?

    lines = if object.errors.respond_to?(:full_messages)
      object.errors.full_messages
    else
      HamlErrorPresenter.new(object.errors).present(self, &block)
    end

    haml_tag(:div, options) do
      haml_tag(:ul) do
        lines.each do |error|
          haml_tag(:li, error)
        end
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 2 rubygems

Version Path
classiccms-0.5.13 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.12 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.11 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.10 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.9 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.8 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.7 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.6 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.5 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.2 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.1 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.5.0 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.4.2 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.4.1 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.4.0 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.3.9 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.3.8 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.3.7 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.3.6 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb
classiccms-0.3.5 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/errorhelpers.rb