fast_jsonapi
fast_jsonapi copied to clipboard
Support automatic pluralization of resource types
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 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 @amysutedja I'd be happy to take this!
@manojmj92 go ahead!
@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!
@manojmj92 If you haven’t taken a look at this yet, I have some ideas on a potential fix.
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 .
@amysutedja can you please share your idea on this or if there is any update
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.
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.
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
@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```