Module: Fluffery::Forms::Utilities
- Included in:
- Builder
- Defined in:
- lib/fluffery/forms/utilities.rb
Instance Method Summary collapse
-
#confirm_or_disable(options) ⇒ Object
Adds data-confirm, or data-disable-with if set.
-
#content_tag(tag, content, options = {}, escape = true, &block) ⇒ Object
Simply shortening calls to content_tag since it is a method of @template.
-
#error_console ⇒ Object
Allows us to call something like f.error_console to log form errors to a javascript console such as firebug.
-
#option_exists?(opt) ⇒ Boolean
Quick helper to see if an option is nil, blank, or false.
-
#render_with_fluff(method, options, html_options = nil, &block) ⇒ Object
Generate additional options on our fields.
- #validator ⇒ Object
-
#without_error_proc ⇒ Object
Override the default error proc for our forms, making sure to set it back when we are finished to avoid compatibility issues.
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() .stringify_keys! .merge!("data-disable-with" => ["disable_with"]) if .delete("disable_with") .merge!("data-confirm" => ["confirm"]) if .delete("confirm") 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 content_tag(tag, content, = {}, escape = true, &block) @template.content_tag(tag, content, , escape, &block) end |
#error_console ⇒ Object
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
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.
-
If a field has errors, wrap it with the defined error template.
-
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, , = nil, &block) @field_order << method = .nil? ? : = validator.add_html_attributes(method, ) # 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] = Fluffery::Utils::Internal.merge_html_classes(, error_class) = if .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) = @object.errors[method.to_sym] = configs[:message_error_class] renderer.result( OpenStruct.new(configs.merge!(:messages => )).send(:binding) ).to_s.html_safe end |
#validator ⇒ Object
76 77 78 |
# File 'lib/fluffery/forms/utilities.rb', line 76 def validator @validator ||= Fluffery::Forms::Validation::Base.new(@object) end |
#without_error_proc ⇒ Object
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 |