module Fluffery::Forms::Utilities

Public Instance Methods

confirm_or_disable(options) click to toggle source

Adds data-confirm, or data-disable-with if set.

# File lib/fluffery/forms/utilities.rb, line 17
def confirm_or_disable(options)
  options.stringify_keys!
  options.merge!("data-disable-with" => options["disable_with"]) if options.delete("disable_with")
  options.merge!("data-confirm" => options["confirm"]) if options.delete("confirm")
  options
end
content_tag(tag, content, options = {}, escape = true, &block) click to toggle source

Simply shortening calls to content_tag since it is a method of @template

# File lib/fluffery/forms/utilities.rb, line 11
def content_tag(tag, content, options = {}, escape = true, &block)
  @template.content_tag(tag, content, options, escape, &block)
end
error_console() click to toggle source

Allows us to call something like f.error_console to log form errors to a javascript console such as firebug

# File lib/fluffery/forms/utilities.rb, line 27
def error_console
  "<script type=\"text/javascript\" charset=\"utf-8\">
  //<![CDATA[
      try{ console.log(\"#{@template.escape_javascript(@object.errors.inspect)}\")}catch(e){}
  //]]>
  </script>".html_safe
end
option_exists?(opt) click to toggle source

Quick helper to see if an option is nil, blank, or false

# File lib/fluffery/forms/utilities.rb, line 37
def option_exists?(opt)
  !(opt.nil? || opt.to_s.blank? || opt.to_s === "false")
end
render_with_fluff(method, options, html_options = nil, &block) click to toggle source

Generate additional options on our fields.

  1. If a field has errors, wrap it with the defined error template.

  2. Also add our error class to the field itself.

# File lib/fluffery/forms/utilities.rb, line 45
def render_with_fluff(method, options, html_options = nil, &block)
  
  @field_order << method       
  
  _options = html_options.nil? ? options : html_options
  _options = validator.add_html_attributes(method, _options)
  
  # If no errors, simply return.
  unless validator.errors_for?(method)
    return block.call
  end
  
  configs     = Fluffery::Config.forms
  template    = configs[:error_template]
  error_class = configs[:error_class]
  _options    = Fluffery::Utils::Internal.merge_html_classes(_options, error_class)
  options     = _options if html_options.nil?
  
  # Capture the original html tag with any updated options.
  html_tag = block.call
  return html_tag if template.nil? or template.blank?
  
  renderer            = ERB.new(template)
  messages            = @object.errors[method.to_sym]
  message_error_class = configs[:message_error_class]
  renderer.result(
    OpenStruct.new(configs.merge!(:messages => messages)).send(:binding)
    ).to_s.html_safe
  
end
validator() click to toggle source
# File lib/fluffery/forms/utilities.rb, line 76
def validator
  @validator ||= Fluffery::Forms::Validation::Base.new(@object)
end
without_error_proc() { || ... } click to toggle source

Override the default error proc for our forms, making sure to set it back when we are finished to avoid compatibility issues.

# File lib/fluffery/forms/utilities.rb, line 83
def without_error_proc
  default_proc = ActionView::Base.field_error_proc
  ActionView::Base.field_error_proc, proc = lambda{ |html_tag, instance| html_tag }
  yield        
  ActionView::Base.field_error_proc = default_proc
end