diff --git a/lib/cache_depends_on/association_cache_dependency_definer.rb b/lib/cache_depends_on/association_cache_dependency_definer.rb index 72161c8..ffe7536 100644 --- a/lib/cache_depends_on/association_cache_dependency_definer.rb +++ b/lib/cache_depends_on/association_cache_dependency_definer.rb @@ -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 diff --git a/spec/cache_depends_on_spec.rb b/spec/cache_depends_on_spec.rb index 3177d28..e703ef1 100644 --- a/spec/cache_depends_on_spec.rb +++ b/spec/cache_depends_on_spec.rb @@ -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 diff --git a/spec/support/database_schema.rb b/spec/support/database_schema.rb index c68ecdc..72c481f 100644 --- a/spec/support/database_schema.rb +++ b/spec/support/database_schema.rb @@ -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