Module: SpamProtect::FormBuilderMethods

Defined in:
lib/spam_protect/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#spam_protect_field(name: nil, timestamp_name: nil, wrapper: false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spam_protect/form_builder.rb', line 5

def spam_protect_field(name: nil, timestamp_name: nil, wrapper: false)
  name ||= SpamProtect.config.honeypot_field
  timestamp_name ||= SpamProtect.config.timestamp_field
  honeypot_class = SpamProtect.config.honeypot_class
  wrapper_class = SpamProtect.config.wrapper_class

  payload = Encryption::Payload.generate
  token = Encryption.encrypt(payload.to_h)

  honeypot = @template.text_field_tag("#{@object_name}[#{name}]", nil, class: honeypot_class, autocomplete: "off", tabindex: "-1")
  signature_input = @template.hidden_field_tag("#{@object_name}[#{timestamp_name}]", token)

  if wrapper
    @template.(:div, honeypot + signature_input, class: wrapper_class)
  else
    (honeypot + signature_input).html_safe
  end
end