Sha256: 084cf1c043e49775c70f33203529281f1452064c5ac780dfde2761029137ffdb

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Draper
  class DecoratedAssociation

    attr_reader :base, :association, :options

    def initialize(base, association, options)
      @base = base
      @association = association
      options.assert_valid_keys(:with, :scope, :context)
      @options = options
    end

    def call
      return undecorated if undecorated.nil?
      decorate
    end

    def source
      base.source
    end

    private

    def undecorated
      @undecorated ||= begin
        associated = source.send(association)
        associated = associated.send(options[:scope]) if options[:scope]
        associated
      end
    end

    def decorate
      @decorated ||= decorator_class.send(decorate_method, undecorated, decorator_options)
    end

    def decorate_method
      if collection? && decorator_class.respond_to?(:decorate_collection)
        :decorate_collection
      else
        :decorate
      end
    end

    def collection?
      undecorated.respond_to?(:first)
    end

    def decorator_class
      return options[:with] if options[:with]

      if collection?
        options[:with] = :infer
        Draper::CollectionDecorator
      else
        undecorated.decorator_class
      end
    end

    def decorator_options
      decorator_class # Ensures options[:with] = :infer for unspecified collections

      dec_options = collection? ? options.slice(:with, :context) : options.slice(:context)
      dec_options[:context] = base.context unless dec_options.key?(:context)
      if dec_options[:context].respond_to?(:call)
        dec_options[:context] = dec_options[:context].call(base.context) 
      end
      dec_options
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
draper-1.0.0.beta4 lib/draper/decorated_association.rb