Sha256: 2e76503cdd9d717a4ee2bf116da0e737e9bb34629acf70d44827654c03497ed4

Contents?: true

Size: 806 Bytes

Versions: 11

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.object.send(association)
      associated = associated.send(scope) if scope

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

  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
draper-3.0.1 lib/draper/decorated_association.rb
draper-3.0.0 lib/draper/decorated_association.rb
draper-3.0.0.pre1 lib/draper/decorated_association.rb
draper_new-3.0.0 lib/draper/decorated_association.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/draper-2.1.0/lib/draper/decorated_association.rb
draper-2.1.0 lib/draper/decorated_association.rb
draper-2.0.0 lib/draper/decorated_association.rb
draper-1.4.0 lib/draper/decorated_association.rb
draper-1.3.1 lib/draper/decorated_association.rb
draper-1.3.0 lib/draper/decorated_association.rb
draper-1.2.1 lib/draper/decorated_association.rb