module NewGoogleRecaptcha::ViewExt

Public Instance Methods

include_recaptcha_js(opts = {}) click to toggle source
# File lib/new_google_recaptcha/view_ext.rb, line 5
def include_recaptcha_js(opts = {})
  badge = opts[:badge] ? "&badge=#{opts[:badge]}" : ""
  generate_recaptcha_callback +
    javascript_include_tag(
      "https://www.google.com/recaptcha/api.js?render=#{NewGoogleRecaptcha.site_key}&onload=newGoogleRecaptchaCallback#{badge}",
      defer: true
    )
end
recaptcha_action(action) click to toggle source
# File lib/new_google_recaptcha/view_ext.rb, line 14
def recaptcha_action(action)
  id = "new_google_recaptcha_token_#{SecureRandom.hex(10)}"
  hidden_field_tag(
    'new_recaptcha_token',
    nil,
    readonly: true,
    'data-google-recaptcha-action' => action,
    id: id
  )
end

Private Instance Methods

generate_recaptcha_callback() click to toggle source
# File lib/new_google_recaptcha/view_ext.rb, line 27
def generate_recaptcha_callback
  javascript_tag %(
    function newGoogleRecaptchaCallback () {
      grecaptcha.ready(function () {
        var elements = document.querySelectorAll('[data-google-recaptcha-action]')
        Array.prototype.slice.call(elements).forEach(function (el) {
          var action = el.dataset.googleRecaptchaAction
          if (!action) return
          grecaptcha
            .execute("#{NewGoogleRecaptcha.site_key}", { action: action })
            .then(function (token) {
              el.value = token
            })
        })
      })
    }
  )
end