content_block_tools 0.3.1 → 0.4.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: '0096d08b83ee59a496db42789c353d5066bb67fb578e5488f43db2763fc6bb98'
4
- data.tar.gz: 65f13b4c646f846aabb2fb04df55ea8ae1f9859946dfa547e4be9a9990e166a0
3
+ metadata.gz: 9d0b492495818ff1ae3a52098b3386bb9f5665fa2c8aca6b260d244305d9f8d6
4
+ data.tar.gz: 4c67d72795c6345aab7619d81213293c537a7d95bb7b8fa015f8c6d1d1ff6ca0
5
5
  SHA512:
6
- metadata.gz: 5bf713af8e94445479cd22a74262c8f83ce15c5c850ce14242a3398c717f8c6a74bcc7eb40ae9c1ea41095d232a03cef9a5d1e9430b6259d243f6e644de67e2a
7
- data.tar.gz: 3a7f1dc2923151056018c517d2adf8ea9c0be93254b6bf41d3fbb6f9111e46d925afc38f1d934cc5245aede7d827d842c2c5eb70e728fb2c4cdbcef6bec212f4
6
+ metadata.gz: 68ea93d11e33b68f51c0a4893d95266cf46fcf09ab5faaff2487a83ee40c4790045610f9d055a21da9bb7d59cf266b36c9b35b397f5bbec393b6bf9778e364db
7
+ data.tar.gz: 9dae479a604a0be374db8bd8267e62c231c00e4f2da39e20d7e60de9131d7580fe4035bd1518fe9b4a94ebb6f2ed25315dc86872b9241e2f76884cd3d539ab9e
data/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@
7
7
  useful summary for people upgrading their application, not a replication
8
8
  of the commit log.
9
9
 
10
+ ## 0.4.0
11
+
12
+ * BREAKING: allow support for field names in block's embed code. new ContentBlocks now require an embed code argument. (
13
+ [17]
14
+ (https://github.com/alphagov/govuk_content_block_tools/pull/17))
15
+
10
16
  ## 0.3.1
11
17
 
12
18
  * Support any Rails version `>= 6` ([#10](https://github.com/alphagov/govuk_content_block_tools/pull/10))
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_development_dependency "rake", "13.2.1"
29
29
  spec.add_development_dependency "rspec", "3.13.0"
30
- spec.add_development_dependency "rubocop-govuk", "5.0.2"
30
+ spec.add_development_dependency "rubocop-govuk", "5.0.3"
31
31
 
32
32
  spec.add_dependency "actionview", ">= 6"
33
33
  end
@@ -1,5 +1,5 @@
1
1
  module ContentBlockTools
2
- ContentBlock = Data.define(:content_id, :title, :document_type, :details)
2
+ ContentBlock = Data.define(:content_id, :title, :document_type, :details, :embed_code)
3
3
 
4
4
  # Defines a Content Block
5
5
  #
@@ -33,10 +33,18 @@ module ContentBlockTools
33
33
  # content_block.details #=> { email_address: "foo@example.com" }
34
34
  # @return [Hash] the details
35
35
  # @api public
36
+ #
37
+ # @!attribute [r] embed_code
38
+ # The embed_code used for a block containing optional field name
39
+ # @example
40
+ # content_block_reference.embed_code #=> "{{embed:content_block_email_address:2b92cade-549c-4449-9796-e7a3957f3a86}}"
41
+ # content_block_reference.embed_code #=> "{{embed:content_block_postal_address:2b92cade-549c-4449-9796-e7a3957f3a86/field_name}}"
42
+ # @return [String]
36
43
  class ContentBlock < Data
37
44
  # A lookup of presenters for particular content block types
38
45
  CONTENT_PRESENTERS = {
39
46
  "content_block_email_address" => ContentBlockTools::Presenters::EmailAddressPresenter,
47
+ "content_block_postal_address" => ContentBlockTools::Presenters::PostalAddressPresenter,
40
48
  }.freeze
41
49
 
42
50
  # Calls the appropriate presenter class to return a HTML representation of a content
@@ -26,11 +26,13 @@ module ContentBlockTools
26
26
  # @return [String]
27
27
  class ContentBlockReference < Data
28
28
  # An array of the supported document types
29
- SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address].freeze
29
+ SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address content_block_postal_address].freeze
30
30
  # The regex used to find UUIDs
31
31
  UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
32
+ # The regex to find optional field names after the UUID, begins with '/'
33
+ FIELD_REGEX = /(\/.*)?/
32
34
  # The regex used when scanning a document using {ContentBlockTools::ContentBlockReference.find_all_in_document}
33
- EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}}})/
35
+ EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}#{FIELD_REGEX}}})/
34
36
 
