Skip to content

Commit

Permalink
Merge pull request #710 from trade-tariff/GL-382-exemption-list-ui
Browse files Browse the repository at this point in the history
GL-382: Exemption list UI
  • Loading branch information
rasikasri authored Jun 12, 2024
2 parents 8523fbe + 1ee6baa commit 7b34d8f
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 12 deletions.
11 changes: 11 additions & 0 deletions app/controllers/concerns/xi_only.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module XiOnly
extend ActiveSupport::Concern

included do
def check_service
if TradeTariffAdmin::ServiceChooser.uk?
raise ActionController::RoutingError, 'Invalid service'
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module GreenLanes
class CategoryAssessmentsController < AuthenticatedController
include XiOnly

before_action :disable_service_switching!
before_action :check_service
def index
Expand Down Expand Up @@ -56,11 +58,5 @@ def ca_params
:theme_id,
)
end

def check_service
if TradeTariffAdmin::ServiceChooser.uk?
raise ActionController::RoutingError, 'Invalid service'
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module GreenLanes
class ExemptingCertificateOverridesController < AuthenticatedController
include XiOnly

before_action :disable_service_switching!
before_action :check_service
def index
Expand Down Expand Up @@ -35,11 +37,5 @@ def eco_params
:certificate_code,
)
end

def check_service
if TradeTariffAdmin::ServiceChooser.uk?
raise ActionController::RoutingError, 'Invalid service'
end
end
end
end
56 changes: 56 additions & 0 deletions app/controllers/green_lanes/exemptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module GreenLanes
class ExemptionsController < AuthenticatedController
include XiOnly

before_action :disable_service_switching!
before_action :check_service
def index
@exemptions = GreenLanes::Exemption.all.fetch
end

def new
@exemption = GreenLanes::Exemption.new
end

def create
@exemption = GreenLanes::Exemption.new(ex_params)

if @exemption.valid? && @exemption.save
redirect_to green_lanes_exemptions_path, notice: 'Exemption created'
else
render :new
end
end

def edit
@exemption = GreenLanes::Exemption.find(params[:id])
end

def update
@exemption = GreenLanes::Exemption.find(params[:id])
@exemption.attributes = ex_params

if @exemption.valid? && @exemption.save
redirect_to green_lanes_exemptions_path, notice: 'Exemption updated'
else
render :edit
end
end

def destroy
@exemption = GreenLanes::Exemption.find(params[:id])
@exemption.destroy

redirect_to green_lanes_exemptions_path, notice: 'Exemption removed'
end

private

def ex_params
params.require(:exemption).permit(
:code,
:description,
)
end
end
end
13 changes: 13 additions & 0 deletions app/models/green_lanes/exemption.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module GreenLanes
class Exemption
include Her::JsonApi::Model
use_api Her::XI_API

attributes :code,
:description,
:created_at,
:updated_at

collection_path '/admin/green_lanes/exemptions'
end
end
13 changes: 13 additions & 0 deletions app/views/green_lanes/exemptions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= govuk_form_for exemption, as: :exemption do |f| %>

<%= f.govuk_text_field :code,
label: { text: 'Code' },
width: 'one-half' %>

<%= f.govuk_text_field :description,
label: { text: 'Description' },
width: 'one-half' %>

<%= submit_and_back_buttons f, green_lanes_exemptions_path %>
<% end %>

21 changes: 21 additions & 0 deletions app/views/green_lanes/exemptions/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= govuk_breadcrumbs 'Exemptions': green_lanes_exemptions_path %>

<h2>Exemption</h2>

<%= render 'form', exemption: @exemption %>

<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">

<h3>
Remove Exemption
</h3>

<p>
Remove this Exemption
</p>

<%= link_to 'Remove',
green_lanes_exemption_path(@exemption),
method: :delete,
class: 'govuk-button govuk-button--warning',
data: { confirm: "Are you sure?", disable: 'Working ...' } %>
34 changes: 34 additions & 0 deletions app/views/green_lanes/exemptions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h2>
Manage Green Lanes Exemptions
</h2>

<%= link_to 'Add a Exemption', new_green_lanes_exemption_path, class: 'govuk-button' %>

<% if @exemptions.any? %>
<table>
<thead>
<tr>
<th>Code</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% @exemptions.each do |ex| %>
<tr id="<%= dom_id(ex) %>">
<td><%= ex.code %></td>
<td><%= ex.description %></td>
<td>
<%= link_to 'Edit',
edit_green_lanes_exemption_path(ex) %>
</td>
</tr>
<% end %>
</tbody>
</table>

<% else %>
<div class="govuk-inset-text">
<p>No any Green Lanes Exception</p>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/green_lanes/exemptions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= govuk_breadcrumbs 'Exemption': green_lanes_exemptions_path %>

