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

feat: support passing through arbitrary HTML options #137

Merged
merged 1 commit into from
Apr 26, 2024
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/rails_cloudflare_turnstile/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions spec/rails_cloudflare_turnstile/view_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def image_tag(name)
it do
expect(subject.cloudflare_turnstile(action: "an-action")).to eq "<div class=\"cloudflare-turnstile\"><div class=\"cf-turnstile\" data-sitekey=\"a_public_key\" data-size=\"regular\" data-action=\"an-action\" data-theme=\"auto\"></div></div>"
end

it "passes through HTML options" do
expect(subject.cloudflare_turnstile(action: "an-action", data: {appearance: "interaction-only"})).to eq "<div class=\"cloudflare-turnstile\"><div class=\"cf-turnstile\" data-sitekey=\"a_public_key\" data-size=\"regular\" data-action=\"an-action\" data-theme=\"auto\" data-appearance=\"interaction-only\"></div></div>"
end
end
end

Expand Down