Skip to content

Commit

Permalink
Merge pull request #2415 from alphagov/filter-by-edition
Browse files Browse the repository at this point in the history
Add constraint to allow only Answer Edition and Help Page Edition
  • Loading branch information
syed-ali-tw authored Nov 13, 2024
2 parents b78361c + c6ca580 commit cfc8d34
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/constraints/allowed_content_types_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AllowedContentTypesConstraint
def initialize(allowed_content_types)
@allowed_content_types = allowed_content_types
end

def matches?(request)
request_path_parameters = "action_dispatch.request.path_parameters"
if request.env[request_path_parameters] && request.env[request_path_parameters][:id]
@allowed_content_types.include?(Edition.find(request.env[request_path_parameters][:id]).class)
end
end
end
5 changes: 5 additions & 0 deletions app/constraints/new_design_system_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NewDesignSystemConstraint
def matches?(request)
AllowedContentTypesConstraint.new([AnswerEdition, HelpPageEdition]).matches?(request) && FeatureConstraint.new("design_system_edit").matches?(request)
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

resources :artefacts, only: %i[new create update]

constraints FeatureConstraint.new("design_system_edit") do
constraints NewDesignSystemConstraint.new do
resources :editions do
member do
get "metadata"
Expand Down
41 changes: 41 additions & 0 deletions test/integration/routes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,45 @@ class RoutesTest < LegacyIntegrationTest
should "route to new downtimes controller new downtime" do
assert_routing("/editions/1/downtime/new", controller: "downtimes", action: "new", edition_id: "1")
end

# rubocop:disable Rails/SaveBang
context "new design system" do
setup do
@test_strategy = Flipflop::FeatureSet.current.test!
@test_strategy.switch!(:design_system_edit, true)
end

context "allowed content types" do
%i[answer_edition help_page_edition].each do |content_type|
context content_type do
setup do
@edition = FactoryBot.create(content_type)
end

should "route to editions controller" do
assert_routing("/editions/#{@edition.id}", controller: "editions", action: "show", id: @edition.id.to_s)
end
end
end
end

context "not allowed content types" do
%i[guide_edition local_transaction_edition completed_transaction_edition place_edition simple_smart_answer_edition transaction_edition].each do |content_type|
context content_type do
setup do
@edition = FactoryBot.create(content_type)
end

should "route to legacy editions controller" do
assert_routing("/editions/#{@edition.id}", controller: "legacy_editions", action: "show", id: @edition.id.to_s)
end
end
end
end

teardown do
@test_strategy.switch!(:design_system_edit, false)
end
end
# rubocop:enable Rails/SaveBang
end

0 comments on commit cfc8d34

Please sign in to comment.