Skip to content

Commit

Permalink
Merge 41653aa into 4164879
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryTsepelev committed Aug 24, 2020
2 parents 4164879 + 41653aa commit d13ef4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Expand Up @@ -20,6 +20,7 @@ gemfile:
- gemfiles/railsmaster.gemfile

env:
- TASK=rubocop
- TASK=spec
- TASK=bench
- TASK=memory
Expand All @@ -36,6 +37,17 @@ matrix:
- rvm: ruby-head
env: TASK=bench

- rvm: 2.3
env: TASK=rubocop
- rvm: 2.4
env: TASK=rubocop
- rvm: 2.5
env: TASK=rubocop
- rvm: 2.6
env: TASK=rubocop
- rvm: ruby-head
env: TASK=rubocop

- rvm: 2.3
env: TASK=memory
- rvm: 2.4
Expand All @@ -60,6 +72,9 @@ matrix:
gemfile: gemfiles/rails_5_0.gemfile
- rvm: 2.6
gemfile: gemfiles/rails_5_1.gemfile
- rvm: 2.6
gemfile: gemfiles/rails_6_0.gemfile
env: TASK=rubocop

allow_failures:
- rvm: ruby-head
Expand Down
17 changes: 8 additions & 9 deletions lib/ar_lazy_preload/active_record/relation.rb
Expand Up @@ -5,7 +5,7 @@
module ArLazyPreload
# ActiveRecord::Relation patch with lazy preloading support
module Relation
attr_writer :preload_associations_lazily_setting
attr_writer :preloads_associations_lazily

# Enhanced #load method will check if association has not been loaded yet and add a context
# for lazy preloading to loaded each record
Expand All @@ -16,14 +16,13 @@ def load
Context.register(
records: ar_lazy_preload_records,
association_tree: lazy_preload_values,
auto_preload: preload_associations_lazily_setting
auto_preload: preloads_associations_lazily?
)
end
result
end

# Lazily autoloads all association
# example:
# Lazily autoloads all associations. For example:
#
# users = User.preload_associations_lazily
# users.each do |user|
Expand All @@ -32,11 +31,7 @@ def load
#
# Same effect can be achieved by User.lazy_preload(posts: :comments)
def preload_associations_lazily
spawn.tap { |relation| relation.preload_associations_lazily_setting = true }
end

def preload_associations_lazily_setting
@preload_associations_lazily_setting ||= false
spawn.tap { |relation| relation.preloads_associations_lazily = true }
end

# Specify relationships to be loaded lazily when association is loaded for the first time. For
Expand Down Expand Up @@ -76,6 +71,10 @@ def ar_lazy_preload_records
@records
end

def preloads_associations_lazily?
@preloads_associations_lazily ||= false
end

attr_writer :lazy_preload_values
end
end
1 change: 0 additions & 1 deletion lib/ar_lazy_preload/contexts/base_context.rb
Expand Up @@ -38,7 +38,6 @@ def association_needs_preload?(_association_name)
raise NotImplementedError
end


private

def perform_preloading(association_name)
Expand Down

0 comments on commit d13ef4c

Please sign in to comment.