-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic field support #164
Changes from all commits
b95e423
15ff212
95ab65a
8554022
cf96ba1
3bfe5c6
97a97c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Blueprinter | ||
# @api private | ||
class Transformer | ||
def transform(result_hash,primary_obj, options={}) | ||
fail NotImplementedError, "A Transformer must implement #transform" | ||
end | ||
|
||
def self.transform(result_hash,primary_obj, options={}) | ||
self.new.transform(result_hash,primary_obj, options) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
module Blueprinter | ||
# @api private | ||
class View | ||
attr_reader :excluded_field_names, :fields, :included_view_names, :name | ||
attr_reader :excluded_field_names, :fields, :included_view_names, :name, :transformers | ||
|
||
def initialize(name, fields: {}, included_view_names: [], excluded_view_names: []) | ||
def initialize(name, fields: {}, included_view_names: [], excluded_view_names: [],transformers: []) | ||
@name = name | ||
@fields = fields | ||
@included_view_names = included_view_names | ||
@excluded_field_names = excluded_view_names | ||
@transformers = transformers | ||
end | ||
|
||
def inherit(view) | ||
|
@@ -22,6 +23,10 @@ def inherit(view) | |
view.excluded_field_names.each do |field_name| | ||
exclude_field(field_name) | ||
end | ||
|
||
view.transformers.each do |transformer| | ||
self.add_transformer(transformer) | ||
end | ||
end | ||
|
||
def include_view(view_name) | ||
|
@@ -38,6 +43,10 @@ def exclude_fields(field_names) | |
end | ||
end | ||
|
||
def add_transformer(custom_transformer) | ||
transformers << custom_transformer | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've never seen Otherwise, can we come up with a method name that makes clear that it is appending transformer here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am usually not into operator overloading, but then I was inspired by the method written for adding fields into the view There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you rename to |
||
|
||
def <<(field) | ||
fields[field.name] = field | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
FactoryBot.define do | ||
factory :user do | ||
first_name { 'Meg' } | ||
last_name { 'Jones' } | ||
last_name { 'Ryan' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
position { 'Manager' } | ||
description { 'A person' } | ||
company { 'Procore' } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,4 +425,22 @@ def self.unless_method(_field_name, _object, _options) | |
end | ||
it('returns json with values derived from options') { should eq(result) } | ||
end | ||
|
||
context 'Given blueprint has a transformer' do | ||
subject { blueprint.render(obj) } | ||
let(:result) { '{"id":' + obj_id + ',"full_name":"Meg Ryan"}' } | ||
let(:blueprint) do | ||
DynamicFieldsTransformer = Class.new(Blueprinter::Transformer) do | ||
def transform(result_hash, object, options={}) | ||
dynamic_fields = (object.is_a? Hash) ? object[:dynamic_fields] : object.dynamic_fields | ||
result_hash.merge!(dynamic_fields) | ||
end | ||
end | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
transform DynamicFieldsTransformer | ||
end | ||
end | ||
it('returns json with values derived from options') { should eq(result) } | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you also add a test to ensure that the transformers are also inherited? There's some inheritance specs here that you can add to: https://github.com/procore/blueprinter/blame/4ba12dd14edf340f9a4a95f1668785adc8c21ca6/spec/integrations/base_spec.rb#L326-L348 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test case has been added. |
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
DynamicTransformer
? (ortransform DynamicFieldTransformer
above)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, example mismatch. would appreciate if you can make a PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, see #174