From 9cf0c851ba0ba8b6125168ca6a2affc440f9fd1d Mon Sep 17 00:00:00 2001 From: James Brown Date: Wed, 24 Apr 2024 14:18:16 -0700 Subject: [PATCH] feat: support passing through arbitrary HTML options --- Gemfile | 2 ++ Gemfile.lock | 4 ++++ README.md | 2 +- lib/rails_cloudflare_turnstile/view_helpers.rb | 14 +++++++------- .../view_helpers_spec.rb | 4 ++++ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index a9d9fe7..1be5068 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } gemspec +gem "base64" +gem "mutex_m" gem "faraday", ">= 1.0", "< 3.0" gem "rails", ">= 6", "< 8" gem "rake", "~> 13.2" diff --git a/Gemfile.lock b/Gemfile.lock index f483ccf..c9d65b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,7 @@ GEM addressable (2.8.6) public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) + base64 (0.2.0) bigdecimal (3.1.6) builder (3.2.4) bundle-audit (0.1.0) @@ -130,6 +131,7 @@ GEM mini_mime (1.1.5) minitest (5.22.3) multipart-post (2.3.0) + mutex_m (0.2.0) net-imap (0.4.10) date net-protocol @@ -279,8 +281,10 @@ PLATFORMS x86_64-linux DEPENDENCIES + base64 bundle-audit (~> 0.1.0) faraday (>= 1.0, < 3.0) + mutex_m pry (~> 0.14.1) rails (>= 6, < 8) rails_cloudflare_turnstile! diff --git a/README.md b/README.md index 6ac4188..d042c24 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ To totally disable Turnstile, you can set `c.enabled = false` and all other conf To use Turnstile for a view: 1. Call `cloudflare_turnstile_script_tag` in your layout - 2. Call `cloudflare_turnstile` in your form View + 2. Call `cloudflare_turnstile` in your form View. Keyword arguments are passed to the tag helper (for example, to set the `tabindex` option, you could use `cloudflare_turnstile(data: {tabindex: 0})`) 3. Call `validate_cloudflare_turnstile` as a `before_action` in your controller. If the challenge fails, the exception `RailsCloudflareTurnstile::Forbidden` will be raised; you should handle this with diff --git a/lib/rails_cloudflare_turnstile/view_helpers.rb b/lib/rails_cloudflare_turnstile/view_helpers.rb index 9db89bd..01fbe94 100644 --- a/lib/rails_cloudflare_turnstile/view_helpers.rb +++ b/lib/rails_cloudflare_turnstile/view_helpers.rb @@ -2,14 +2,14 @@ module RailsCloudflareTurnstile module ViewHelpers - def cloudflare_turnstile(action: "other", data_callback: nil) + def cloudflare_turnstile(action: "other", data_callback: nil, **html_options) if RailsCloudflareTurnstile.enabled? content_tag(:div, class: "cloudflare-turnstile") do - concat turnstile_div(action, data_callback: data_callback) + concat turnstile_div(action, data_callback: data_callback, **html_options) end elsif RailsCloudflareTurnstile.mock_enabled? content_tag(:div, class: "cloudflare-turnstile") do - concat mock_turnstile_div(action, data_callback: data_callback) + concat mock_turnstile_div(action, data_callback: data_callback, **html_options) end end end @@ -28,15 +28,15 @@ def cloudflare_turnstile_script_tag(async: true, defer: true) private - def turnstile_div(action, data_callback: nil) + def turnstile_div(action, data_callback: nil, **html_options) config = RailsCloudflareTurnstile.configuration - content_tag(:div, :class => "cf-turnstile", "data-sitekey" => site_key, "data-size" => config.size, "data-action" => action, "data-callback" => data_callback, "data-theme" => config.theme) do + content_tag(:div, :class => "cf-turnstile", "data-sitekey" => site_key, "data-size" => config.size, "data-action" => action, "data-callback" => data_callback, "data-theme" => config.theme, **html_options) do "" end end - def mock_turnstile_div(action, data_callback: nil) - content_tag(:div, :class => "cf-turnstile", :style => "width: 300px; height: 65px; border: 1px solid gray; display: flex; flex-direction: row; justify-content: center; align-items: center; margin: 10px;", "data-callback" => data_callback) do + def mock_turnstile_div(action, data_callback: nil, **html_options) + content_tag(:div, :class => "cf-turnstile", :style => "width: 300px; height: 65px; border: 1px solid gray; display: flex; flex-direction: row; justify-content: center; align-items: center; margin: 10px;", "data-callback" => data_callback, **html_options) do [ tag.input(type: "hidden", name: "cf-turnstile-response", value: "mocked"), image_tag("turnstile-logo.svg"), diff --git a/spec/rails_cloudflare_turnstile/view_helpers_spec.rb b/spec/rails_cloudflare_turnstile/view_helpers_spec.rb index 7752cbd..c105879 100644 --- a/spec/rails_cloudflare_turnstile/view_helpers_spec.rb +++ b/spec/rails_cloudflare_turnstile/view_helpers_spec.rb @@ -62,6 +62,10 @@ def image_tag(name) it do expect(subject.cloudflare_turnstile(action: "an-action")).to eq "
" end + + it "passes through HTML options" do + expect(subject.cloudflare_turnstile(action: "an-action", data: {appearance: "interaction-only"})).to eq "
" + end end end