content_block_tools 1.14.1 → 2.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: 838582c2538bbe70c10178123a0d45012cc374f311ab2c201248315bdc4a75ca
4
- data.tar.gz: 6df681c851529b676a3c99e9102eb650d14cb4264e6a19c9af090457f6f84faa
3
+ metadata.gz: 225bb65ced69b2b51cb081edfe13a9c6456747d64d3a1f7b561d58dc2c9aa47c
4
+ data.tar.gz: 58a80f5c4111e17f38a2eb1baffa6921fea0f7d405a69180423f5599b8473cc6
5
5
  SHA512:
6
- metadata.gz: 2811f4de394ea44cdad0a0b780b8933c6748d65a21b183f33134d1a94e3b8ab3ab1e423ac116ebda8cd84541abdf5487a73101e03612c5ba9b7e834570da592a
7
- data.tar.gz: 4e43661c293351a6e00038116ee6da74e51b43fe9ad6db0c1e5cf1f8b2e758439e537c5c8b73466490f219cdaf76083bcfb7f70aefa9561f78f9909a024dca47
6
+ metadata.gz: 4b9f52c49ce3fe7ee95fcde5c5973e8c8d76c70dbb740fd4f96e0c6a57806c526f82257c0a88f2b9b58c5d404c3835d3dc756c587d49ef0748d1dc9f9f18e96d
7
+ data.tar.gz: 197c2203d07a5bb9b16efe787a60d741b28e81eabd7156e5708d1939b363e6a012fff0cc94e2b34f85c9ae414d37c3df84c107a65e5ddab0b1f21f45d01c8037
data/README.md CHANGED
@@ -50,6 +50,20 @@ content_block.render
50
50
 