<h2>New Exemption</h2>

<%= render 'form', exemption: @exemption %>
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<%= header.with_navigation_item text: 'Category assessments',
href: green_lanes_category_assessments_path,
active: active_nav_link?(/\/category_assessments/) %>
<%= header.with_navigation_item text: 'Exemptions',
href: green_lanes_exemptions_path,
active: active_nav_link?(/\/exemptions/) %>
<%= header.with_navigation_item text: 'Exempting Certificate Overrides',
href: green_lanes_exempting_certificate_overrides_path,
active: active_nav_link?(/\/exempting_certificate_overrides/) %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
namespace :green_lanes, path: 'green_lanes' do
resources :category_assessments, only: %i[index new create edit update destroy]
resources :exempting_certificate_overrides, only: %i[index new create destroy]
resources :exemptions, only: %i[index new create edit update destroy]
end

resources :tariff_updates, only: %i[index show] do
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/green_lanes/exemption_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :exemption, class: 'GreenLanes::Exemption' do
sequence(:id) { |n| n }
code { 'P' }
description { 'pseudo exemptions' }
created_at { 2.days.ago.to_date }
updated_at { nil }
end
end
113 changes: 113 additions & 0 deletions spec/requests/green_lanes/exemptions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
RSpec.describe GreenLanes::ExemptionsController do
subject(:rendered_page) { create_user && make_request && response }

let(:exemption) { build :exemption }
let(:create_user) { create :user, permissions: ['signin', 'HMRC Editor'] }

before do
allow(TradeTariffAdmin::ServiceChooser).to receive(:service_choice).and_return 'xi'
end

describe 'GET #index' do
before do
stub_api_request('/admin/green_lanes/exemptions', backend: 'xi').and_return \
jsonapi_response :exemptions, attributes_for_list(:exemption, 3)
end

let(:make_request) { get green_lanes_exemptions_path }

it { is_expected.to have_http_status :success }
it { is_expected.not_to include 'div.current-service' }
end

describe 'GET #new' do
let(:make_request) { get new_green_lanes_exemption_path }

it { is_expected.to have_http_status :ok }
it { is_expected.not_to include 'div.current-service' }
end

describe 'POST #create' do
before do
stub_api_request('/admin/green_lanes/exemptions', :post).to_return create_response
end

let :make_request do
post green_lanes_exemptions_path,
params: { exemption: ex_params }
end

context 'with valid item' do
let(:ex_params) { exemption.attributes.without(:id) }
let(:create_response) { webmock_response(:created, exemption.attributes) }

it { is_expected.to redirect_to green_lanes_exemptions_path }
end

context 'with invalid item' do
let(:ex_params) { exemption.attributes.without(:id, :code) }
let(:create_response) { webmock_response(:error, code: "can't be blank'") }

it { is_expected.to have_http_status :ok }
it { is_expected.to have_attributes body: /can.+t be blank/ }
it { is_expected.not_to include 'div.current-service' }
end
end

describe 'GET #edit' do
before do
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}")
.and_return jsonapi_response(:exemption, exemption.attributes)
end

let(:make_request) { get edit_green_lanes_exemption_path(exemption) }

it { is_expected.to have_http_status :success }
it { is_expected.not_to include 'div.current-service' }
end

describe 'PATCH #update' do
before do
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}")
.and_return jsonapi_response(:exemption, exemption.attributes)

stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}", :patch)
.and_return patch_response
end

let :make_request do
patch green_lanes_exemption_path(exemption),
params: { exemption: exemption.attributes.merge(description: new_desc) }
end

context 'with valid change' do
let(:new_desc) { 'New description' }
let(:patch_response) { webmock_response :updated, "/admin/green_lanes/exemptions/#{exemption.id}" }

it { is_expected.to redirect_to green_lanes_exemptions_path }
end

context 'with invalid change' do
let(:new_desc) { nil }
let(:patch_response) { webmock_response :error, description: "can't be blank" }

it { is_expected.to have_http_status :ok }
it { is_expected.to have_attributes body: /can.+t be blank/ }
it { is_expected.not_to include 'div.current-service' }
end
end

describe 'DELETE #destroy' do
before do
stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}")
.and_return jsonapi_response(:exemption, exemption.attributes)

stub_api_request("/admin/green_lanes/exemptions/#{exemption.id}", :delete)
.and_return webmock_response :no_content
end

let(:make_request) { delete green_lanes_exemption_path(exemption) }

it { is_expected.to redirect_to green_lanes_exemptions_path }
end
end

0 comments on commit 7b34d8f

Please sign in to comment.