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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 196412eb6b781fae101b937a9f76b0aa5eaebf94be239466cf3a9edc3175356b
4
- data.tar.gz: 4077e06435e1c39254bd4b74aee2eaa7cb219cea37d8cbef8c37998e50f0075a
3
+ metadata.gz: b7a94bbf6a29069c0d4f5f291ca3e80d368942aad2e03f20ae7b64091f9d900e
4
+ data.tar.gz: 3156465de7ffac378b48b61250377536272acd9bd15998d61be263701fe92657
5
5
  SHA512:
6
- metadata.gz: 3e28b314dc36c83eaf191ecbe6a47e2477a7c80a54e7d791b5a3ee0d07c6f6617fbd3f06bb822c15cba75e3637b963c6524b40473eaa399932543f649abb85f1
7
- data.tar.gz: d28f2f512b7efc7bd387e410e6829053c6a7019d730b3fb7dfa5bd69d87c59dab7dd0816187a621d7c80533c67931ce3511bca42266804f39e15f4ad77368e7d
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
- [![Gem Version](https://img.shields.io/gem/v/cloudflare-turnstile-rails.svg)](https://rubygems.org/gems/cloudflare-turnstile-rails)
4
- [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
- [![Lint Status](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/lint.yml/badge.svg)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/lint.yml)
6
- [![Test Status](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/test.yml/badge.svg)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/test.yml)
3
+ [![Gem Version](https://img.shields.io/gem/v/cloudflare-turnstile-rails.svg?label=Gem&logo=rubygems&logoColor=white)](https://rubygems.org/gems/cloudflare-turnstile-rails)
4
+ [![Ruby](https://img.shields.io/badge/Ruby-2.6%20to%204.0-CC342D?logo=ruby&logoColor=white)](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/.github/workflows/test.yml)
5
+ [![Rails](https://img.shields.io/badge/Rails-5.0%20to%208.1-D30001?logo=rubyonrails&logoColor=white)](https://github.com/vkononov/cloudflare-turnstile-rails/blob/main/Appraisals)
6
+ [![Test Matrix](https://img.shields.io/github/actions/workflow/status/vkononov/cloudflare-turnstile-rails/test.yml?branch=main&label=Test%20Matrix&logo=github)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/test.yml)
7
+ [![Lint](https://img.shields.io/github/actions/workflow/status/vkononov/cloudflare-turnstile-rails/lint.yml?branch=main&label=Lint&logo=github)](https://github.com/vkononov/cloudflare-turnstile-rails/actions/workflows/lint.yml)
8
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](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 `Rails >= 5.0` with `Ruby >= 2.6.0`.
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
  [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](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! Though it is recommended to match your `theme` and `language` to your app's design and locale:
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: 'light', language: 'en' } %>
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
@@ -1,7 +1,7 @@
1
1
  module Cloudflare
2
2
  module Turnstile
3
3
  module Rails
4
- VERSION = '1.0.2'.freeze
4
+ VERSION = '1.1.0'.freeze
5
5
  end
6
6
  end
7
7
  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.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflare-turnstile-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Kononov