Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move fatality notices from government frontend #4640

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/models/fatality_notice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class FatalityNotice < ContentItem
include Updatable

def field_of_operation
content_item_links = content_store_hash["links"]["field_of_operation"]
if content_item_links
attributes = content_item_links.first
OpenStruct.new(title: attributes["title"], path: attributes["base_path"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice! I think we should use this pattern in other places too.

end
end

def contributors
organisations_ordered_by_emphasis + links_group(%w[worldwide_organisations people speaker])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If worldwide_organisations are needed then the WorldwideOrganisations concern will need to be included.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do fatality notices have a "speaker"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how many content items need to access "people" in contributors. Perhaps people should be it's own model?

Do we need to start thinking about modelling Links? Not a linkable concern, but something that does the job of getting the title and base_path so we don't have to keep repeating that code?

end

private

def links_group(types)
types.flat_map { |type| links(type) }.uniq
end

def links(type)
expanded_links_from_content_item(type)
.select { |link| link["base_path"] }
.map { |link| { "title" => link["title"], "base_path" => link["base_path"] } }
end

def expanded_links_from_content_item(type)
return [] unless content_store_hash["links"][type]

content_store_hash["links"][type]
end
end
43 changes: 43 additions & 0 deletions spec/models/fatality_notice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe FatalityNotice do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests should also include the shared examples "it has updates" and "it has no updates"

let(:content_item) { GovukSchemas::Example.find("fatality_notice", example_name: "fatality_notice") }

describe "#field_of_operation" do
it "returns the field of operation link structure" do
model_instance = described_class.new(content_item)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick. I'd probably have called this content_item rather than model_instance as it's being set to an instance of ContentItem.


expect(model_instance.field_of_operation.title).to eq("Zululand")
expect(model_instance.field_of_operation.path).to eq("/government/fields-of-operation/zululand")
end

context "when no field of operation is present" do
it "returns nil" do
content_item["links"].delete("field_of_operation")
model_instance = described_class.new(content_item)

expect(model_instance.field_of_operation).to be_nil
end
end
end

describe "#contributors" do
it "returns the publishing organisation" do
model_instance = described_class.new(content_item)

expect(model_instance.contributors.count).to eq(1)
expect(model_instance.contributors[0]["title"]).to eq("Ministry of Defence")
expect(model_instance.contributors[0]["base_path"]).to eq("/government/organisations/ministry-of-defence")
end

context "when a minister is listed" do
let(:content_item) { GovukSchemas::Example.find("fatality_notice", example_name: "fatality_notice_with_minister") }

it "includes the minister" do
model_instance = described_class.new(content_item)

expect(model_instance.contributors.count).to eq(2)
expect(model_instance.contributors[1]["title"]).to eq("The Rt Hon Sir Eric Pickles MP")
expect(model_instance.contributors[1]["base_path"]).to eq("/government/people/eric-pickles")
end
end
end
end