Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/controllers/planning_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ def supply_documents

def build_planning_application
@planning_application = current_local_authority.planning_applications.new
@planning_application.case_record = CaseRecord.new(local_authority: current_local_authority)

@planning_application
end

def planning_application_params
Expand Down
4 changes: 3 additions & 1 deletion app/models/case_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require "securerandom"

class CaseRecord < ApplicationRecord
delegated_type :caseable, types: %w[Enforcement PlanningApplication], dependent: :destroy
CASEABLE_TYPES = %w[Enforcement PlanningApplication].freeze

delegated_type :caseable, types: CASEABLE_TYPES, dependent: :destroy

has_many :tasks, -> { order(:position) }, as: :parent, dependent: :destroy, autosave: true
has_many :documents, dependent: :destroy
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/caseable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Caseable
included do
has_one :case_record, as: :caseable, touch: true, dependent: :destroy
delegate :local_authority, to: :case_record
delegate :local_authority=, to: :case_record
delegate :submission, to: :case_record
delegate :user, to: :case_record
end
Expand Down
7 changes: 5 additions & 2 deletions app/models/local_authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class LocalAuthority < ApplicationRecord

with_options dependent: :destroy do
has_many :users
has_many :planning_applications, -> { kept }
has_many :constraints
has_many :contacts
has_many :informatives
Expand All @@ -19,6 +18,11 @@ class LocalAuthority < ApplicationRecord
has_many :case_records
end

with_options dependent: :destroy, through: :case_records, source: :caseable do
has_many :enforcements, source_type: "Enforcement"
has_many :planning_applications, -> { kept }, source_type: "PlanningApplication"
end

with_options through: :planning_applications do
has_many :audits
has_many :consultations
Expand All @@ -27,7 +31,6 @@ class LocalAuthority < ApplicationRecord
end

has_many :neighbour_responses, through: :consultations
has_many :enforcements, through: :case_records, source: :caseable, source_type: "Enforcement"

class Accessibility < Struct.new(:postal_address, :phone_number, :email_address); end

Expand Down
6 changes: 6 additions & 0 deletions app/models/planning_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,10 @@ def application_short_case
pre_application? ? "pre-application" : "application"
end

def case_record
attributes[:case_record] ||= CaseRecord.new
end

private

def create_fee_calculation
Expand Down Expand Up @@ -1210,6 +1214,8 @@ def determination_date_is_not_in_the_future
end

def set_application_number
pp case_record
pp case_record.local_authority
max_application_number = local_authority.planning_applications.with_discarded.maximum(:application_number)

self.application_number = max_application_number ? max_application_number + 1 : 100
Expand Down
6 changes: 3 additions & 3 deletions app/services/planning_applications_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def perform
attr_reader(*ATTRIBUTES)

def importer
pa = PlanningApplication.find_or_initialize_by(reference: reference)
pa.update!(case_record: CaseRecord.new(local_authority: planning_application_attributes[:local_authority]),
**planning_application_attributes)
case_record = CaseRecord.new(local_authority: planning_application_attributes[:local_authority])
case_record.caseable = PlanningApplication.find_or_initialize_by(reference: reference)
pa.update!(**planning_application_attributes)
rescue => e
Rails.logger.debug { "[IMPORT ERROR] #{e.class}: #{e.message}" }
Rails.logger.debug pa.errors.full_messages.join(", ") if pa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ def files
end

def build_planning_application
PlanningApplication.new(planning_application_params).tap do |pa|
pa.api_user_id = user.id
pa.local_authority_id = local_authority.id
pa.case_record = CaseRecord.new(local_authority:)
end
local_authority.planning_applications.new(api_user: user, **planning_application_params)
end

def planning_application_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def create_planning_application!
end

def build_planning_application
PlanningApplication.new(planning_application_params).tap do |pa|
pa.case_record = CaseRecord.new(local_authority:, submission:)
pa.local_authority_id = local_authority.id
end
local_authority.planning_applications.new(planning_application_params)
end

def planning_application_params
Expand Down
Loading