lib/alba/association.rb in alba-2.0.0 vs lib/alba/association.rb in alba-2.0.1
- old
+ new
@@ -18,14 +18,13 @@
def initialize(name:, condition: nil, resource: nil, params: {}, nesting: nil, key_transformation: :none, &block)
@name = name
@condition = condition
@resource = resource
@params = params
- @key_transformation = key_transformation
return if @resource
- assign_resource(nesting, block)
+ assign_resource(nesting, key_transformation, block)
end
# Recursively converts an object into a Hash
#
# @param target [Object] the object having an association method
@@ -56,13 +55,16 @@
self.class.const_cache[resource] = Object.const_get(resource)
end
end
end
- def assign_resource(nesting, block)
+ def assign_resource(nesting, key_transformation, block)
@resource = if block
- Alba.resource_class(&block)
+ klass = Alba.resource_class
+ klass.transform_keys(key_transformation)
+ klass.class_eval(&block)
+ klass
elsif Alba.inferring
Alba.infer_resource_class(@name, nesting: nesting)
else
raise ArgumentError, 'When Alba.inferring is false, either resource or block is required'
end
@@ -74,10 +76,9 @@
end
end
def to_h_with_constantize_resource(within, params)
@resource = constantize(@resource)
- @resource.transform_keys(@key_transformation)
@resource.new(object, params: params, within: within).to_h
end
end
end