-
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
Any way to configure/perform something like key_transform in AMS? #158
Comments
The only supported way is to pass in the
But it sounds like you need to rename them in a lot of places, so it may not be easy to do it everywhere. Here is an unsupported solution (monkey patch), however this is at app initialization level: # config/initializers/blueprinter.rb
Module Blueprinter
class Field
def name
@name.camelize
end
end
end If you're really looking for something dynamic, based on the API request header, you're really going to need a bigger monkey patch. You could override this https://github.com/procore/blueprinter/blame/80eef670cfedcda45528b925fa8c77394ed4a15a/lib/blueprinter/helpers/base_helpers.rb#L46-L51 to something like this: # config/initializers/blueprinter.rb
module Blueprinter
class Base
def self.object_to_hash(object, view_name:, local_options:)
view_collection.fields_for(view_name).each_with_object({}) do |field, hash|
next if field.skip?(object, local_options)
name = if local_options[:camelize]
field.name.to_s.camelize.to_sym
else
field.name.to_s.underscore.to_sym
end
hash[name] = field.extract(object, local_options)
end
end
end
end
# in your controller
def show
render json: SomeBlueprint.render(yourObject, camelize: true)
end Future versions of blueprinter could break my suggestions above because these are private methods you'd be overriding. |
@philipqnguyen I used your suggested solution with 🐒 Monkey patching as I wanted all fields to be # config/initializers/blueprinter.rb
module Blueprinter
class Field
def name
@name.to_s.camelize(:lower)
end
end
end |
For anyone who stumbled on this like I did, there's another way to do it now that does not require any monkey patching, using the
I set this option in a |
Your solution worked for me in most cases, but I've found that using a view seems to break the functionality. For example: class CompanyBlueprint < BaseBlueprint
identifier :id
fields :name, :address
view :customer_view do
fields :name, :address, :industry_name
end
end
# Elsewhere:
CompanyBlueprint.render(@company, view: :customer_view) In the case above, |
Interesting. I'm new to this gem and haven't used views yet. My first thought would be to try defining the transform inside the |
@stlewis |
That worked, thank you! |
Nice! Love it when something works that intuitively. |
This could be dangerous and break the functionality of other gems/engines using Blueprinter |
jbuilder does this hilariously easily:
is it not something the library can consider having out-of-the-box? my use case is that the developer chooses which format they want as the response, when calling my api |
We're switching off AMS, and we made heavy use of the key_transform feature it had, allowing us to CamelCase or underscore_case keys as specified by a request header we bubble up.
Now that we're moving to Blueprinter, is there a clean way to manipulate keys returned by blueprinter? if not, is there a less than clean way to do so, other than revisiting all the keys in the serialized objects, and transforming them after the fact?
Thanks,
David
The text was updated successfully, but these errors were encountered: