jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

Make extract! accept objects with internal hash implementation

Open bacchir opened this issue 6 years ago • 2 comments

In a recent project I've experienced an odd situation of having an object that implements some Hash behavior and, even though it has methods such as [] and fetch, jbuilder.extract! did not work with it. So I just created this little PR with some tests to make jbuilder more agnostic.

Here's an example (also used in the tests) of the object I'm trying to pass to jbuilder:

class PersonWithHash
  attr_reader :name, :collection

  def initialize(name, age)
    @collection = { age: age }
    @name = name
  end

  delegate :[], :fetch, to: :@collection
end

bacchir avatar Jun 20 '18 10:06 bacchir

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

rails-bot avatar Jun 20 '18 10:06 rails-bot

@robin850 I changed the implementation the way you suggested, however I changed respond_to?(:fetch) to respond_to?(:[]) to avoid problems when an object have a custom fetch method for other purposes.

bacchir avatar Dec 08 '18 19:12 bacchir