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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9637536cde1b2f57e976fa87ab6dfc2bdb34dee0
4
- data.tar.gz: b639ad58c3b7b328a7e37a858b5693832604e620
3
+ metadata.gz: 93cf9f49f91eee71e3ad4f920ab2a6be8f212363
4
+ data.tar.gz: 0b213d2fc9d5882a4c387e94989af64d96c0b037
5
5
  SHA512:
6
- metadata.gz: c84146477d0ba45f0f5a36386e06ade453339b45473a8bebba2f4b4da2eda876e6cfe62dc86c4a1f539838ff765c3753436f10853bc97df2a800f605c064daba
7
- data.tar.gz: f941ae734b28a694921090d664e3a263c168aba74461d3106c3a04aeeaba3889157db5629c89f3f57187150e6622b3bb66977371999525c89188022da1db2a5b
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, -> { select("widget_id, description") }).
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
- In addition to custom eager loading queries, we're also adding nested eager loading (and customizing those queries!).
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, -> { select("widget_id, order_id") }) {
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, -> { select("id, name") })
88
+ eager_load(:customer, select: "id, name")
89
89
  }
90
90
  }.
91
91
  run
@@ -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
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # Library version
6
- VERSION = '0.2.0'.freeze
6
+ VERSION = '0.3.0'.freeze
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: occams-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger