Sha256: 8ea0996a7a5ae884e1dab7efc9854ce4b7db9898ba33f4edf048700e805d1a5e

Contents?: true

Size: 804 Bytes

Versions: 6

Compression:

Stored size: 804 Bytes

Contents

module Draper
  # @private
  class DecoratedAssociation
    def initialize(owner, association, options)
      options.assert_valid_keys(:with, :scope, :context)

      @owner = owner
      @association = association

      @scope = options[:scope]

      decorator_class = options[:with]
      context = options.fetch(:context, ->(context){ context })
      @factory = Draper::Factory.new(with: decorator_class, context: context)
    end

    def call
      decorate unless defined?(@decorated)
      @decorated
    end

    private

    attr_reader :factory, :owner, :association, :scope

    def decorate
      associated = owner.object.send(association)
      associated = associated.send(scope) if scope

      @decorated = factory.decorate(associated, context_args: owner.context)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
draper-4.0.4 lib/draper/decorated_association.rb
draper-4.0.3 lib/draper/decorated_association.rb
draper-4.0.2 lib/draper/decorated_association.rb
draper-4.0.1 lib/draper/decorated_association.rb
draper-4.0.0 lib/draper/decorated_association.rb
draper-3.1.0 lib/draper/decorated_association.rb