Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of turbo to enable turbo functionality for recaptcha_v3 #434

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Next

## 5.16.0
* Allow usage of `options[:turbo]` as well as `options[:turbolinks]` for `recaptcha_v3`

## 5.15.0
* Add 3.2 to the list of Ruby CI versions
* Add ability to submit verify_recaptcha via POST with JSON Body with `options[:json] = true`
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ but only accepts the following options:
| `:script` | Same as setting both `:inline_script` and `:external_script`. (default: `true`). |
| `:inline_script` | If `true`, adds an inline script tag that calls `grecaptcha.execute` for the given `site_key` and `action` and calls the `callback` with the resulting response token. Pass `false` if you want to handle calling `grecaptcha.execute` yourself. (default: `true`) |
| `:element` | The element to render, if any (default: `:input`)<br/>`:input`: Renders a hidden `<input type="hidden">` tag. The value of this will be set to the response token by the default `setInputWithRecaptchaResponseTokenFor{action}` callback.<br/>`false`: Doesn't render any tag. You'll have to add a custom callback that does something with the token. |
| `:turbolinks` | If `true`, calls the js function which executes reCAPTCHA after all the dependencies have been loaded. This cannot be used with the js param `:onload`. This makes reCAPTCHAv3 usable with turbolinks. |
| `:turbo` | If `true`, calls the js function which executes reCAPTCHA after all the dependencies have been loaded. This cannot be used with the js param `:onload`. This makes reCAPTCHAv3 usable with turbo. |
| `:turbolinks` | Alias of `:turbo`. Will be deprecated soon. |
| `:ignore_no_element` | If `true`, adds null element checker for forms that can be removed from the page by javascript like modals with forms. (default: true) |

[JavaScript resource (api.js) parameters](https://developers.google.com/recaptcha/docs/invisible#js_param):
Expand Down
6 changes: 3 additions & 3 deletions lib/recaptcha/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.recaptcha_v3(options = {})
action = options.delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
id = options.delete(:id) || "g-recaptcha-response-data-#{dasherize_action(action)}"
name = options.delete(:name) || "g-recaptcha-response-data[#{action}]"
turbolinks = options.delete(:turbolinks)
turbo = options.delete(:turbo) || options.delete(:turbolinks)
options[:render] = site_key
options[:script_async] ||= false
options[:script_defer] ||= false
Expand All @@ -24,11 +24,11 @@ def self.recaptcha_v3(options = {})
end
options[:class] = "g-recaptcha-response #{options[:class]}"

if turbolinks
if turbo
options[:onload] = recaptcha_v3_execute_function_name(action)
end
html, tag_attributes = components(options)
if turbolinks
if turbo
html << recaptcha_v3_onload_script(site_key, action, callback, id, options)
elsif recaptcha_v3_inline_script?(options)
html << recaptcha_v3_inline_script(site_key, action, callback, id, options)
Expand Down
13 changes: 13 additions & 0 deletions test/client_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@
html.must_include("onload=executeRecaptchaForRequest")
end
end

describe "turbo" do
it "adds onload to defined function" do
html = recaptcha_v3(action: 'request', turbo: true)
html.must_include("onload=executeRecaptchaForRequest")
end

it "overrides specified onload" do
html = recaptcha_v3(action: 'request', onload: "foobar", turbo: true)
html.wont_include("onload=foobar")
html.must_include("onload=executeRecaptchaForRequest")
end
end

it "adds :render option to the url" do
html = recaptcha_tags(render: 'onload')
Expand Down
Loading