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

Add serializer keyword argument as an alias for resource #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion lib/alba/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,16 @@ def attribute(name, **options, &block)
# @param name [String, Symbol] name of the association, used as key when `key` param doesn't exist
# @param condition [Proc, nil] a Proc to modify the association
# @param resource [Class<Alba::Resource>, String, Proc, nil] representing resource for this association
# @param serializer [Class<Alba::Resource>, String, Proc, nil] alias for `resource`
# @param key [String, Symbol, nil] used as key when given
# @param params [Hash] params override for the association
# @param options [Hash<Symbol, Proc>]
# @option options [Proc] if a condition to decide if this association should be serialized
# @param block [Block]
# @return [void]
# @see Alba::Association#initialize
def association(name, condition = nil, resource: nil, key: nil, params: {}, **options, &block)
def association(name, condition = nil, resource: nil, serializer: nil, key: nil, params: {}, **options, &block)
resource ||= serializer
transformation = @_key_transformation_cascade ? @_transform_type : :none
assoc = Association.new(
name: name, condition: condition, resource: resource, params: params, nesting: nesting, key_transformation: transformation, helper: @_helper, &block
Expand Down
3 changes: 2 additions & 1 deletion test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BarResource
include Alba::Resource
attributes :id
end
BarSerializer = BarResource

class FooResource
include Alba::Resource
Expand Down Expand Up @@ -111,7 +112,7 @@ class FooSerializer
include Alba::Serializer
root_key :foo
attributes :id, :bar_size
many :bars, resource: BarResource
has_many :bars, serializer: BarSerializer

def bar_size(foo)
foo.bars.size
Expand Down
Loading