Skip to content
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

Sideloading associations #1088

Closed
wants to merge 2 commits into from

Conversation

NullVoxPopuli
Copy link
Contributor

Hey all, since this is a highly requested feature, I figured I'd take a stab at it since I need it for my own ember work.

Thoughts on the implementation?

The goal of this is to address needs of #938 and #899

Usage

#config/initializers/active_model_serializers.rb
ActiveModel::Serializer.config.adapter = :json
ActiveModel::Serializer.config.sideload_associations = true

And that's it. :-)
No need to have embed: :ids on all the associations.

@ZackMattor
Copy link

👍 This is working for me so far in my application! I had to tweak one thing though. I have a has_one and this sideloads it as a singleton object as apposed to an array. Before I'm pretty sure it still stuck it in an array.

Had to do the following hack...

  has_many :foos

  def foos
    [ object.foo ]
  end

VS

  has_one :foo

@NullVoxPopuli
Copy link
Contributor Author

@criten could you elaborate? I haven't used a has_one in my app yet, so I'm uncertain what the exact issue you found is / was

@ZackMattor
Copy link

Just edited my comment.

@NullVoxPopuli
Copy link
Contributor Author

interesting. Does has_one work in normal AMS, nested the data under foo?

@ZackMattor
Copy link

Yeah.. just checked. To be a bit more clear....

# 0.9.X
class FooSerializer < ActiveModel::Serializer
  has_one :bar
end
bar: {
  foo_id: 1
},
foos:
[{
  id: 1
}]

VS

# 0.10.X
class FooSerializer < ActiveModel::Serializer
  has_one :foo
end
bar: {
  foo_id: 1
},
foo:
{
  id: 1
}

@ZackMattor
Copy link

I think that's what its doing....

@NullVoxPopuli
Copy link
Contributor Author

kk, I'll see if I can come up with a test and fix it

Implementation idea for sideloading to help out JS Frameworks

added tests for sideloading and binding.pry for debugging

added configuration option for sideloading associations

switching computers

tests pass

revert require of dev-friendly gems - as they aren't as compatible as AMS is aiming to be

also fixed a bug introduced by the sideloading - no existing tests caught the bug before initial push

added a test for has_one
@NullVoxPopuli
Copy link
Contributor Author

@criten I added a test but was not able to replicate. :-(

Is there anyway you have time to monkey around with the sideload_test.rb tests and see if you can replicate in there?

(I did add two more tests, though, for has_one and belongs_to)

@estum
Copy link

estum commented Aug 28, 2015

Heh, About a week ago I've started to make my own implementation of the same thing. Currently it is documented but doesn't contain the proper specs, that is why I haven't yet made a PR. In general It works for me, but doesn't support custom names of associations and excludes not sideloaded reflections from a "relationships" object. Also, I've added a sideloading to the SerializedResource's initialization process and now doubt about this decision.

@BenMorganIO
Copy link

👍

@ZackMattor
Copy link

I don't think this is working for me with nested has_many? Do you know if that works?

@NullVoxPopuli
Copy link
Contributor Author

Does regular AMS work with nested has many? When I was looking through the
code, I don't think I saw any sort of recursion on the serialization bit. I
could be wrong though.

I do know that tested has many doesn't work. But I also don't know if it's
pre existing.

On Fri, Aug 28, 2015, 5:00 PM Zachary Mattor [email protected]
wrote:

I don't think this is working for me with nested has_many? Do you know if
that works?


Reply to this email directly or view it on GitHub
#1088 (comment)
.

@ZackMattor
Copy link

It definitely worked in version 0.9...

@NullVoxPopuli
Copy link
Contributor Author

I'll take a look at the tests on master when I can, and see if there is a
nested serialization gap. :-)

On Fri, Aug 28, 2015, 5:06 PM Zachary Mattor [email protected]
wrote:

It definitely worked in version 0.9...


Reply to this email directly or view it on GitHub
#1088 (comment)
.

@NullVoxPopuli
Copy link
Contributor Author

@criten it looks like nested has many is broken in master: NullVoxPopuli@15147de
(commit for a failing test)

if sideload
association_ids = {}
@hash.map do |association_name, associated_models|
# TODO: use active support inflectors?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes, but I'm concerned with ppl using AMS outside Rails context.
I'd keep it like that and come back at it after Rails 5 release

@NullVoxPopuli
Copy link
Contributor Author

Closing, in favor of an implementation based off my refactored work (#1111)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants