lib/alba/association.rb in alba-3.1.0 vs lib/alba/association.rb in alba-3.2.0
- old
+ new
@@ -27,10 +27,15 @@
return if @resource
assign_resource(nesting, key_transformation, block, helper)
end
+ # This is the same API in `NestedAttribute`
+ def key_transformation=(type)
+ @resource.transform_keys(type) unless @resource.is_a?(Proc)
+ end
+
# Recursively converts an object into a Hash
#
# @param target [Object] the object having an association method
# @param within [Hash] determines what associations to be serialized. If not set, it serializes all associations.
# @param params [Hash] user-given Hash for arbitrary data
@@ -63,21 +68,25 @@
else
raise Error, "Unexpected resource type: #{resource.class}"
end
end
- def assign_resource(nesting, key_transformation, block, helper) # rubocop:disable Metrics/MethodLength
+ def assign_resource(nesting, key_transformation, block, helper)
@resource = if block
- klass = Alba.resource_class
- klass.helper(helper) if helper
- klass.transform_keys(key_transformation)
- klass.class_eval(&block)
- klass
+ charged_resource_class(helper, key_transformation, block)
elsif Alba.inflector
Alba.infer_resource_class(@name, nesting: nesting)
else
raise ArgumentError, 'When Alba.inflector is nil, either resource or block is required'
end
+ end
+
+ def charged_resource_class(helper, key_transformation, block)
+ klass = Alba.resource_class
+ klass.helper(helper) if helper
+ klass.transform_keys(key_transformation)
+ klass.class_eval(&block)
+ klass
end
def to_h_with_each_resource(object, within, params)
object.map do |item|
@resource.call(item).new(item, within: within, params: params).to_h