Module: Fluffery::Forms::Utilities

Included in:
Builder
Defined in:
lib/fluffery/forms/utilities.rb

Instance Method Summary collapse

Instance Method Details

#confirm_or_disable(options) ⇒ Object

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



17
18
19
20
21
22
# 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) ⇒ Object

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



11
12
13
# File 'lib/fluffery/forms/utilities.rb', line 11

def (tag, content, options = {}, escape = true, &block)
  @template.(tag, content, options, escape, &block)
end

#error_consoleObject

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



27
28
29
30
31
32
33
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


37
38
39
# 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) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

#validatorObject



76
77
78
# File 'lib/fluffery/forms/utilities.rb', line 76

def validator
  @validator ||= Fluffery::Forms::Validation::Base.new(@object)
end

#without_error_procObject

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



83
84
85
86
87
88
# 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