Skip to content

Commit

Permalink
Merge pull request #4291 from alphagov/expose-schema-name-and-query
Browse files Browse the repository at this point in the history
Expose schema name and query
  • Loading branch information
KludgeKML authored Oct 16, 2024
2 parents 021f26c + 9de814b commit eec83a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ContentItem
attr_reader :content_store_response, :content_store_hash, :body, :image, :description, :document_type, :title, :base_path, :locale
attr_reader :content_store_response, :content_store_hash, :body, :image, :description,
:document_type, :schema, :title, :base_path, :locale

# SCAFFOLDING: remove the override_content_store_hash parameter when full landing page
# content items including block details are available from content-store
Expand All @@ -11,11 +12,26 @@ def initialize(content_store_response, override_content_store_hash: nil)
@image = content_store_hash.dig("details", "image")
@description = content_store_hash["description"]
@document_type = content_store_hash["document_type"]
@schema = content_store_hash["schema"]
@title = content_store_hash["title"]
@base_path = content_store_hash["base_path"]
@locale = content_store_hash["locale"]
end

alias_method :to_h, :content_store_hash
delegate :cache_control, to: :content_store_response

REGEX_IS_A = /is_an?_(.*)\?/

def respond_to_missing?(method_name, _include_private = false)
method_name.to_s =~ REGEX_IS_A ? true : super
end

def method_missing(method_name, *_args, &_block)
if method_name.to_s =~ REGEX_IS_A
schema == ::Regexp.last_match(1)
else
super
end
end
end
27 changes: 27 additions & 0 deletions spec/models/content_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
RSpec.describe ContentItem do
describe "#is_a_xxxx?" do
let(:subject) { described_class.new({ "schema" => "landing_page" }) }

it "returns true when called with the schema of the object" do
expect(subject.is_a_landing_page?).to be true
end

it "returns false when called with a mismatching schema" do
expect(subject.is_a_place?).to be false
end

it "also handles is_an_xxxx?" do
expect(subject.is_an_organisation?).to be false
end

it "still passes other missing methods to parent" do
expect { subject.was_a_landing_page? }.to raise_error(NoMethodError)
end

it "responds to the various methods" do
expect(subject.respond_to?(:is_a_landing_page?)).to be true
expect(subject.respond_to?(:is_an_organisation?)).to be true
expect(subject.respond_to?(:was_a_landing_page?)).to be false
end
end
end

0 comments on commit eec83a9

Please sign in to comment.