-
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
Changes from 6 commits
4d92877
4e65502
85c067c
2e54a15
050d4a5
c0ca438
de1df89
9e98c0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -561,7 +561,7 @@ def resource_klass_for_model(model) | |
end | ||
|
||
def _resource_name_from_type(type) | ||
"#{type.to_s.underscore.singularize}_resource".camelize | ||
"#{type.to_s.classify}Resource" | ||
end | ||
|
||
def resource_type_for(model) | ||
|
@@ -578,6 +578,10 @@ def resource_type_for(model) | |
end | ||
end | ||
|
||
def polymorphic_type_for(model_name) | ||
model_name&.to_s&.classify | ||
end | ||
|
||
attr_accessor :_attributes, | ||
:_relationships, | ||
:_type, | ||
|
@@ -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 commentThe 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.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 commentThe reason will be displayed to describe this comment to others. Learn more. 🔨 the fix |
||
end | ||
else | ||
define_on_resource "#{relationship.foreign_key}=" do |value| | ||
_model.method("#{relationship.foreign_key}=").call(value) | ||
_model.public_send("#{relationship.foreign_key}=", value) | ||
end | ||
end | ||
relationship.foreign_key | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,7 @@ | |
t.string :drive_layout | ||
t.string :serial_number | ||
t.integer :person_id | ||
t.references :imageable, polymorphic: true, index: true | ||
t.timestamps null: false | ||
end | ||
|
||
|
@@ -734,6 +735,9 @@ class Picture < ActiveRecord::Base | |
|
||
class Vehicle < ActiveRecord::Base | ||
belongs_to :person | ||
belongs_to :imageable, polymorphic: true | ||
# belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ) }, foreign_key: 'imageable_id' | ||
# belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ) }, foreign_key: 'imageable_id' | ||
end | ||
|
||
class Car < Vehicle | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. just for greppability |
||
belongs_to :author, class_name: 'Person', foreign_key: 'author_id' | ||
has_one :file_properties, as: :fileable | ||
end | ||
|
||
class Product < ActiveRecord::Base | ||
has_many :pictures, as: :imageable | ||
has_many :pictures, as: :imageable # polymorphic | ||
belongs_to :designer, class_name: 'Person', foreign_key: 'designer_id' | ||
has_one :file_properties, as: :fileable | ||
end | ||
|
@@ -1336,6 +1340,7 @@ class VehicleResource < JSONAPI::Resource | |
immutable | ||
|
||
has_one :person | ||
has_one :imageable, polymorphic: true | ||
attributes :make, :model, :serial_number | ||
end | ||
|
||
|
@@ -1915,6 +1920,8 @@ class PreferencesResource < PreferencesResource; end | |
class SectionResource < SectionResource; end | ||
class TagResource < TagResource; end | ||
class CommentResource < CommentResource; end | ||
class DocumentResource < DocumentResource; end | ||
class ProductResource < ProductResource; end | ||
class VehicleResource < VehicleResource; end | ||
class CarResource < CarResource; end | ||
class BoatResource < BoatResource; end | ||
|
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.
💯