diff --git a/app/controllers/world_controller.rb b/app/controllers/world_controller.rb index 218430489..2e7c3c175 100644 --- a/app/controllers/world_controller.rb +++ b/app/controllers/world_controller.rb @@ -1,13 +1,14 @@ class WorldController < ApplicationController def index - if Features.graphql_feature_enabled? || params.include?(:graphql) - index = WorldIndexGraphql.find!("/world") - @presented_index = WorldIndexGraphqlPresenter.new(index) + if params.include?(:graphql) + world_index = Graphql::WorldIndex.find!(request.path) + content_item_data = world_index.content_item else - index = WorldIndex.find!("/world") - @presented_index = WorldIndexPresenter.new(index) + world_index = WorldIndex.find!(request.path) + content_item_data = world_index.content_item.content_item_data end - setup_content_item_and_navigation_helpers(index) + @presented_index = WorldIndexPresenter.new(content_item_data) + setup_content_item_and_navigation_helpers(world_index) end end diff --git a/app/models/graphql/world_index.rb b/app/models/graphql/world_index.rb new file mode 100644 index 000000000..930217b40 --- /dev/null +++ b/app/models/graphql/world_index.rb @@ -0,0 +1,19 @@ +class Graphql::WorldIndex + attr_reader :content_item + + def initialize(content_item) + @content_item = content_item + end + + def self.find!(base_path) + query = Graphql::WorldIndexQuery.new(base_path).query + + # binding.pry + + edition = Services.publishing_api.graphql_query(query) + .dig("data", "edition") + .deep_transform_keys(&:underscore) + + new(edition) + end +end diff --git a/app/presenters/world_index_graphql_presenter.rb b/app/presenters/world_index_graphql_presenter.rb deleted file mode 100644 index d3f99485d..000000000 --- a/app/presenters/world_index_graphql_presenter.rb +++ /dev/null @@ -1,15 +0,0 @@ -class WorldIndexGraphqlPresenter < WorldIndexPresenter - def title - @world_index.content_item["title"] - end - - def international_delegations - @world_index.content_item["internationalDelegations"] - end - -private - - def world_locations - @world_index.content_item["worldLocations"] - end -end diff --git a/app/presenters/world_index_presenter.rb b/app/presenters/world_index_presenter.rb index f4cecc910..d3a99abe1 100644 --- a/app/presenters/world_index_presenter.rb +++ b/app/presenters/world_index_presenter.rb @@ -4,22 +4,22 @@ class WorldIndexPresenter delegate :count, to: :world_locations, prefix: true delegate :count, to: :international_delegations, prefix: true - def initialize(world_index) - @world_index = world_index + def initialize(content_item_data) + @content_item_data = content_item_data end def title - @world_index.content_item.title + @content_item_data.fetch("title") end def grouped_world_locations - world_locations + @content_item_data.dig("details", "world_locations") .group_by { |location| location_group(location) } .sort end def international_delegations - details["international_delegations"] + @content_item_data.dig("details", "international_delegations") end def world_location_link(world_location) @@ -45,10 +45,10 @@ def location_group(location) end def details - @world_index.content_item.details + @content_item_data.fetch("details") end def world_locations - details["world_locations"] + @content_item_data.dig("details", "world_locations") end end diff --git a/app/queries/graphql/world_index_query.rb b/app/queries/graphql/world_index_query.rb index 0c45052a8..d0e47d0af 100644 --- a/app/queries/graphql/world_index_query.rb +++ b/app/queries/graphql/world_index_query.rb @@ -16,12 +16,14 @@ def query ... on WorldIndex { title - worldLocations { - ...worldLocationInfo - } + details { + worldLocations { + ...worldLocationInfo + } - internationalDelegations { - ...worldLocationInfo + internationalDelegations { + ...worldLocationInfo + } } } } diff --git a/spec/presenters/world_index_graphql_presenter_spec.rb b/spec/presenters/world_index_graphql_presenter_spec.rb deleted file mode 100644 index 4de963628..000000000 --- a/spec/presenters/world_index_graphql_presenter_spec.rb +++ /dev/null @@ -1,95 +0,0 @@ -RSpec.describe WorldIndexGraphqlPresenter do - before do - fixture = fetch_graphql_fixture("world_index").dig("data", "edition") - content_item = WorldIndexGraphql.new(fixture) - @world_index_presenter ||= described_class.new(content_item) - end - - describe "#title" do - subject { @world_index_presenter.title } - - it "returns the title from the content item" do - expect(subject).to eq("Help and services around the world") - end - end - - describe "#grouped_world_locations" do - subject { @world_index_presenter.grouped_world_locations } - - it "groups world locations by letter" do - expect(subject.length).to be 25 # No locations beginning with `X` - expect(subject.first.first).to eq "A" - expect(subject.last.first).to eq "Z" - end - - it "disregards the `The` prefix of world location names when grouping" do - g_group = subject.find { |group| group.first == "G" } - g_group_locations = g_group.last - - expect(g_group_locations.pluck("name")).to include "The Gambia" - end - end - - describe "#world_locations_count" do - subject { @world_index_presenter.world_locations_count } - - it "returns the number of world locations" do - expect(subject).to be 229 - end - end - - describe "#world_location_link" do - subject { @world_index_presenter.world_location_link(location) } - - context "when the location is active" do - let(:location) do - { - "active": true, - "name": "Afghanistan", - "slug": "afghanistan", - }.with_indifferent_access - end - - it "returns a link to the location" do - expect(subject).to eq "Afghanistan" - end - end - - context "when the location is inactive" do - let(:location) do - { - "active": false, - "name": "United Kingdom", - "slug": "united-kingdom", - }.with_indifferent_access - end - - it "returns the location" do - expect(subject).to eq "United Kingdom" - end - end - end - - describe "#filter_terms" do - let(:location) do - { - "name": "United Kingdom", - "slug": "united-kingdom", - }.with_indifferent_access - end - - subject { @world_index_presenter.filter_terms(location) } - - it "returns the joined name and slug" do - expect(subject).to eq "united-kingdom United Kingdom" - end - end - - describe "#international_delegations_count" do - subject { @world_index_presenter.international_delegations_count } - - it "returns the number of international delegations" do - expect(subject).to be 10 - end - end -end diff --git a/spec/presenters/world_index_presenter_spec.rb b/spec/presenters/world_index_presenter_spec.rb index 0d13e068f..ef908f204 100644 --- a/spec/presenters/world_index_presenter_spec.rb +++ b/spec/presenters/world_index_presenter_spec.rb @@ -3,7 +3,7 @@ fixture = GovukSchemas::Example.find("world_index", example_name: "world_index") content_item = ContentItem.new(fixture) world_index = WorldIndex.new(content_item) - @world_index_presenter ||= described_class.new(world_index) + @world_index_presenter ||= described_class.new(world_index.content_item.content_item_data) end describe "#title" do