Module: ActionView::Helpers::FormHelper

Defined in:
lib/mead_captcha/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#mead_honeypot(form_tag, object_name, name = nil, options = {}, &block) ⇒ Object

Creates a honeypot on forms that will be appropriately namespaced.

Returns a FormHelper tag of type form_tag that will be namespaced via the object_name and that will use the hash of options. Allows you to declare a name, or if nil grabs a pseudo-random name. Also allows you to pass in a hash of options that will be passed to the form_tag. Takes a form_tag as a string or symbol, the object_name as a string or symbol, a name as a string or symbol, a hash of options that will be passed to the form_tag, and an optional block.

If you pass in a block you will have access to the pre-made tag and the pseudo-randomly created name.

Examples

mead_honeypot(:text_field, :user)
# => <input type="text" name="user[honeypot]" id=user_honeypot">

mead_honeypot(:text_field, :user) do |honeypot, name|
  label(:user, name)
  honeypot
# => <label for="name">
#    <input type="text" name="user[name]" id="user_name">


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mead_captcha/form_helper.rb', line 28

def mead_honeypot(form_tag, object_name, name = nil, options = {}, &block)
  defaults = {value: nil}.merge(mead_input_attributes).merge(options)
  name ||= mead_field_name
  form_tag = :text_field unless [:text_field, :text_area].include?(form_tag)

  html = tag_class(form_tag).new(object_name, name, self, defaults).render

  if block_given?
    capture(html, name, &block)
  else
    html
  end
end

#mead_obfuscate(input_type, object_name, method, options = {}, &block) ⇒ Object

Creates an obfuscation on forms that will be appropriately namespaced.

Returns an input tag of type input_type that will get the method value from the object_name that is passed in. Can alternatively be provided with a block that will give the user access to a pre-made input tag, the obfuscated name, and the name of the method that was passed in.

Examples

mead_obfuscate(:checkbox, :user, :active)
# => <input type="checkbox" name="user[foo]" id="user_foo">

mead_obfuscate(:checkbox, :user, :active) do |html, obfus_name, real_name|
  label(:user, obfus_name, real_name)
  html
# => <label for="user_foo">Active</label>
#    <input type="hidden" value="0" name="user[foo]" id="user_foo">
#    <input type="checkbox" value="1" name="user[foo]" id="user_foo">


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mead_captcha/form_helper.rb', line 60

def mead_obfuscate(input_type, object_name, method, options = {}, &block)
  real_name = method.to_s
  obfuscated_name = mead_obfuscate_field(real_name)
  options.merge!({
    obfuscated_name: obfuscated_name,
    type: input_type.to_s
  })

  html = Tags::ObfuscatedTag.new(object_name, real_name, self, options).render

  if block_given?
    capture(html, obfuscated_name, real_name.titleize, &block)
  else
    html
  end
end