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:
- 'app/models/role.rb'
Rails/HasManyOrHasOneDependent:
Enabled: false
Rails/LexicallyScopedActionFilter:
Exclude:
- 'app/controllers/application_controller.rb'

View file

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

View file

@ -2,4 +2,16 @@
class ApplicationRecord < ActiveRecord::Base
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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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