51
51
  For more information, see the [Documentation](https://www.rubydoc.info/github/alphagov/content_block_tools)
52
52
 
53
+ ## Development
54
+
55
+ ### Running the test suites
56
+
57
+ To run the unit tests:
58
+ ```
59
+ bundle exec rspec
60
+ ```
61
+
62
+ To run the feature tests:
63
+ ```
64
+ bundle exec cucumber
65
+ ```
66
+
53
67
  ## License
54
68
 
55
69
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -8,11 +8,10 @@ module ContentBlockTools
8
8
  def initialize(content_block:, _block_type: nil, _block_name: nil)
9
9
  @content_block = content_block
10
10
  validate_format!
11
- validate_presence_of_income_tax_rates! if tax_table_format?
12
11
  end
13
12
 
14
13
  def render
15
- return "" unless tax_table_format?
14
+ raise format_inapplicable_error unless tax_table_format? && able_to_format_as_table?
16
15
 
17
16
  Tax::TaxTableComponent.new(rates: income_tax_rates).render
18
17
  end
@@ -23,17 +22,18 @@ module ContentBlockTools
23
22
 
24
23
  delegate :format, :details, to: :content_block
25
24
 
25
+ def format_inapplicable_error
26
+ InvalidFormatError.new("Cannot render 'tax_table' format: missing income tax rates")
27
+ end
28
+
26
29
  def validate_format!
27
30
  return if SUPPORTED_FORMATS.include?(format)
28
31
 
29
32
  raise InvalidFormatError, "Unknown format '#{format}' for tax"
30
33
  end
31
34
 
32
- def validate_presence_of_income_tax_rates!
33
- return if income_tax_rates&.any?
34
-
35
- raise InvalidFormatError,
36
- "Cannot render 'tax_table' format: missing income tax rates"
35
+ def able_to_format_as_table?
36
+ income_tax_rates&.present?
37
37
  end
38
38
 
39
39
  def tax_table_format?
@@ -49,38 +49,6 @@ module ContentBlockTools
49
49
 
50
50
  attr_reader :content_id, :title, :embed_code
51
51
 
52
- # Creates a ContentBlock instance from an embed code string by fetching
53
- # the content item data from the Content Store API.
54
- #
55
- # @param embed_code [String] The embed code string to parse and fetch content for
56
- # @example
57
- # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}")
58
- #
59
- # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store
60
- #
61
- # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid
62
- # @raise [GdsApi::HTTPErrorResponse] if the API request fails
63
- #
64
- # @example Create a ContentBlock from an embed code
65
- # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}"
66
- # content_block = ContentBlock.from_embed_code(embed_code)
67
- # content_block.title #=> "Contact Email"
68
- # content_block.document_type #=> "email"
69
- #
70
- # @see ContentBlockReference.from_string
71
- # @see GdsApi.content_store
72
- def self.from_embed_code(embed_code)
73
- reference = ContentBlockReference.from_string(embed_code)
74
- api_response = GdsApi.content_store.content_item(reference.content_store_identifier)
75
- new(
76
- content_id: api_response["content_id"],
77
- title: api_response["title"],
78
- document_type: api_response["document_type"],
79
- details: api_response["details"],
80
- embed_code:,
81
- )
82
- end
83
-
84
52
  def initialize(content_id:, title:, document_type:, details:, embed_code:)
85
53
  @content_id = content_id
86
54
  @title = title
@@ -52,21 +52,6 @@ module ContentBlockTools
52
52
  !identifier.match?(UUID_REGEX)
53
53
  end
54
54
 
55
- # Returns the content store path for this content block reference
56
- #
57
- # Constructs a path used to identify and retrieve the content block from the content store.
58
- # The path follows the format `/content-blocks/{document_type}/{identifier}`.
59
- #
60
- # @example
61
- # reference = ContentBlockReference.new(document_type: "content_block_contact", identifier: "some-slug", embed_code: "...")
62
- # reference.content_store_identifier
63
- # #=> "/content-blocks/content_block_contact/some-slug"
64
- #
65
- # @return [String] the content store path for this content block
66
- def content_store_identifier
67
- "/content-blocks/#{document_type}/#{identifier}"
68
- end
69
-
70
55
  class << self
71
56
  # Finds all content block references within a document, using `ContentBlockReference::EMBED_REGEX`
72
57
  # to scan through the document
@@ -5,8 +5,8 @@ module ContentBlockTools
5
5
  # fields to render. The format is:
6
6
  # {{embed:block_type:identifier}}
7
7
  # {{embed:block_type:identifier/field1/nested_field2}}
8
- # {{embed:block_type:identifier|format}}
9
- # {{embed:block_type:identifier/field1|format}}
8
+ # {{embed:block_type:identifier#format}}
9
+ # {{embed:block_type:identifier/field1#format}}
10
10
  #
11
11
  # @example Basic embed code
12
12
  # embed_code = EmbedCode.new("{{embed:content_block_contact:main-office}}")
@@ -4,7 +4,7 @@ module ContentBlockTools
4
4
  module Pension
5
5
  class AmountPresenter < BasePresenter
6
6
  def render
7
- field.start_with?("£") ? field : "£#{field}"
7
+ "£#{field}"
8
8
  end
9
9
  end
10
10
  end
@@ -39,14 +39,18 @@ module ContentBlockTools
39
39
 
40
40
  attr_reader :content_block
41
41
 
42
+ def failure_message
43
+ "Unable to render block '#{content_block.title}' #{content_block.embed_code}"
44
+ end
45
+
42
46
  def base_tag
43
47
  rendering_block? ? :div : :span
44
48
  end
45
49
 
46
50
  def content
47
51
  internal_content_path.present? ? field_or_block_content : component.new(content_block:).render
48
- rescue UnknownComponentError
49
- content_block.title
52
+ rescue UnknownComponentError, InvalidFormatError
53
+ failure_message
50
54
  end
51
55
 
52
56
  def field_or_block_content
@@ -59,7 +63,7 @@ module ContentBlockTools
59
63
  render_hash_content(field_content)
60
64
  else
61
65
  log_content_not_found
62
- content_block.embed_code
66
+ failure_message
63
67
  end
64
68
  end
65
69
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentBlockTools
4
- VERSION = "1.14.1"
4
+ VERSION = "2.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: content_block_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
@@ -1117,14 +1117,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
1117
1117
  requirements:
1118
1118
  - - ">="
1119
1119
  - !ruby/object:Gem::Version
1120
- version: '3.2'
1120
+ version: '3.3'
1121
1121
  required_rubygems_version: !ruby/object:Gem::Requirement
1122
1122
  requirements:
1123
1123
  - - ">="
1124
1124
  - !ruby/object:Gem::Version
1125
1125
  version: '0'
1126
1126
  requirements: []
1127
- rubygems_version: 4.0.12
1127
+ rubygems_version: 4.0.18
1128
1128
  specification_version: 4
1129
1129
  summary: A suite of tools for working with GOV.UK Content Blocks
1130
1130
  test_files: []