lib/alba/association.rb in alba-2.3.0 vs lib/alba/association.rb in alba-2.4.0
- old
+ new
@@ -1,10 +1,12 @@
module Alba
# Representing association
+ # @api private
class Association
@const_cache = {}
class << self
+ # cache for `const_get`
attr_reader :const_cache
end
attr_reader :object, :name
@@ -12,19 +14,20 @@
# @param condition [Proc, nil] a proc filtering data
# @param resource [Class<Alba::Resource>, nil] a resource class for the association
# @param params [Hash] params override for the association
# @param nesting [String] a namespace where source class is inferred with
# @param key_transformation [Symbol] key transformation type
+ # @param helper [Module] helper module to include
# @param block [Block] used to define resource when resource arg is absent
- def initialize(name:, condition: nil, resource: nil, params: {}, nesting: nil, key_transformation: :none, &block)
+ def initialize(name:, condition: nil, resource: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block)
@name = name
@condition = condition
@resource = resource
@params = params
return if @resource
- assign_resource(nesting, key_transformation, block)
+ assign_resource(nesting, key_transformation, block, helper)
end
# Recursively converts an object into a Hash
#
# @param target [Object] the object having an association method
@@ -57,12 +60,13 @@
self.class.const_cache[resource] = Object.const_get(resource)
end
end
end
- def assign_resource(nesting, key_transformation, block)
+ def assign_resource(nesting, key_transformation, block, helper) # rubocop:disable Metrics/MethodLength
@resource = if block
klass = Alba.resource_class
+ klass.helper(helper) if helper
klass.transform_keys(key_transformation)
klass.class_eval(&block)
klass
elsif Alba.inflector
Alba.infer_resource_class(@name, nesting: nesting)