Module: Recaptcha::ClientHelper

Defined in:
lib/recaptcha/client_helper.rb

Instance Method Summary collapse

Instance Method Details

#recaptcha_v2_checkbox(options = {}) ⇒ Object Also known as: recaptcha_tags, recaptcha_v2_tags

Renders a reCAPTCHA v2 Checkbox widget


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/recaptcha/client_helper.rb', line 6

def recaptcha_v2_checkbox(options = {})
  if options.key?(:stoken)
    raise(RecaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to recaptcha_tags.")
  end
  if options.key?(:ssl)
    raise(RecaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to recaptcha_tags.")
  end

  options[:site_key] ||= Recaptcha.configuration.site_key_v2_checkbox!
  noscript = options.delete(:noscript)

  html, tag_attributes, fallback_uri = Recaptcha::ClientHelper.recaptcha_components(options)
  html << %(<div #{tag_attributes}></div>\n)

  if noscript != false
    html << <<-HTML
      <noscript>
        <div>
          <div style="width: 302px; height: 422px; position: relative;">
            <div style="width: 302px; height: 422px; position: absolute;">
              <iframe
                src="#{fallback_uri}"
                name="ReCAPTCHA"
                style="width: 302px; height: 422px; border-style: none; border: 0; overflow: hidden;">
              </iframe>
            </div>
          </div>
          <div style="width: 300px; height: 60px; border-style: none;
            bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
            background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
            <textarea id="g-recaptcha-response" name="g-recaptcha-response"
              class="g-recaptcha-response"
              style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
              margin: 10px 25px; padding: 0px; resize: none;">
            </textarea>
          </div>
        </div>
      </noscript>
    HTML
  end

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

#recaptcha_v2_invisible(options = {}) ⇒ Object Also known as: invisible_recaptcha_tags

Renders a reCAPTCHA v2 Invisible reCAPTCHA


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/recaptcha/client_helper.rb', line 53

def recaptcha_v2_invisible(options = {})
  options[:callback] ||= 'invisibleRecaptchaSubmit'
  options[:ui]       ||= :button
  options[:site_key] ||= Recaptcha.configuration.site_key_v2_invisible!
  text = options.delete(:text) || 'Submit'

  html, tag_attributes = Recaptcha::ClientHelper.recaptcha_components(options)
  html << recaptcha_default_callback(options) if recaptcha_default_callback_required?(options)
  case options[:ui]
  when :button
    html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  when :invisible
    html << %(<div data-size="invisible" #{tag_attributes}></div>\n)
  when :input
    html << %(<input type="submit" #{tag_attributes} value="#{text}"/>\n)
  else
    raise(RecaptchaError, "ReCAPTCHA ui `#{options[:ui]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#recaptcha_v3(options = {}) ⇒ Object Also known as: recaptcha_v3_tags

Renders a reCAPTCHA v3 script and (by default) a hidden input to submit the response token.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/recaptcha/client_helper.rb', line 77

def recaptcha_v3(options = {})
  action = options.delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
  site_key = options[:site_key] ||= Recaptcha.configuration.site_key_v3!
  id   = options.delete(:id)   || "g-recaptcha-response-" + action.to_s.dasherize
  name = options.delete(:name) || "g-recaptcha-response[#{action}]"
  options[:render] = site_key
  options[:script_async] ||= false
  options[:script_defer] ||= false
  element = options.delete(:element)
  element = element == false ? false : :input
  if element == :input
    callback = options.delete(:callback) || recaptcha_v3_default_callback_name
  end

  html, tag_attributes = Recaptcha::ClientHelper.recaptcha_components(options)
  if recaptcha_v3_inline_script?(options)
    html << recaptcha_v3_inline_script(site_key, action, callback, id, options)
  end
  case element
  when :input
    html << %(<input type="hidden" name="#{name}" id="#{id}" #{tag_attributes}/>\n)
  when false
    # No tag
    nil
  else
    raise(RecaptchaError, "ReCAPTCHA element `#{options[:element]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end