Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f075db2
Showing
4 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'active_record_ext' | ||
require 'array_ext' | ||
|
||
# extend activerecord with the :preload method | ||
ActiveRecord::Base.send(:extend, AssociationsPreload::Base) | ||
# add :preload method to array | ||
Array.send(:include, AssociationsPreload::Array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module AssociationsPreload | ||
module Base | ||
def preload(records, *args) | ||
include_associations = send(:merge_includes, send(:scope, :find, :include), args) | ||
preload_associations(records, include_associations) unless include_associations.empty? | ||
records | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module AssociationsPreload | ||
module Array | ||
def preload(*args) | ||
return self if self.empty? | ||
first.class.preload(self, *args) | ||
end | ||
end | ||
end |