1
0
Fork 0

Override ApplicationRecord.has_many, .has_one

This commit is contained in:
Alex Kotov 2019-07-20 15:25:23 +05:00
parent aa1ad35ae9
commit f2dc33b8dc
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
9 changed files with 30 additions and 21 deletions

View file

@ -50,6 +50,9 @@ Rails/HasAndBelongsToMany:
Exclude: Exclude:
- 'app/models/role.rb' - 'app/models/role.rb'
Rails/HasManyOrHasOneDependent:
Enabled: false
Rails/LexicallyScopedActionFilter: Rails/LexicallyScopedActionFilter:
Exclude: Exclude:
- 'app/controllers/application_controller.rb' - 'app/controllers/application_controller.rb'

View file

@ -26,8 +26,7 @@ class Account < ApplicationRecord
has_many :account_roles, has_many :account_roles,
-> { active }, -> { active },
inverse_of: :account, inverse_of: :account
dependent: :restrict_with_exception
has_many :roles, has_many :roles,
-> { distinct }, -> { distinct },
@ -37,7 +36,7 @@ class Account < ApplicationRecord
belongs_to :contacts_list belongs_to :contacts_list
has_one :user, dependent: :restrict_with_exception has_one :user
############# #############
# Callbacks # # Callbacks #

View file

@ -2,4 +2,16 @@
class ApplicationRecord < ActiveRecord::Base class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
def self.has_many(*args) # rubocop:disable Naming/PredicateName
options = args.extract_options!
options[:dependent] ||= :restrict_with_exception
super(*args, options)
end
def self.has_one(*args) # rubocop:disable Naming/PredicateName
options = args.extract_options!
options[:dependent] ||= :restrict_with_exception
super(*args, options)
end
end end

View file

@ -5,7 +5,7 @@ class ContactsList < ApplicationRecord
# Associations # # Associations #
################ ################
has_one :account, dependent: :restrict_with_exception has_one :account
has_one :person, dependent: :restrict_with_exception has_one :person
end end

View file

@ -19,7 +19,7 @@ class FederalSubject < ApplicationRecord
# Associations # # Associations #
################ ################
has_one :regional_office, dependent: :restrict_with_exception has_one :regional_office
############### ###############
# Validations # # Validations #

View file

@ -11,28 +11,25 @@ class Person < ApplicationRecord
belongs_to :contacts_list belongs_to :contacts_list
has_one :account, dependent: :restrict_with_exception has_one :account
has_many :relationships, has_many :relationships,
-> { order(from_date: :asc) }, -> { order(from_date: :asc) },
inverse_of: :person, inverse_of: :person
dependent: :restrict_with_exception
has_one :current_relationship, has_one :current_relationship,
-> { order(from_date: :desc) }, -> { order(from_date: :desc) },
class_name: 'Relationship', class_name: 'Relationship',
inverse_of: :person, inverse_of: :person
dependent: :restrict_with_exception
has_one :regional_office, has_one :regional_office,
inverse_of: :people, inverse_of: :people,
through: :current_relationship, through: :current_relationship,
source: :regional_office, source: :regional_office
dependent: :restrict_with_exception
has_many :person_comments, dependent: :restrict_with_exception has_many :person_comments
has_many :passports, dependent: :restrict_with_exception has_many :passports
############### ###############
# Validations # # Validations #

View file

@ -7,13 +7,12 @@ class RegionalOffice < ApplicationRecord
belongs_to :federal_subject belongs_to :federal_subject
has_many :relationships, dependent: :restrict_with_exception has_many :relationships
has_many :people, has_many :people,
inverse_of: :regional_office, inverse_of: :regional_office,
through: :relationships, through: :relationships,
source: :person, source: :person
dependent: :restrict_with_exception
############### ###############
# Validations # # Validations #

View file

@ -13,8 +13,7 @@ class Role < ApplicationRecord
has_many :account_roles, has_many :account_roles,
-> { active }, -> { active },
inverse_of: :role, inverse_of: :role
dependent: :restrict_with_exception
has_many :accounts, through: :account_roles has_many :accounts, through: :account_roles

View file

@ -21,7 +21,7 @@ class User < ApplicationRecord
belongs_to :account belongs_to :account
has_many :user_omniauths, dependent: :restrict_with_exception has_many :user_omniauths
############### ###############
# Validations # # Validations #