fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

`set_key_transform` to transform `:meta` and `:links`

Open tomasc opened this issue 7 years ago • 0 comments

While switching to Fast JSON API from Active Model Serializers, I noticed that set_key_transform :dash does not transform :meta and :links – as I would expect.

Overriding the #hash_for_one_record and #hash_for_collection method solved this:

    def hash_for_one_record
      transform_meta_and_link_keys(super)
    end

    def hash_for_collection
      transform_meta_and_link_keys(super)
    end

    private

    def transform_meta_and_link_keys(serializable_hash)
      serializable_hash[:meta] = serializable_hash.fetch(:meta, {}).transform_keys { |k| self.class.run_key_transform(k) }
      serializable_hash[:links] = serializable_hash.fetch(:links, {}).transform_keys { |k| self.class.run_key_transform(k) }
      serializable_hash
    end

I would expect this to be the default behavior. If you agree, I would be happy to submit PR.

tomasc avatar Jan 02 '19 12:01 tomasc