Browse Source

feat: be more explicit about org scopes

pull/1789/head
natdeanlewissoftwire 3 years ago
parent
commit
92ca71a591
  1. 15
      app/models/organisation.rb
  2. 1
      app/models/sales_log.rb
  3. 4
      app/models/user.rb

15
app/models/organisation.rb

@ -1,9 +1,6 @@
class Organisation < ApplicationRecord class Organisation < ApplicationRecord
has_many :users, dependent: :delete_all has_many :users, dependent: :delete_all
has_many :data_protection_officers, -> { where(is_dpo: true) }, class_name: "User" has_many :data_protection_officers, -> { where(is_dpo: true) }, class_name: "User"
has_many :owned_lettings_logs, class_name: "LettingsLog", foreign_key: "owning_organisation_id", dependent: :delete_all
has_many :managed_lettings_logs, class_name: "LettingsLog", foreign_key: "managing_organisation_id"
has_many :owned_sales_logs, class_name: "SalesLog", foreign_key: "owning_organisation_id", dependent: :delete_all
has_one :data_protection_confirmation has_one :data_protection_confirmation
has_many :organisation_rent_periods has_many :organisation_rent_periods
has_many :owned_schemes, class_name: "Scheme", foreign_key: "owning_organisation_id", dependent: :delete_all has_many :owned_schemes, class_name: "Scheme", foreign_key: "owning_organisation_id", dependent: :delete_all
@ -67,11 +64,19 @@ class Organisation < ApplicationRecord
end end
def lettings_logs def lettings_logs
LettingsLog.filter_by_organisation(self) LettingsLog.filter_by_organisation(absorbed_organisations << self)
end end
def sales_logs def sales_logs
SalesLog.filter_by_organisation(self) SalesLog.filter_by_owning_organisation(absorbed_organisations << self)
end
def owned_lettings_logs
LettingsLog.filter_by_owning_organisation(absorbed_organisations << self)
end
def managed_lettings_logs
LettingsLog.filter_by_managing_organisation(absorbed_organisations << self)
end end
def address_string def address_string

1
app/models/sales_log.rb

@ -40,6 +40,7 @@ class SalesLog < Log
.or(filter_by_postcode(param)) .or(filter_by_postcode(param))
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :filter_by_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) }
scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) }
scope :duplicate_logs, lambda { |log| scope :duplicate_logs, lambda { |log|
visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))

4
app/models/user.rb

@ -77,10 +77,6 @@ class User < ApplicationRecord
LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations << organisation) LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations << organisation)
end end
def owned_sales_logs
SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations << organisation)
end
def is_key_contact? def is_key_contact?
is_key_contact is_key_contact
end end

Loading…
Cancel
Save