Skip to content

Commit

Permalink
WIP #1105 fix bug with inclusion and linkage of has_one polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
senid231 committed Sep 7, 2017
1 parent 38757c6 commit 097c228
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/jsonapi/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def self.polymorphic_types(name)
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
(hash[reflection.options[:as]] ||= []) << klass.name.downcase
end
klass.reflect_on_all_associations(:has_one).select{|r| r.options[:as] }.each do |reflection|
(hash[reflection.options[:as]] ||= []) << klass.name.downcase
end
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,10 @@ 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_id = v.nil? ? nil : v[:id]
model_type = v.nil? ? nil : self.class.resource_klass_for(v[:type].to_s)._model_class.to_s
_model.method("#{relationship.foreign_key}=").call(model_id)
_model.public_send("#{relationship.polymorphic_type}=", model_type)
end
else
define_on_resource "#{relationship.foreign_key}=" do |value|
Expand Down

0 comments on commit 097c228

Please sign in to comment.