35
37
  class << self
36
38
  # Finds all content block references within a document, using `ContentBlockReference::EMBED_REGEX`
@@ -29,21 +29,43 @@ module ContentBlockTools
29
29
  content_block: "",
30
30
  document_type: content_block.document_type,
31
31
  content_id: content_block.content_id,
32
+ field_names: field_names&.join(","),
32
33
  },
33
34
  )
34
35
  end
35
36
 
36
37
  private
37
38
 
39
+ attr_reader :content_block
40
+
38
41
  # The default representation of the content block - this can be overridden in a subclass
42
+ # by overriding the content, default_content or content_for_fields methods
39
43
  #
40
44
  # @return [string] A representation of the content block to be wrapped in the base_tag in
41
45
  # {#content}
42
46
  def content
47
+ if field_names.present?
48
+ content_for_fields
49
+ else
50
+ default_content
51
+ end
52
+ end
53
+
54
+ def default_content
43
55
  content_block.title
44
56
  end
45
57
 
46
- attr_reader :content_block
58
+ def content_for_fields
59
+ content_block.details.dig(*field_names)
60
+ end
61
+
62
+ def field_names
63
+ embed_code_match = ContentBlockReference::EMBED_REGEX.match(content_block.embed_code)
64
+ if embed_code_match.present?
65
+ all_fields = embed_code_match[4]&.reverse&.chomp("/")&.reverse
66
+ all_fields&.split("/")&.map(&:to_sym)
67
+ end
68
+ end
47
69
  end
48
70
  end
49
71
  end
@@ -0,0 +1,11 @@
1
+ module ContentBlockTools
2
+ module Presenters
3
+ class PostalAddressPresenter < BasePresenter
4
+ private
5
+
6
+ def default_content
7
+ "#{content_block.details[:line_1]}, #{content_block.details[:town_or_city]}, #{content_block.details[:postcode]}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentBlockTools
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -5,6 +5,7 @@ require "uri"
5
5
 
6
6
  require "content_block_tools/presenters/base_presenter"
7
7
  require "content_block_tools/presenters/email_address_presenter"
8
+ require "content_block_tools/presenters/postal_address_presenter"
8
9
 
9
10
  require "content_block_tools/content_block"
10
11
  require "content_block_tools/content_block_reference"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: content_block_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-25 00:00:00.000000000 Z
10
+ date: 2025-02-04 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake
@@ -44,14 +43,14 @@ dependencies:
44
43
  requirements:
45
44
  - - '='
46
45
  - !ruby/object:Gem::Version
47
- version: 5.0.2
46
+ version: 5.0.3
48
47
  type: :development
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
51
  - - '='
53
52
  - !ruby/object:Gem::Version
54
- version: 5.0.2
53
+ version: 5.0.3
55
54
  - !ruby/object:Gem::Dependency
56
55
  name: actionview
57
56
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +65,6 @@ dependencies:
66
65
  - - ">="
67
66
  - !ruby/object:Gem::Version
68
67
  version: '6'
69
- description:
70
68
  email:
71
69
  - govuk-dev@digital.cabinet-office.gov.uk
72
70
  executables: []
@@ -94,12 +92,12 @@ files:
94
92
  - lib/content_block_tools/content_block_reference.rb
95
93
  - lib/content_block_tools/presenters/base_presenter.rb
96
94
  - lib/content_block_tools/presenters/email_address_presenter.rb
95
+ - lib/content_block_tools/presenters/postal_address_presenter.rb
97
96
  - lib/content_block_tools/version.rb
98
97
  homepage: https://github.com/alphagov/content_block_tools
99
98
  licenses:
100
99
  - MIT
101
100
  metadata: {}
102
- post_install_message:
103
101
  rdoc_options: []
104
102
  require_paths:
105
103
  - lib
@@ -114,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
112
  - !ruby/object:Gem::Version
115
113
  version: '0'
116
114
  requirements: []
117
- rubygems_version: 3.5.23
118
- signing_key:
115
+ rubygems_version: 3.6.3
119
116
  specification_version: 4
120
117
  summary: A suite of tools for working with GOV.UK Content Blocks
121
118
  test_files: []