Skip to content

Commit

Permalink
Merge branch 'master' into reference-one-object-in-collection
Browse files Browse the repository at this point in the history
  • Loading branch information
cintamani authored Oct 14, 2019
2 parents 51a89ae + 3dfb753 commit 78e000e
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ def new
def create
super(TierCheckForm, "tier_check_form")
end

private

def transient_registration_attributes
params.require(:tier_check_form).permit(:temp_tier_check)
end
end
end
16 changes: 1 addition & 15 deletions app/forms/waste_carriers_engine/tier_check_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@

module WasteCarriersEngine
class TierCheckForm < BaseForm
attr_accessor :temp_tier_check
delegate :temp_tier_check, to: :transient_registration

validates :temp_tier_check, "waste_carriers_engine/yes_no": true

def initialize(transient_registration)
super

self.temp_tier_check = transient_registration.temp_tier_check
end

def submit(params)
# Assign the params for validation and pass them to the BaseForm method for updating
self.temp_tier_check = params[:temp_tier_check]
attributes = { temp_tier_check: temp_tier_check }

super(attributes)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ module CanHaveRegistrationAttributes
field :title, type: String
field :total_fee, type: String

scope :search_term, lambda { |term|
any_of({ reg_identifier: /\A#{term}\z/i },
{ company_name: /#{term}/i },
{ last_name: /#{term}/i },
"addresses.postcode": /#{term}/i)
}

def charity?
business_type == "charity"
end
Expand Down
6 changes: 0 additions & 6 deletions app/models/waste_carriers_engine/transient_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ class TransientRegistration
field :temp_payment_method, type: String
field :temp_tier_check, type: String # 'yes' or 'no' - should refactor to boolean

scope :search_term, lambda { |term|
any_of({ reg_identifier: /\A#{term}\z/i },
{ company_name: /#{term}/i },
{ last_name: /#{term}/i },
"addresses.postcode": /#{term}/i)
}
scope :in_progress, -> { where(:workflow_state.nin => %w[renewal_complete_form renewal_received_form]) }
scope :submitted, -> { where(:workflow_state.in => %w[renewal_complete_form renewal_received_form]) }
scope :pending_payment, -> { submitted.where(:"financeDetails.balance".gt => 0) }
Expand Down
13 changes: 10 additions & 3 deletions spec/factories/forms/tier_check_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
FactoryBot.define do
factory :tier_check_form, class: WasteCarriersEngine::TierCheckForm do
trait :has_required_data do
temp_tier_check { "no" }

initialize_with { new(create(:transient_registration, :has_required_data, workflow_state: "tier_check_form")) }
initialize_with do
new(
create(
:transient_registration,
:has_required_data,
workflow_state: "tier_check_form",
temp_tier_check: "no"
)
)
end
end
end
end
6 changes: 6 additions & 0 deletions spec/models/waste_carriers_engine/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,11 @@ module WasteCarriersEngine
end
end
end

describe "search" do
it_should_behave_like "Search scopes",
record_class: WasteCarriersEngine::Registration,
factory: :registration
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ module WasteCarriersEngine
end
end

describe "scope" do
describe "search" do
it_should_behave_like "Search scopes",
record_class: WasteCarriersEngine::TransientRegistration,
factory: :transient_registration
end

describe "scopes" do
it_should_behave_like "TransientRegistration named scopes"
end

Expand Down
74 changes: 74 additions & 0 deletions spec/support/shared_examples/search_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

RSpec.shared_examples "Search scopes" do |record_class:, factory:|
let(:non_matching_record) do
create(factory,
:has_required_data)
end

describe "#search_term" do
let(:term) { nil }
let(:scope) { record_class.search_term(term) }

it "returns everything when no search term is given" do
expect(scope.length).to eq(record_class.all.length)
end

context "when the search term is a reg_identifier" do
let(:matching_record) do
create(factory, :has_required_data)
end

let(:term) { matching_record.reg_identifier }

it "returns records with a matching reg_identifier" do
expect(scope).to include(matching_record)
end

it "does not return others" do
expect(scope).not_to include(non_matching_record)
end
end

context "when the search term is a name" do
let(:term) { "Lee" }

let(:matching_company_name_record) do
create(factory, :has_required_data, company_name: "Stan Lee Waste Company")
end

let(:matching_person_name_record) do
create(factory, :has_required_data, last_name: "Lee")
end

it "returns records with a matching company_name" do
expect(scope).to include(matching_company_name_record)
end

it "returns records with a matching last_name" do
expect(scope).to include(matching_person_name_record)
end

it "does not return others" do
expect(scope).not_to include(non_matching_record)
end
end

context "when the search term is a postcode" do
let(:term) { "SW1A 2AA" }

let(:matching_postcode_record) do
address = build(:address, postcode: term)
create(factory, :has_required_data, addresses: [address])
end

it "returns records with a matching postcode" do
expect(scope).to include(matching_postcode_record)
end

it "does not return others" do
expect(scope).not_to include(non_matching_record)
end
end
end
end
62 changes: 0 additions & 62 deletions spec/support/shared_examples/transient_registration_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,68 +25,6 @@
workflow_state: :renewal_received_form)
end

describe "#search_term" do
let(:term) { nil }
let(:scope) { WasteCarriersEngine::TransientRegistration.search_term(term) }

it "returns everything when no search term is given" do
expect(scope.length).to eq(WasteCarriersEngine::TransientRegistration.all.length)
end

context "when the search term is a reg_identifier" do
let(:term) { in_progress_renewal.reg_identifier }

it "returns renewals with a matching reg_identifier" do
expect(scope).to include(in_progress_renewal)
end

it "does not return others" do
expect(scope).not_to include(submitted_renewal)
end
end

context "when the search term is a name" do
let(:term) { "Lee" }

let(:matching_company_name_renewal) do
create(:transient_registration, :has_required_data, company_name: "Stan Lee Waste Company")
end

let(:matching_person_name_renewal) do
create(:transient_registration, :has_required_data, last_name: "Lee")
end

it "returns renewals with a matching company_name" do
expect(scope).to include(matching_company_name_renewal)
end

it "returns renewals with a matching last_name" do
expect(scope).to include(matching_person_name_renewal)
end

it "does not return others" do
expect(scope).not_to include(in_progress_renewal)
end
end

context "when the search term is a postcode" do
let(:term) { "SW1A 2AA" }

let(:matching_postcode_renewal) do
address = build(:address, postcode: term)
create(:transient_registration, :has_required_data, addresses: [address])
end

it "returns renewals with a matching postcode" do
expect(scope).to include(matching_postcode_renewal)
end

it "does not return others" do
expect(scope).not_to include(in_progress_renewal)
end
end
end

describe "#in_progress" do
let(:scope) { WasteCarriersEngine::TransientRegistration.in_progress }

Expand Down

0 comments on commit 78e000e

Please sign in to comment.