Sha256: 980ec103a43eada6d8cf9fe84a02d51d37c1ce8c02da9de42e59c54e264582fa

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module ActiveAdmin
  # This class decorates a collection of objects delegating
  # mehods to behave like an Array. It's used to decouple ActiveAdmin
  # from Draper and thus being able to use PORO decorators as well.
  #
  # It's implementation is heavily based on the Draper::CollectionDecorator
  # https://github.com/drapergem/draper/blob/aaa06bd2f1e219838b241a5534e7ca513edd1fe2/lib/draper/collection_decorator.rb
  class CollectionDecorator
    # @return the collection being decorated.
    attr_reader :object

    # @return [Class] the decorator class used to decorate each item, as set by {#initialize}.
    attr_reader :decorator_class

    array_methods = Array.instance_methods - Object.instance_methods
    delegate :==, :as_json, *array_methods, to: :decorated_collection

    def initialize(object, with:)
      @object = object
      @decorator_class = with
    end

    class << self
      alias_method :decorate, :new
    end

    def decorated_collection
      @decorated_collection ||= object.map { |item| decorator_class.new(item) }
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/collection_decorator.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/collection_decorator.rb
activeadmin-2.9.0 lib/active_admin/collection_decorator.rb
activeadmin-2.8.1 lib/active_admin/collection_decorator.rb
activeadmin-2.8.0 lib/active_admin/collection_decorator.rb