Browse Source

feat: update locations and schemes displayed

pull/1739/head
natdeanlewissoftwire 3 years ago
parent
commit
61f8730c3a
  1. 2
      app/models/derived_variables/lettings_log_variables.rb
  2. 5
      app/models/form/lettings/questions/location_id.rb
  3. 3
      app/models/form/lettings/questions/scheme_id.rb
  4. 2
      app/models/location.rb

2
app/models/derived_variables/lettings_log_variables.rb

@ -32,7 +32,7 @@ module DerivedVariables::LettingsLogVariables
def scheme_has_multiple_locations? def scheme_has_multiple_locations?
return false unless scheme return false unless scheme
@scheme_locations_count ||= scheme.locations.active.size @scheme_locations_count ||= scheme.locations.active_in_2_weeks.size
@scheme_locations_count > 1 @scheme_locations_count > 1
end end

5
app/models/form/lettings/questions/location_id.rb

@ -19,8 +19,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question
answer_opts = {} answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
Location.select(:id, :postcode, :name).where("startdate <= ? or startdate IS NULL", Location.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh|
Time.zone.today).each_with_object(answer_opts) do |location, hsh|
hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name }
hsh hsh
end end
@ -29,7 +28,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question
def displayed_answer_options(lettings_log, _user = nil) def displayed_answer_options(lettings_log, _user = nil)
return {} unless lettings_log.scheme return {} unless lettings_log.scheme
scheme_location_ids = lettings_log.scheme.locations.confirmed.pluck(:id) scheme_location_ids = lettings_log.scheme.locations.pluck(:id)
answer_options.select { |k, _v| scheme_location_ids.include?(k.to_i) } answer_options.select { |k, _v| scheme_location_ids.include?(k.to_i) }
end end

3
app/models/form/lettings/questions/scheme_id.rb

@ -35,8 +35,7 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question
else else
Scheme.includes(:locations).select(:id).where(confirmed: true) Scheme.includes(:locations).select(:id).where(confirmed: true)
end end
filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id)
Time.zone.today)).map(&:id)
answer_options.select do |k, _v| answer_options.select do |k, _v|
filtered_scheme_ids.include?(k.to_i) || k.blank? filtered_scheme_ids.include?(k.to_i) || k.blank?
end end

2
app/models/location.rb

@ -23,6 +23,8 @@ class Location < ApplicationRecord
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) }
scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) } scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) }
scope :active, -> { where(confirmed: true).and(started) } scope :active, -> { where(confirmed: true).and(started) }
scope :started_in_2_weeks, -> { where("startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) }
scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) }
scope :confirmed, -> { where(confirmed: true) } scope :confirmed, -> { where(confirmed: true) }
scope :unconfirmed, -> { where.not(confirmed: true) } scope :unconfirmed, -> { where.not(confirmed: true) }

Loading…
Cancel
Save