Skip to content

Commit

Permalink
Merge pull request #101 from co-cddo/add-agreements-api
Browse files Browse the repository at this point in the history
Add agreements API
  • Loading branch information
RobNicholsGDS authored Nov 11, 2024
2 parents dbfc198 + c36d964 commit 6b37a2a
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ gem "pagy", "~> 6.0"
# HTTP client
gem "faraday"

# API
gem "oj" # JSON parser
gem "rabl"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[mri mingw x64_mingw]
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ GEM
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
bigdecimal (3.1.8)
bindex (0.8.1)
bootsnap (1.16.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -149,6 +150,10 @@ GEM
racc (~> 1.4)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
oj (3.16.7)
bigdecimal (>= 3.0)
ostruct (>= 0.2)
ostruct (0.6.0)
pagy (6.5.0)
parallel (1.23.0)
parser (3.2.2.1)
Expand All @@ -165,6 +170,8 @@ GEM
public_suffix (5.0.1)
puma (5.6.8)
nio4r (~> 2.0)
rabl (0.16.1)
activesupport (>= 2.3.14)
racc (1.7.3)
rack (2.2.8.1)
rack-test (2.1.0)
Expand Down Expand Up @@ -296,11 +303,13 @@ DEPENDENCIES
govuk-components
govuk_design_system_formbuilder
jsbundling-rails
oj
pagy (~> 6.0)
pg (~> 1.1)
pg_search
propshaft
puma (~> 5.6)
rabl
rails (~> 7.0.8)
rspec-rails (~> 6.0.0)
rubocop-govuk
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/agreements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def index
end

def show
@agreement = Agreement.find(params[:id])
@agreement = Agreement.find_by(record_id: params[:id])
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/models/agreement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def self.find_by_id!(id)
def id_and_name
[fields["id"], name].select(&:present?).join(" - ")
end

def to_param
record_id
end
end
29 changes: 29 additions & 0 deletions app/views/agreements/index.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
object false
child(@agreements => :agreements) do |_agreement|
node(:data) do |agreement|
{
id: agreement.record_id,
name: agreement.name,
}
end
node(:link) do |agreement|
Rails.application.routes.url_helpers.agreement_url(agreement, format: :json)
end
end
node(:links) do
hash = {
page_size: @pagy.items,
current_page: @pagy.page,
}
hash[:next] = Rails.application.routes.url_helpers.agreements_url(page: @pagy.next, format: :json) if @pagy.next
hash[:previous] = Rails.application.routes.url_helpers.agreements_url(page: @pagy.prev, format: :json) if @pagy.prev
hash
end

# collection @agreements => :agreements
# node(:data) do |agreement|
# { name: agreement.name }
# end
# node(:link) do |agreement|
# Rails.application.routes.url_helpers.agreement_url(agreement, format: :json)
# end
22 changes: 22 additions & 0 deletions app/views/agreements/show.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
object @agreement
node(:data) do |agreement|
agreement.fields.except("agreement_name")
end
node(:links) do
{ index: Rails.application.routes.url_helpers.agreements_url(format: :json) }
end
child control_people: :controllers do
node(:data) do |control_person|
{ name: control_person.name }
end
end
child powers: :powers do
node(:data) do |power|
{ name: power.name }
end
end
child processors: :processors do
node(:data) do |processor|
{ name: processor.name }
end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "active_support/core_ext/integer/time"

Rails.application.default_url_options = { host: "localhost", port: 3000 }

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
2 changes: 2 additions & 0 deletions config/environments/heroku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "active_support/core_ext/integer/time"

Rails.application.default_url_options = { host: "www.digital-economy-act-register.data.gov.uk", protocol: :https }

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "active_support/core_ext/integer/time"

Rails.application.default_url_options = { host: "www.digital-economy-act-register.data.gov.uk", protocol: :https }

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!

Rails.application.default_url_options = { host: "localhost", port: 3000 }

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
root "agreements#index"

resources :agreements, only: [:show]
resources :agreements, only: %i[show index], constraints: { format: :json }
resources :powers, only: %i[index show]
resources :processors, only: %i[index show]
resources :control_people, only: %i[index show], path: :controllers
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/agreements.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FactoryBot.define do
factory :agreement do
name { Faker::Name.name }
record_id { rand(1..100) }
sequence :fields do |n|
{ name:, id: n }
sequence :record_id
fields do
{ name:, id: record_id }
end
end
end
62 changes: 61 additions & 1 deletion spec/requests/agreements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,67 @@

it "displays agreement fields" do
get agreement_path(agreement)
expect(response.body).to include(fields["purpose"])
expect(response.body).to include(escape_html(fields["purpose"]))
end
end

let(:json) { JSON.parse(response.body) }

describe "GET /agreements.json index" do
it "returns http success" do
get agreements_path(format: :json), as: :json
expect(response).to have_http_status(:success)
end

it "has agreements root with a JSON object" do
get agreements_path(format: :json), as: :json
expect(json.keys).to include("agreements")
end

it "displays agreement name" do
get agreements_path(format: :json), as: :json
expect(response.body).to include(escape_html(agreement.name))
end

it "displays path to agreement json show page" do
get agreements_path(format: :json), as: :json
expect(response.body).to include(agreement_path(agreement, format: :json))
end

it "show no links when only one agreement" do
get agreements_path(format: :json), as: :json
expect(json.dig("links", "next")).to be_blank
expect(json.dig("links", "previous")).to be_blank
end

context "when pages of data" do
let!(:agreements) { create_list :agreement, 30 }
it "display link to next page" do
get agreements_path(format: :json), as: :json
expect(json.dig("links", "next")).to include(agreements_path(format: :json, page: 2))
end

it "displays link to previous page after first page" do
get agreements_path(format: :json, page: 2), as: :json
expect(json.dig("links", "previous")).to include(agreements_path(format: :json))
end
end
end

describe "GET /agreements/:id.json show" do
it "returns http success" do
get agreement_path(agreement, format: :json), as: :json
expect(response).to have_http_status(:success)
end

it "displays data in fields" do
get agreement_path(agreement, format: :json), as: :json
expect(json.dig("agreement", "data")).to eq(agreement.fields)
end

it "includes the index json view path" do
get agreement_path(agreement, format: :json), as: :json
expect(json.dig("agreement", "links", "index")).to include(agreements_path(format: :json))
end
end
end
4 changes: 2 additions & 2 deletions spec/requests/control_people_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

it "displays control person" do
get control_person_path(control_person)
expect(response.body).to include(control_person.name)
expect(response.body).to include(escape_html(control_person.name))
end

context "when power present" do
Expand All @@ -54,7 +54,7 @@

it "displays control person" do
get control_person_path(control_person)
expect(response.body).to include(control_person.name)
expect(response.body).to include(escape_html(control_person.name))
end

it "displays a link to the power" do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/processors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

it "displays processor name" do
get processor_path(processor)
expect(response.body).to include(processor.name)
expect(response.body).to include(escape_html(processor.name))
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/update_logs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

it "displays log" do
get update_logs_path
expect(response.body).to include(update_log.comment)
expect(response.body).to include(escape_html(update_log.comment))
end
end
end

0 comments on commit 6b37a2a

Please sign in to comment.