occams-record 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/occams-record/query.rb +3 -1
- data/lib/occams-record/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93cf9f49f91eee71e3ad4f920ab2a6be8f212363
|
4
|
+
data.tar.gz: 0b213d2fc9d5882a4c387e94989af64d96c0b037
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34f6973012cdae863862cf6a8850442d18333f320bd5920866d1f7bd402dc6530bcadb76bece429512b6ec6c5a527afbdf8d1d8041145ff5691071c95ee9dba0
|
7
|
+
data.tar.gz: 2068ccf8a5c863ac5c9a63f5df408b6d3fafda5cca6b0bb292873397480048f985e89c8d27e9ce08dcc80890c72f2720172c477baa527e00c51e526b04f7a5d9
|
data/README.md
CHANGED
@@ -59,7 +59,7 @@ Notice that we're eager loading splines, but *only the fields that we need*. If
|
|
59
59
|
widgets = OccamsRecord.
|
60
60
|
query(Widget.order("name")).
|
61
61
|
eager_load(:category).
|
62
|
-
eager_load(:splines,
|
62
|
+
eager_load(:splines, select: "widget_id, description").
|
63
63
|
run
|
64
64
|
|
65
65
|
widgets[0].splines.map { |s| s.description }
|
@@ -71,7 +71,7 @@ widgets[1].splines.map { |s| s.description }
|
|
71
71
|
|
72
72
|
**An insane example, but only half as insane as the one that prompted the creation of this library**
|
73
73
|
|
74
|
-
|
74
|
+
Here we're eager loading several levels down. Notice the `Proc` given to `eager_load(:orders)`. The `select:` option is just for convenience; you may instead pass a `Proc` and customize the query with any of ActiveRecord's query builder helpers (`select`, `where`, `order`, etc).
|
75
75
|
|
76
76
|
```ruby
|
77
77
|
widgets = OccamsRecord.
|
@@ -79,13 +79,13 @@ widgets = OccamsRecord.
|
|
79
79
|
eager_load(:category).
|
80
80
|
|
81
81
|
# load order_items, but only the fields needed to identify which orders go with which widgets
|
82
|
-
eager_load(:order_items,
|
82
|
+
eager_load(:order_items, select: "widget_id, order_id") {
|
83
83
|
|
84
84
|
# load the orders
|
85
|
-
eager_load(:orders) {
|
85
|
+
eager_load(:orders, -> { order "order_date DESC" }) {
|
86
86
|
|
87
87
|
# load the customers who made the orders, but only their names
|
88
|
-
eager_load(:customer,
|
88
|
+
eager_load(:customer, select: "id, name")
|
89
89
|
}
|
90
90
|
}.
|
91
91
|
run
|
data/lib/occams-record/query.rb
CHANGED
@@ -66,13 +66,15 @@ module OccamsRecord
|
|
66
66
|
#
|
67
67
|
# @param assoc [Symbol] name of association
|
68
68
|
# @param scope [Proc] a scope to apply to the query (optional)
|
69
|
+
# @param select [String] a custom SELECT statement, minus the SELECT (optional)
|
69
70
|
# @param use [Array<Module>] optional Module to include in the result class (single or array)
|
70
71
|
# @param eval_block [Proc] a block where you may perform eager loading on *this* association (optional)
|
71
72
|
# @return [OccamsRecord::Query] returns self
|
72
73
|
#
|
73
|
-
def eager_load(assoc, scope = nil, use: nil, &eval_block)
|
74
|
+
def eager_load(assoc, scope = nil, select: nil, use: nil, &eval_block)
|
74
75
|
ref = model.reflections[assoc.to_s]
|
75
76
|
raise "OccamsRecord: No assocation `:#{assoc}` on `#{model.name}`" if ref.nil?
|
77
|
+
scope ||= -> { self.select select } if select
|
76
78
|
@eager_loaders << EagerLoaders.fetch!(ref).new(ref, scope, use, &eval_block)
|
77
79
|
self
|
78
80
|
end
|