Module: Turnstile::Helpers

Defined in:
lib/turnstile/helpers.rb

Constant Summary collapse

DEFAULT_ERRORS =
{
  turnstile_unreachable: 'Oops, we failed to validate your Turnstile response. Please try again.',
  verification_failed: 'Turnstile verification failed, please try again.'
}.freeze

Class Method Summary collapse

Class Method Details

.invisible_turnstile_tags(custom) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/turnstile/helpers.rb', line 36

def self.invisible_turnstile_tags(custom)
  options = { callback: 'javascriptCallback', ui: :button }.merge(custom)
  text = options.delete(:text)
  html, tag_attributes = components(options.dup)
  html << default_callback(options) if default_callback_required?(options)

  case options[:ui]
  when :button
    html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  when :input
    html << %(<input type="submit" #{tag_attributes} value="#{text}"/>\n)
  else
    raise(TurnstileError, "Turnstile ui `#{options[:ui]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.to_error_message(key) ⇒ Object



10
11
12
13
# File 'lib/turnstile/helpers.rb', line 10

def self.to_error_message(key)
  default = DEFAULT_ERRORS.fetch(key) { raise ArgumentError "Unknown Turnstile captcha error - #{key}" }
  to_message("turnstile.errors.#{key}", default)
end

.to_message(_key, default) ⇒ Object



16
17
18
# File 'lib/turnstile/helpers.rb', line 16

def self.to_message(key, default)
  I18n.translate(key, default: default)
end

.turnstile_tags(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/turnstile/helpers.rb', line 25

def self.turnstile_tags(options)
  if options.key?(:ssl)
    raise(TurnstileError, "SSL is now always true. Please remove 'ssl' from your calls to turnstile_tags.")
  end

  html, tag_attributes = components(options.dup)
  html << %(<div #{tag_attributes}></div>\n)

  html.respond_to?(:html_safe) ? html.html_safe : html
end