DmitryTsepelev / ar_lazy_preload

Lazy loading associations for the ActiveRecord models
MIT License
677 stars 24 forks source link

Improve compatibility with ActiveRecord API for singular associations #28

Closed sandersiim closed 4 years ago

sandersiim commented 4 years ago

Use relevant ActiveRecord::Associations::Association methods instead of relying on public_send. This allows application code to override behaviour of model generated association methods, without changing the behaviour of the model.association().reader method, when the association is "lazy preloaded".

E.g for a model:

class Model < ActiveRecord
  has_one :link

  def link
    puts 'application code'
    super
  end
end

calling Model.lazy_preload(:link).association(:link).reader would currently print out 'application code'. This PR makes it so that it doesn't, which is the expected behaviour since that's how ActiveRecord .preload behaves.

This incompatibility initially blocked the gem usage in our codebase, but this patch solves the issue for us.

coveralls commented 4 years ago

Pull Request Test Coverage Report for Build 232


Totals Coverage Status
Change from base Build 219: -5.2%
Covered Lines: 549
Relevant Lines: 580

💛 - Coveralls
coveralls commented 4 years ago

Pull Request Test Coverage Report for Build 232


Totals Coverage Status
Change from base Build 219: 0.0%
Covered Lines: 545
Relevant Lines: 546

💛 - Coveralls
coveralls commented 4 years ago

Pull Request Test Coverage Report for Build 232


Totals Coverage Status
Change from base Build 219: 0.0%
Covered Lines: 545
Relevant Lines: 546

💛 - Coveralls
sandersiim commented 4 years ago

Thanks @DmitryTsepelev!