-
Notifications
You must be signed in to change notification settings - Fork 532
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
fix: format model polymorphic type from resource object type #1435
Conversation
a45b85d
to
4d92877
Compare
@@ -743,13 +747,13 @@ class Boat < Vehicle | |||
end | |||
|
|||
class Document < ActiveRecord::Base | |||
has_many :pictures, as: :imageable | |||
has_many :pictures, as: :imageable # polymorphic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for greppability
@@ -578,6 +578,10 @@ def resource_type_for(model) | |||
end | |||
end | |||
|
|||
def polymorphic_type_for(model_name) | |||
model_name&.to_s&.classify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
@@ -1200,12 +1204,12 @@ def define_relationship_methods(relationship_name, relationship_klass, options) | |||
def define_foreign_key_setter(relationship) | |||
if relationship.polymorphic? | |||
define_on_resource "#{relationship.foreign_key}=" do |v| | |||
_model.method("#{relationship.foreign_key}=").call(v[:id]) | |||
_model.public_send("#{relationship.polymorphic_type}=", v[:type]) | |||
_model.public_send("#{relationship.foreign_key}=", v[:id]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the instantiation of the method object here in favor of public send since we couldn't think of a reason the method object would be preferable
_model.method("#{relationship.foreign_key}=").call(v[:id]) | ||
_model.public_send("#{relationship.polymorphic_type}=", v[:type]) | ||
_model.public_send("#{relationship.foreign_key}=", v[:id]) | ||
_model.public_send("#{relationship.polymorphic_type}=", self.class.polymorphic_type_for(v[:type])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔨 the fix
* test: failing request posting sti with polymorphic has one * fix: polymorphic resource assignment * Add polymorphic_type_for method * Favor classify over singularize.camelize --------- Co-authored-by: lgebhardt <[email protected]>
In our app which is currently on the latest 0.9 we have a test
failing on the 0-11-dev branch (which was also failing on 0-10)
which posts the JSON body
to
v1/broker-memberships
BrokerMembership is an STI ActiveRecord model
below Membership like below.
our BrokerMembership resource looks like
In
_replace_fields
you can see the parsedfield_data
isAnd in the call to
_replace_polymorphic_to_one_link
(as defined inJSONAPI::ResourceCommon)
calls the method defined by JSONAPI::ResourceCommon.define_on_resource like
which practially speaking calls
This latter behavior causes a failure since the 'organization_type' should be 'Broker' and not ':brokers'.
I'm not sure if this should be addressed in the definition of
define_on_resource
(by making it lookup the polymorphic type)or in the
JSONAPI::Request(Parser).parse_to_one_relationship
(by removing the unformat and calling classify) or elsewhere.All Submissions:
New Feature Submissions:
Bug fixes and Changes to Core Features:
Test Plan:
Reviewer Checklist: