fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Support automatic pluralization of resource types

Open amysutedja opened this issue 6 years ago • 11 comments

AMS produces pluralized resource types by default for all serialized types, whereas fast_jsonapi does not. This makes migration from AMS to fast_jsonapi cumbersome, especially if you have a lot of types.

My original plan was to just write some code to perform the pluralization transform automatically, but it turns out that FastJsonapi::ObjectSerializer calls set_type rather aggressively, which results in my automatic pluralization changes being either ignored or clobbered.

What do you all think about just supporting pluralization of the type directly?

amysutedja avatar Aug 15 '18 01:08 amysutedja

@amysutedja why don't you add a new option to the serializer called pluralize_type: true by default let it be false that way existing behavior would be retained. Is this something you could contribute?

shishirmk avatar Aug 23 '18 03:08 shishirmk

@shishirmk @amysutedja I'd be happy to take this!

manojmj92 avatar Aug 23 '18 05:08 manojmj92

@manojmj92 go ahead!

shishirmk avatar Aug 31 '18 19:08 shishirmk

@manojmj92 hey! Thanks for taking time to work on this enhancement. Any updates on the automatic pluralization feature (or any idea of a workaround)?

Right now my team and I are setting each relationship's record_type manually, and we'd love to do it in a more programmatic way.

Thanks again!

brandonjyuhas avatar Oct 08 '18 16:10 brandonjyuhas

@manojmj92 If you haven’t taken a look at this yet, I have some ideas on a potential fix.

amysutedja avatar Oct 08 '18 22:10 amysutedja

I've not taken this up. Please feel free to work on this.

On Tue, Oct 9, 2018, 4:11 AM Amy Sutedja [email protected] wrote:

@manojmj92 https://github.com/manojmj92 If you haven’t taken a look at this yet, I have some ideas on a potential fix.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Netflix/fast_jsonapi/issues/301#issuecomment-428002100, or mute the thread https://github.com/notifications/unsubscribe-auth/AD2OwaUr7s5QdiC-KQfDu53V1Rzz_ZwTks5ui9SUgaJpZM4V9ayN .

manojmj92 avatar Oct 09 '18 01:10 manojmj92

@amysutedja can you please share your idea on this or if there is any update

ritesh2741 avatar Nov 17 '18 04:11 ritesh2741

I needed this as well so I shoved this into config/initializers/fast_jsonapi.rb:

# frozen_string_literal: true

# rubocop:disable Naming/PredicateName
# rubocop:disable Naming/AccessorMethodName

module FastJsonapi
  module ObjectSerializer
    class_methods do
      def pluralize(type_name, options = {})
        (options[:serializer] || options[:record_type] || type_name).to_s.pluralize
      end

      alias_method :original_set_type, :set_type
      def set_type(type_name)
        original_set_type pluralize(type_name)
      end

      alias_method :original_belongs_to, :belongs_to
      def belongs_to(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_belongs_to type_name, options, &block
      end

      alias_method :original_has_one, :has_one
      def has_one(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_has_one type_name, options, &block
      end

      alias_method :original_has_many, :has_many
      def has_many(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_has_many type_name, options, &block
      end
    end
  end
end

# rubocop:enable Naming/PredicateName
# rubocop:enable Naming/AccessorMethodName

UPDATE: Modified to use the serializer or record_type overrides before the original type_name for relationships.

PhilT avatar Dec 30 '18 21:12 PhilT

Is there anything else that's needed to get https://github.com/Netflix/fast_jsonapi/pull/376 merged in? I've got the same problem and would like to see this finalized. I'm willing to wrap things up if no one else is on this.

glyoko avatar Apr 04 '19 18:04 glyoko

This is an important feature that would really really help adoption of fast_jsonapi, considering that I think ember data assumes that type is always pluralized

sdhull avatar Jun 11 '19 18:06 sdhull

@PhilT your solution works well, except that if doesn't respect set_key_transform. The easy fix is to just wrap pluralize in run_key_transform.

# frozen_string_literal: true

# rubocop:disable Naming/PredicateName
# rubocop:disable Naming/AccessorMethodName

module FastJsonapi
  module ObjectSerializer
    class_methods do
      def pluralize(type_name, options = {})
        run_key_transform((options[:serializer] || options[:record_type] || type_name).to_s.pluralize)
      end

      alias_method :original_set_type, :set_type
      def set_type(type_name)
        original_set_type pluralize(type_name)
      end

      alias_method :original_belongs_to, :belongs_to
      def belongs_to(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_belongs_to type_name, options, &block
      end

      alias_method :original_has_one, :has_one
      def has_one(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_has_one type_name, options, &block
      end

      alias_method :original_has_many, :has_many
      def has_many(type_name, options = {}, &block)
        options[:record_type] = pluralize(type_name, options)
        original_has_many type_name, options, &block
      end
    end
  end
end

# rubocop:enable Naming/PredicateName
# rubocop:enable Naming/AccessorMethodName```

toneplex avatar Jun 25 '19 21:06 toneplex