Sha256: be554b3863bd66d2efff7273ad08d23bd4eb77980f17bb672816c16d0bca5676

Contents?: true

Size: 806 Bytes

Versions: 2

Compression:

Stored size: 806 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.source.send(association)
      associated = associated.send(scope) if scope

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

  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
draper-1.2.0 lib/draper/decorated_association.rb
jamesgolick-draper-1.1.1a lib/draper/decorated_association.rb