Browse Source

Add derived variables to sales log

pull/922/head
Jack S 3 years ago
parent
commit
dbac051bc6
  1. 5
      app/models/derived_variables/sales_log_variables.rb
  2. 4
      app/models/lettings_log.rb
  3. 4
      app/models/log.rb
  4. 5
      app/models/sales_log.rb
  5. 3
      spec/models/lettings_log_spec.rb
  6. 3
      spec/models/sales_log_spec.rb
  7. 17
      spec/shared/shared_examples_for_derived_fields.rb

5
app/models/derived_variables/sales_log_variables.rb

@ -0,0 +1,5 @@
module DerivedVariables::SalesLogVariables
def set_derived_fields!
self.ethnic = 17 if ethnic_refused?
end
end

4
app/models/lettings_log.rb

@ -372,10 +372,6 @@ class LettingsLog < Log
hb == 7
end
def ethnic_refused?
ethnic_group == 17
end
def receives_housing_related_benefits?
if collection_start_year <= 2021
receives_housing_benefit_only? || receives_uc_with_housing_element_excl_housing_benefit? ||

4
app/models/log.rb

@ -36,6 +36,10 @@ class Log < ApplicationRecord
false
end
def ethnic_refused?
ethnic_group == 17
end
private
def update_status!

5
app/models/sales_log.rb

@ -3,10 +3,15 @@ class SalesLogValidator < ActiveModel::Validator
end
class SalesLog < Log
include DerivedVariables::SalesLogVariables
self.inheritance_column = :_type_disabled
has_paper_trail
validates_with SalesLogValidator
before_validation :set_derived_fields!
before_validation :reset_invalidated_dependent_fields!
scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :search_by, ->(param) { filter_by_id(param) }

3
spec/models/lettings_log_spec.rb

@ -1,4 +1,5 @@
require "rails_helper"
require "shared/shared_examples_for_derived_fields"
RSpec.describe LettingsLog do
let(:owning_organisation) { FactoryBot.create(:organisation) }
@ -10,6 +11,8 @@ RSpec.describe LettingsLog do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
include_examples "shared examples for derived fields", :lettings_log
it "inherits from log" do
expect(described_class).to be < Log
expect(described_class).to be < ApplicationRecord

3
spec/models/sales_log_spec.rb

@ -1,9 +1,12 @@
require "rails_helper"
require "shared/shared_examples_for_derived_fields"
RSpec.describe SalesLog, type: :model do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:created_by_user) { FactoryBot.create(:user) }
include_examples "shared examples for derived fields", :sales_log
it "inherits from log" do
expect(described_class).to be < Log
expect(described_class).to be < ApplicationRecord

17
spec/shared/shared_examples_for_derived_fields.rb

@ -0,0 +1,17 @@
require "rails_helper"
RSpec.shared_examples "shared examples for derived fields" do |log_type|
describe "sets ethnic based on the value of ethnic_refused" do
it "is set to 17 when ethnic_group is 17" do
log = FactoryBot.build(log_type, ethnic_group: 17, ethnic: nil)
expect { log.set_derived_fields! }.to change(log, :ethnic).from(nil).to(17)
end
it "is is not modified otherwise" do
log = FactoryBot.build(log_type, ethnic_group: nil, ethnic: nil)
expect { log.set_derived_fields! }.not_to change(log, :ethnic)
end
end
end
Loading…
Cancel
Save