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

Consider evaluating association in serializer context #1378

Merged
merged 1 commit into from
Jan 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ Breaking changes:

Features:

- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0
- [#1378](https://github.com/rails-api/active_model_serializers/pull/1378) Change association blocks
to be evaluated in *serializer* scope, rather than *association* scope. (@bf4)
* Syntax changes from e.g.
`has_many :titles do customers.pluck(:title) end` (in #1356) to
`has_many :titles do object.customers.pluck(:title) end`
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this belong here?

Copy link
Member Author

Choose a reason for hiding this comment

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

why wouldn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because this PR is #1378 and not #1356

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh... They're just missing from changelog. How would you prefer i include them?

B mobile phone

On Dec 31, 2015, at 12:05 PM, Lucas Hosseini notifications@github.com wrote:

In CHANGELOG.md:

@@ -16,7 +16,22 @@ Breaking changes:

Features:

-- #1336 Added support for Grape >= 0.13, < 1.0
+- #1378 Change association blocks


Reply to this email directly or view it on GitHub.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it to my local master and merged locally so it's two commits, but not part of this PR merge commit

attributes and associations (@bf4 @beauby @noahsilas)
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
:title do 'Mr. Topum Hat' end`
* Allows defining associations so that they don't conflict with existing methods. e.g. `has_many
:titles do customers.pluck(:title) end`
* Allows dynamic associations, as compared to compare to using
[`virtual_value`](https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r47146466).
e.g. `has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]`
* Removes dynamically defined methods on the serializer
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0 (@johnhamelink)
- [#1291](https://github.com/rails-api/active_model_serializers/pull/1291) Add logging (@maurogeorge)
- [#1225](https://github.com/rails-api/active_model_serializers/pull/1225) Better serializer lookup, use nested serializer when it exists (@beauby)
- [#1172](https://github.com/rails-api/active_model_serializers/pull/1172) Better serializer registration, get more than just the first module (@bf4)
Expand Down
8 changes: 4 additions & 4 deletions lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class Serializer
# has_one :author, serializer: AuthorSerializer
# has_many :comments
# has_many :comments, key: :last_comments do
# last(1)
# object.comments.last(1)
# end
# end
#
# Notice that the association block is evaluated in the context of the association.
# Notice that the association block is evaluated in the context of the serializer.
# Specifically, the association 'comments' is evaluated two different ways:
# 1) as 'comments' and named 'comments'.
# 2) as 'comments.last(1)' and named 'last_comments'.
# 2) as 'object.comments.last(1)' and named 'last_comments'.
#
# PostSerializer._reflections #=>
# # [
Expand All @@ -29,7 +29,7 @@ class Serializer
# @api private
def value(instance)
if block
instance.read_attribute_for_serialization(name).instance_eval(&block)
instance.instance_eval(&block)
else
instance.read_attribute_for_serialization(name)
end
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Profile < Model
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description

# TODO: is this used anywhere?
def arguments_passed_in?
instance_options[:my_options] == :accessible
end
Expand Down Expand Up @@ -75,6 +76,7 @@ def blog
Blog.new(id: 999, name: 'Custom blog')
end

# TODO: is this used anywhere?
def custom_options
instance_options
end
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_associations_custom_keys
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
has_many :comments
has_many :comments, key: :last_comments do
last(1)
object.comments.last(1)
end
end

Expand Down