katalyst-google-apis 1.3.1 → 1.3.2

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: 7b20f30239ff09170beb094e155676df9da9a96f7693d90aed8bac756b693e77
4
- data.tar.gz: d3ab4d196dacb58ed7c1c9b2a04715bd08f6c8871918bc1359c5054839e923d4
3
+ metadata.gz: 91607481f87f73f9f4e17f687e6f9f98c6005ec7b25b90f2e16f3a96dd2f8286
4
+ data.tar.gz: 9663f5605e940f362fb2b7e9d7294890960c6666dfe0ae595e83c700c5e559be
5
5
  SHA512:
6
- metadata.gz: afb1e05bc0bdea960d8a572da3d5448fea590830440f97f753cc99ac2361109268b502f8d90375db938657e201e892bc02312d8f8cbfae624e85124197838a4e
7
- data.tar.gz: 69fcfce31104540742fd9c23eb8da530695f3eef9137d80ae2aab06340c67f27196dcaad73d5caac374b58e28e93b455e811f5f0345fee674fa3503bde848c62
6
+ metadata.gz: 77b2105dd910de97aaf9c0eb80999d76f3e0f972bd9e8b29699c8daf33504166ceef279f07c601dd8ea19e6337d642a8e439ee50b55dc816251e617f4a1cc735
7
+ data.tar.gz: a2d8a5ed4d6f19fcf58380084400c355b1a013b71aa62e5eb5964b2a7fe32158b0e41a8233b0a8f718120d16cdade6ba4df856cbfdccea782cacc75d9d4329d9
@@ -2,28 +2,33 @@
2
2
 
3
3
  module Katalyst
4
4
  module GoogleApis
5
+ # rubocop:disable Rails/HelperInstanceVariable
5
6
  module FormBuilder
6
- def recaptcha_field(attribute = :recaptcha_token, action: object_name,
7
- site_key: GoogleApis.config.recaptcha.site_key)
8
- safe_join([
9
- RecaptchaField.new(action:, attribute:, site_key:).render(self),
10
- hidden_field(attribute),
11
- ])
7
+ ##
8
+ # :method: recaptcha_field
9
+ #
10
+ # :call-seq: recaptcha_field(method = :recaptcha_token, options = {})
11
+ #
12
+ # <%= form_with model: @contact do |f| %>
13
+ # <%= f.recaptcha_field %>
14
+ # <% end %>
15
+ #
16
+ # Please refer to the documentation of the base helper for details.
17
+ def recaptcha_field(attribute = :recaptcha_token, site_key: GoogleApis.config.recaptcha.site_key, **)
18
+ RecaptchaField.new(@object_name, attribute, site_key:, **).render(@template)
12
19
  end
13
20
 
14
21
  class RecaptchaField
15
- attr_reader :action, :attribute, :site_key
16
-
17
- def initialize(action:, attribute:, site_key:)
18
- # rubocop:disable Rails/HelperInstanceVariable
19
- @action = action
20
- @attribute = attribute
21
- @site_key = site_key
22
- # rubocop:enable Rails/HelperInstanceVariable
22
+ def initialize(object_name, attribute, site_key:, **html_attributes)
23
+ @object_name = object_name
24
+ @attribute = attribute
25
+ @site_key = site_key
26
+ @html_attributes = html_attributes
23
27
  end
24
28
 
25
29
  def render(template)
26
- template.tag.div(data:)
30
+ template.safe_join([template.tag.div(**@html_attributes, data:),
31
+ template.hidden_field(@object_name, @attribute)])
27
32
  end
28
33
 
29
34
  private
@@ -32,11 +37,12 @@ module Katalyst
32
37
  {
33
38
  controller: "recaptcha",
34
39
  action: "turbo:before-morph-element->recaptcha#morph",
35
- recaptcha_action_value: action,
36
- recaptcha_site_key_value: site_key,
40
+ recaptcha_action_value: @object_name,
41
+ recaptcha_site_key_value: @site_key,
37
42
  }
38
43
  end
39
44
  end
40
45
  end
46
+ # rubocop:enable Rails/HelperInstanceVariable
41
47
  end
42
48
  end
@@ -3,13 +3,73 @@
3
3
  module Katalyst
4
4
  module GoogleApis
5
5
  module GOVUKFormBuilder
6
- def govuk_recaptcha_field(attribute = :recaptcha_token, **)
7
- GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(self, object_name, attribute).html do
6
+ # Generates a Google recaptcha input that shows errors.
7
+ #
8
+ # @param attribute_name [Symbol] The name of the attribute
9
+ # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
10
+ # @param form_group [Hash] configures the form group
11
+ # @option form_group kwargs [Hash] additional attributes added to the form group
12
+ # @param before_input [String,Proc] the content injected before the input. No content will be added if left +nil+
13
+ # @param after_input [String,Proc] the content injected after the input. No content will be added if left +nil+
14
+ # @return [ActiveSupport::SafeBuffer] HTML output
15
+ #
16
+ # @example A google recaptcha input
17
+ # = f.govuk_recaptcha_field
18
+ #
19
+ def govuk_recaptcha_field(attribute_name = :recaptcha_token,
20
+ form_group: {}, before_input: nil, after_input: nil, **)
21
+ Recaptcha.new(self, object_name, attribute_name, form_group:, before_input:, after_input:, **).html
22
+ end
23
+
24
+ class Recaptcha < GOVUKDesignSystemFormBuilder::Base
25
+ include GOVUKDesignSystemFormBuilder::Traits::Error
26
+ include GOVUKDesignSystemFormBuilder::Traits::HTMLAttributes
27
+ include GOVUKDesignSystemFormBuilder::Traits::ContentBeforeAndAfter
28
+
29
+ def initialize(builder, object_name, attribute_name, form_group:, before_input:, after_input:, **kwargs)
30
+ super(builder, object_name, attribute_name)
31
+
32
+ @html_attributes = kwargs
33
+ @form_group = form_group
34
+ @before_input = before_input
35
+ @after_input = after_input
36
+ end
37
+
38
+ def html
39
+ GOVUKDesignSystemFormBuilder::Containers::FormGroup.new(*bound, **@form_group).html do
40
+ safe_join([error_element, content])
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def content
8
47
  safe_join([
9
- GOVUKDesignSystemFormBuilder::Elements::ErrorMessage.new(self, object_name, attribute),
10
- recaptcha_field(attribute, **),
48
+ before_input_content,
49
+ input,
50
+ after_input_content,
11
51
  ])
12
52
  end
53
+
54
+ def input
55
+ @builder.send(:recaptcha_field, @attribute_name, **attributes(@html_attributes))
56
+ end
57
+
58
+ def options
59
+ {
60
+ id: field_id(link_errors: true),
61
+ class: classes,
62
+ aria: { describedby: combine_references(error_id) },
63
+ }
64
+ end
65
+
66
+ def classes
67
+ [%(#{brand}-recaptcha)].push(error_classes).compact
68
+ end
69
+
70
+ def error_classes
71
+ %(#{brand}-recaptcha--error) if has_errors?
72
+ end
13
73
  end
14
74
  end
15
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-google-apis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive