-
Notifications
You must be signed in to change notification settings - Fork 463
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
Serialization options to include/except attributes with arrange_serializable #213
Comments
Hi @ArTiSTiX I'm looking to do something like this too. I normally use https://github.com/rails-api/active_model_serializers with my usual controllers etc, but I'm not yet sure how to integrate these two gems yet... Can anyone suggest a way to do this? Cheers, Rikki |
I've just run into this too - my solution was: # On the model
def self.arrange_custom_serializable options={}, nodes=nil, &block
nodes = arrange(options) if nodes.nil?
nodes.map do |parent, children|
yield parent, arrange_custom_serializable(options, children, &block)
end
end On the controller data = item.subtree.arrange_custom_serializable do |parent, children|
ModelSerializer.new(parent, children: children)
end Inside the serializer attributes :children
def children
return @options[:children]
end This gives a nice serializer tree with the a root node of |
I don't think Ancestry should be concerned with serialization. If you want to arrange but have access to full models with eager loaded associations, why not use regular |
I agree completely with having ancestry not handle serialization; I'm suggesting accepting a block in Something like: def arrange_serializable options={}, nodes=nil, &block
nodes = arrange(options) if nodes.nil?
nodes.map do |parent, children|
if block_provided?
yield parent, arrange_serializable(options, children, &block)
else
parent.serializable_hash.merge 'children' => arrange_serializable(options, children)
end
end
end |
Great idea! Thx for the explanation. If you provide a pull request with tests and docs I'll merge it. |
I'm currently having some difficulties with arrange_serializable.
I eager load some of relations of the hierarchical model... but there seems to be no options to add those fields in serialization...
I'm doing:
But user relation is not serialized.
I expect something like:
Any practice to do this with arrange_serializable ?
The text was updated successfully, but these errors were encountered: