Skip to content

Commit

Permalink
Merge pull request #1057 from alphagov/add-scoped-constituencies
Browse files Browse the repository at this point in the history
Add a scoped endpoint for constituencies
  • Loading branch information
pixeltrix authored Oct 30, 2024
2 parents b9b3eac + 8b1132f commit fcd5361
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 3 deletions.
14 changes: 13 additions & 1 deletion app/controllers/constituencies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
class ConstituenciesController < ApplicationController
before_action :set_cors_headers, only: [:index], if: :json_request?
before_action :fetch_parliament, only: [:index]

def index
@constituencies = Constituency.current.by_ons_code
@constituencies = @parliament.constituencies.by_ons_code

respond_to do |format|
format.json
end
end

private

def fetch_parliament
case params[:period]
when Parliament::PERIOD_FORMAT
@parliament = Parliament.archived.find_by!(period: params[:period])
else
@parliament = Parliament.instance
end
end
end
4 changes: 3 additions & 1 deletion app/controllers/petitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def redirect_to_home_page_if_dissolved
end

def redirect_to_home_page_unless_opened
redirect_to home_url unless Parliament.opened?
unless json_request?
redirect_to home_url unless Parliament.opened?
end
end

def request_format
Expand Down
1 change: 1 addition & 0 deletions app/models/parliament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Parliament < ActiveRecord::Base
include ActiveSupport::NumberHelper

CUTOFF_DATE = Date.civil(2015, 5, 7)
PERIOD_FORMAT = /\A\d{4}-\d{4}\z/

has_many :petitions, inverse_of: :parliament, class_name: "Archived::Petition"
has_many :parliament_constituencies
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
constraints Site.constraints_for_public do
controller 'constituencies' do
get '/constituencies', action: 'index', as: :constituencies

constraints period: /\d{4}-\d{4}|current/ do
get '/parliaments/:period/constituencies', action: 'index', as: nil
end
end

controller 'parliaments' do
get '/parliaments', action: 'index', as: :parliaments
get '/parliaments/:period', action: 'show'
get '/parliaments/:period', action: 'show', constraints: { period: /\d{4}-\d{4}/ }
end

controller 'topics' do
Expand Down
32 changes: 32 additions & 0 deletions spec/controllers/constituencies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,36 @@
expect(response.headers["Access-Control-Allow-Headers"]).to eq("Origin, X-Requested-With, Content-Type, Accept")
end
end

describe "GET /parliaments/:period/constituencies.json" do
before do
FactoryBot.create(:parliament, :conservatives_2019)

get :index, format: "json", params: { period: "2019-2024" }
end

it "responds with 200 OK" do
expect(response.status).to eq(200)
end

it "assigns the @constituencies instance variable" do
expect(assigns[:constituencies]).not_to be_nil
end

it "renders the constituencies/index template" do
expect(response).to render_template("constituencies/index")
end

it "sets the Access-Control-Allow-Origin header to '*'" do
expect(response.headers["Access-Control-Allow-Origin"]).to eq("*")
end

it "sets the Access-Control-Allow-Methods header to 'GET'" do
expect(response.headers["Access-Control-Allow-Methods"]).to eq("GET")
end

it "sets the Access-Control-Allow-Headers header to 'Origin, X-Requested-With, Content-Type, Accept'" do
expect(response.headers["Access-Control-Allow-Headers"]).to eq("Origin, X-Requested-With, Content-Type, Accept")
end
end
end
5 changes: 5 additions & 0 deletions spec/controllers/petitions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@
get :index
expect(response).to redirect_to("https://petition.parliament.uk/")
end

it "doesn't redirect JSON requests" do
get :index, as: :json
expect(response).to be_successful
end
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,19 @@
trait :archived do
archived_at { 1.month.ago }
end

trait :conservatives_2019 do
government { "Conservatives" }
state_opening_at { "2019-12-19 11:30:00".in_time_zone }
opening_at { "2020-03-03 10:55:00".in_time_zone }
archived_at { "2024-10-21 09:00:00".in_time_zone }

dissolution_heading { "Parliament is dissolving" }
dissolution_message { "This means all petitions will close in 2 weeks" }
dissolved_heading { "Parliament is dissolved" }
dissolved_message { "All petitions are now closed" }
dissolution_at { "2024-05-26 20:40:00".in_time_zone }
end
end

factory :tag do
Expand Down

0 comments on commit fcd5361

Please sign in to comment.