blueprinter 0.9.0 → 0.10.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: bc5da9a0f99343970675961e3a4774db44fc587b
4
- data.tar.gz: 3a70a6c6a544b3b71c50616ef69ddc1f81b62761
3
+ metadata.gz: 280eafa98bef71d08595d4f39aaf02738a2b2b57
4
+ data.tar.gz: 4e7e99f14c98f8f4912677dcecf3638333099079
5
5
  SHA512:
6
- metadata.gz: 0cb410a384a642e7111175d2ae7252c43b37b444272c14fdea416acc59c8958bef1507bee3e13c8de1fb21fc65281f91e18b7509ba55fa512ebb871b173fdc81
7
- data.tar.gz: 0fa13303416d908c42894e70ac94106fb6e205a37301373644c29c89aebc2cf60fbe5ba6c716002a760091c31010f55889724316522e79dc18112baf414884de
6
+ metadata.gz: f79abb1b26088f23bb17cf984d9cc9549cb852454dfdf040d708cd538fae4433afdb122ad0fcd10f1294bb0ed7c32c833f8e465ed23124ceb0f8df9aa54232f1
7
+ data.tar.gz: 583bc6a0050501552f935ccf8fbc6819889e0912a8f64c4d51efc3ce44d8a8ad610348efab6bd3c8a24bcf088a0a77cb5e91e9825a583424005978302bd9e831
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.10.0 - 2018/12/20
2
+
3
+ * [FEATURE] Association Blueprints can be dynamically evaluated using a proc. [#122](https://github.com/procore/blueprinter/pull/122). Thanks to [@ritikesh](https://github.com/ritikesh).
4
+
1
5
  ## 0.9.0 - 2018/11/29
2
6
 
3
7
  * [FEATURE] Added a `render_as_json` API. Similar to `render_as_hash` but returns a JSONified hash. Please see pr [#119](https://github.com/procore/blueprinter/pull/119). Thanks to [@ritikesh](https://github.com/ritikesh).
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![CircleCI](https://circleci.com/gh/procore/blueprinter.svg?style=svg)](https://circleci.com/gh/procore/blueprinter)
2
2
  [![Gem Version](https://badge.fury.io/rb/blueprinter.svg)](https://badge.fury.io/rb/blueprinter)
3
+ [![Gitter chat](https://badges.gitter.im/procore/blueprinter.png)](https://gitter.im/blueprinter-gem/community)
3
4
 
4
5
  <img src="blueprinter_logo.svg" width="25%">
5
6
 
@@ -179,8 +180,7 @@ Output:
179
180
  ```
180
181
 
181
182
  ### Default Association/Field Option
182
- By default, an association or field that evaluates to `nil` is serialized as `nil`. A default serialized value can be
183
- specified as option on the association or field for cases when the association/field could potentially evaluate to `nil`.
183
+ By default, an association or field that evaluates to `nil` is serialized as `nil`. A default serialized value can be specified as option on the association or field for cases when the association/field could potentially evaluate to `nil`.
184
184
  ```ruby
185
185
  class UserBlueprint < Blueprinter::Base
186
186
  identifier :uuid
@@ -192,6 +192,32 @@ class UserBlueprint < Blueprinter::Base
192
192
  end
193
193
  ```
194
194
 
195
+ ### Supporting Dynamic Blueprints for associations
196
+ When defining an association, we can dynamically evaluate the blueprint. This comes in handy when adding polymorphic associations, by allowing reuse of existing blueprints.
197
+ ```ruby
198
+ class Task < ActiveRecord::Base
199
+ belongs_to :taskable, polymorphic: true
200
+ end
201
+
202
+ class Project < ActiveRecord::Base
203
+ has_many :tasks, as: :taskable
204
+
205
+ def blueprint
206
+ ProjectBlueprint
207
+ end
208
+ end
209
+
210
+ class TaskBlueprint < Blueprinter::Base
211
+ identifier :uuid
212
+
213
+ view :normal do
214
+ field :title, default: "N/A"
215
+ association :taskable, blueprint: ->(taskable) {taskable.blueprint}, default: {}
216
+ end
217
+ end
218
+ ```
219
+ Note: `taskable.blueprint` should return a valid Blueprint class. Currently, `has_many` is not supported because of the very nature of polymorphic associations.
220
+
195
221
  ### Defining a field directly in the Blueprint
196
222
 
197
223
  You can define a field directly in the Blueprint by passing it a block. This is especially useful if the object does not already have such an attribute or method defined, and you want to define it specifically for use with the Blueprint. This is done by passing `field` a block. The block also yields the object and any options that were passed from `render`. For example:
@@ -8,7 +8,14 @@ module Blueprinter
8
8
  value = @extractor.extract(association_name, object, local_options, options.except(:default))
9
9
  return options[:default] if value.nil?
10
10
  view = options[:view] || :default
11
- options[:blueprint].prepare(value, view_name: view, local_options: local_options)
11
+ blueprint = association_blueprint(options[:blueprint], value)
12
+ blueprint.prepare(value, view_name: view, local_options: local_options)
13
+ end
14
+
15
+ private
16
+
17
+ def association_blueprint(blueprint, value)
18
+ blueprint.is_a?(Proc) ? blueprint.call(value) : blueprint
12
19
  end
13
20
  end
14
21
  end
@@ -1,3 +1,3 @@
1
1
  module Blueprinter
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hess
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-12-06 00:00:00.000000000 Z
12
+ date: 2019-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_bot