cloudflare-turnstile-rails 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +34 -7
- data/lib/cloudflare/turnstile/rails/configuration.rb +2 -1
- data/lib/cloudflare/turnstile/rails/helpers.rb +11 -1
- data/lib/cloudflare/turnstile/rails/version.rb +1 -1
- data/lib/generators/cloudflare_turnstile/templates/cloudflare_turnstile.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b7a94bbf6a29069c0d4f5f291ca3e80d368942aad2e03f20ae7b64091f9d900e
|
|
4
|
+
data.tar.gz: 3156465de7ffac378b48b61250377536272acd9bd15998d61be263701fe92657
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e7304843b0657e901e07f3264e6ab6676914af2b9d6a8f38a0778b53587cff1ba7454c71793fe45cac69bb7741e8264e3495337fb4544bd5f88551e569847a5d
|
|
7
|
+
data.tar.gz: 808592cee1ae30ea1a6c19a9887bfe62d33979b991810bb078bec618a0e2e37ec4a93152d7f5552c4786245a25f4497e2ab7f0cdf925c387f0e5588468cb4a52
|
data/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# Cloudflare Turnstile Rails
|
|
2
2
|
|
|
3
|
-
[](https://rubygems.org/gems/cloudflare-turnstile-rails)
|
|
4
|
-
[](https://rubygems.org/gems/cloudflare-turnstile-rails)
|
|
4
|
+
[](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/.github/workflows/test.yml)
|
|
5
|
+
[](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/Appraisals)
|
|
6
|
+
[](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/test.yml)
|
|
7
|
+
[](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/lint.yml)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
9
|
|
|
8
10
|
Cloudflare Turnstile gem for Ruby on Rails with built-in Turbo and Turbolinks support and CSP compliance.
|
|
9
11
|
|
|
10
|
-
Supports
|
|
12
|
+
Supports **Rails 5.0 → latest** and **Ruby 2.6 → latest**, with the full Rails/Ruby matrix tested daily in CI.
|
|
11
13
|
|
|
12
14
|
[](https://www.buymeacoffee.com/vkononov)
|
|
13
15
|
|
|
@@ -87,10 +89,35 @@ Supports `Rails >= 5.0` with `Ruby >= 2.6.0`.
|
|
|
87
89
|
<%= cloudflare_turnstile_tag %>
|
|
88
90
|
```
|
|
89
91
|
|
|
90
|
-
That's it!
|
|
92
|
+
That's it! By default the widget auto-detects the visitor's `theme` and `language`.
|
|
93
|
+
|
|
94
|
+
* To match your app's design and locale, set defaults once in the initializer and they'll apply to every `cloudflare_turnstile_tag` automatically:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Cloudflare::Turnstile::Rails.configure do |config|
|
|
98
|
+
config.default_data = {
|
|
99
|
+
theme: 'light',
|
|
100
|
+
language: 'en'
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Values may also be a proc, evaluated at render time — useful for following the current locale:
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
config.default_data = { language: -> { I18n.locale } }
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
* You can still override any default (or add extra options) on an individual tag via the `data:` option. Per-tag values take precedence over the configured defaults:
|
|
112
|
+
|
|
113
|
+
```erb
|
|
114
|
+
<%= cloudflare_turnstile_tag data: { theme: 'dark' } %>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
* To drop a configured default on a single tag (so its attribute isn't rendered at all), pass `nil` for that key:
|
|
91
118
|
|
|
92
119
|
```erb
|
|
93
|
-
<%= cloudflare_turnstile_tag data: { theme:
|
|
120
|
+
<%= cloudflare_turnstile_tag data: { theme: nil } %>
|
|
94
121
|
```
|
|
95
122
|
|
|
96
123
|
* For all available **data-**\* options (e.g., `action`, `cdata`, `theme`, etc.), refer to the official Cloudflare client-side rendering docs:
|
|
@@ -3,7 +3,7 @@ module Cloudflare
|
|
|
3
3
|
module Rails
|
|
4
4
|
class Configuration
|
|
5
5
|
attr_writer :script_url
|
|
6
|
-
attr_accessor :site_key, :secret_key, :render, :onload, :auto_populate_response_in_test_env
|
|
6
|
+
attr_accessor :site_key, :secret_key, :render, :onload, :default_data, :auto_populate_response_in_test_env
|
|
7
7
|
|
|
8
8
|
def initialize
|
|
9
9
|
@script_url = Cloudflare::SCRIPT_URL
|
|
@@ -11,6 +11,7 @@ module Cloudflare
|
|
|
11
11
|
@secret_key = nil
|
|
12
12
|
@render = nil
|
|
13
13
|
@onload = nil
|
|
14
|
+
@default_data = {}
|
|
14
15
|
@auto_populate_response_in_test_env = true
|
|
15
16
|
end
|
|
16
17
|
|
|
@@ -7,7 +7,7 @@ module Cloudflare
|
|
|
7
7
|
def cloudflare_turnstile_tag(site_key: nil, include_script: true, **html_options) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
8
8
|
site_key ||= Rails.configuration.site_key
|
|
9
9
|
html_options[:class] = Cloudflare::WIDGET_CLASS unless html_options.key?(:class)
|
|
10
|
-
html_options[:data]
|
|
10
|
+
html_options[:data] = cloudflare_turnstile_default_data.merge(html_options[:data] || {})
|
|
11
11
|
html_options[:data][:sitekey] ||= site_key
|
|
12
12
|
|
|
13
13
|
script_tag = nil
|
|
@@ -27,6 +27,16 @@ module Cloudflare
|
|
|
27
27
|
widget = content_tag(:div, '', html_options)
|
|
28
28
|
safe_join([script_tag, widget].compact, "\n")
|
|
29
29
|
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# Resolves the configured default data attributes, evaluating any
|
|
34
|
+
# callable values (e.g. a proc bound to I18n.locale) at render time.
|
|
35
|
+
def cloudflare_turnstile_default_data
|
|
36
|
+
(Rails.configuration.default_data || {}).transform_values do |value|
|
|
37
|
+
value.respond_to?(:call) ? value.call : value
|
|
38
|
+
end
|
|
39
|
+
end
|
|
30
40
|
end
|
|
31
41
|
end
|
|
32
42
|
end
|
|
@@ -16,6 +16,15 @@ Cloudflare::Turnstile::Rails.configure do |config|
|
|
|
16
16
|
# config.render = 'explicit'
|
|
17
17
|
# config.onload = 'onloadTurnstileCallback'
|
|
18
18
|
|
|
19
|
+
# Optional: Default data-* attributes applied to every `cloudflare_turnstile_tag`.
|
|
20
|
+
# These are merged into each widget and can be overridden per tag via the `data:` option.
|
|
21
|
+
# See https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configuration-options
|
|
22
|
+
# Values may be a proc, evaluated at render time (e.g. to follow the current locale).
|
|
23
|
+
# config.default_data = {
|
|
24
|
+
# theme: 'auto',
|
|
25
|
+
# language: -> { I18n.locale }
|
|
26
|
+
# }
|
|
27
|
+
|
|
19
28
|
# In the Rails Test environment, automatically fill in a dummy response if none was provided.
|
|
20
29
|
# This lets you keep existing controller tests without having to add
|
|
21
30
|
# params["cf-turnstile-response"] manually in every test.
|