Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Add support for associations with inverse_of option #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/cache_depends_on/association_cache_dependency_definer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def find_inverse_of(association)
end

def find_singular_inverse_of(association)
suggested_singular_reverse_association_name = model_klass.model_name.singular.to_sym
suggested_singular_reverse_association_name = association.options.fetch(:inverse_of, model_klass.model_name.singular.to_sym)
association.klass.reflect_on_association suggested_singular_reverse_association_name
end

def find_plural_inverse_of(association)
suggested_plural_reverse_association_name = model_klass.model_name.plural.to_sym
suggested_plural_reverse_association_name = association.options.fetch(:inverse_of, model_klass.model_name.plural.to_sym)
association.klass.reflect_on_association suggested_plural_reverse_association_name
end

Expand Down
69 changes: 69 additions & 0 deletions spec/cache_depends_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,73 @@ def another_after_cache_invalidation_hook; end
provider.touch
end
end

describe 'associations with inverse_of option' do
class CompanyAddress < ActiveRecord::Base
belongs_to :company, inverse_of: :address
has_many :company_employees, through: :company, source: :employees
end

class Company < ActiveRecord::Base
has_many :employees, class_name: 'User'
has_one :address, class_name: 'CompanyAddress', inverse_of: :company
end

class UserProfile < ActiveRecord::Base
belongs_to :user, inverse_of: :profile
end

class UserRole < ActiveRecord::Base
belongs_to :user, inverse_of: :roles
end

class User < ActiveRecord::Base
belongs_to :company, inverse_of: :employees
has_many :roles, class_name: 'UserRole'
has_one :profile, class_name: 'UserProfile'
has_one :company_address, through: :company, source: :address, inverse_of: :company_employees

cache_depends_on :roles, :profile, :company, :company_address
end

let!(:company) { Company.create! }
let!(:company_address) { CompanyAddress.create! company: company }
let!(:user) { User.create! company: company }
let!(:user_profile) { UserProfile.create! user: user }
let!(:user_role) { UserRole.create! user: user }

before { user.update_column(:updated_at, 1.day.ago) }

shared_examples 'dependent entity updating' do
let(:dependent_entity) { user }

it 'updates dependent entity' do
expect { entity.reload.update_attribute(:updated_at, Time.now) }.to change { dependent_entity.reload.updated_at }
end
end

context 'when relation is has_many with inverse_of' do
let(:entity) { user_role }

it_behaves_like 'dependent entity updating'
end

context 'when relation is has_one with inverse_of' do
let(:entity) { user_profile }

it_behaves_like 'dependent entity updating'
end

context 'when relation is belongs_to with inverse_of' do
let(:entity) { company }

it_behaves_like 'dependent entity updating'
end

context 'when relation is has_one through another association with inverse_of' do
let(:entity) { company_address }

it_behaves_like 'dependent entity updating'
end
end
end
24 changes: 24 additions & 0 deletions spec/support/database_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,28 @@
create_table(:payers) do |t|
t.timestamps null: false
end

create_table(:companies) do |t|
t.timestamps null: false
end

create_table(:company_addresses) do |t|
t.belongs_to :company
t.timestamps null: false
end

create_table(:users) do |t|
t.belongs_to :company
t.timestamps null: false
end

create_table(:user_profiles) do |t|
t.belongs_to :user
t.timestamps null: false
end

create_table(:user_roles) do |t|
t.belongs_to :user
t.timestamps null: false
end
end