Sha256: 145ca28b2fcf0419dc2b27ab018c65c00591af34f33378888ed5929d8bcf841d

Contents?: true

Size: 1.08 KB

Versions: 22

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true
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

22 entries across 22 versions & 1 rubygems

Version Path
activeadmin-3.2.5 lib/active_admin/collection_decorator.rb
activeadmin-3.2.4 lib/active_admin/collection_decorator.rb
activeadmin-3.2.3 lib/active_admin/collection_decorator.rb
activeadmin-3.2.2 lib/active_admin/collection_decorator.rb
activeadmin-3.2.1 lib/active_admin/collection_decorator.rb
activeadmin-4.0.0.beta5 lib/active_admin/collection_decorator.rb
activeadmin-4.0.0.beta4 lib/active_admin/collection_decorator.rb
activeadmin-4.0.0.beta3 lib/active_admin/collection_decorator.rb
activeadmin-4.0.0.beta2 lib/active_admin/collection_decorator.rb
activeadmin-4.0.0.beta1 lib/active_admin/collection_decorator.rb
activeadmin-3.2.0 lib/active_admin/collection_decorator.rb
activeadmin-3.1.0 lib/active_admin/collection_decorator.rb
activeadmin-3.0.0 lib/active_admin/collection_decorator.rb
activeadmin-2.14.0 lib/active_admin/collection_decorator.rb
activeadmin-2.13.1 lib/active_admin/collection_decorator.rb
activeadmin-2.13.0 lib/active_admin/collection_decorator.rb
activeadmin-2.12.0 lib/active_admin/collection_decorator.rb
activeadmin-2.11.2 lib/active_admin/collection_decorator.rb
activeadmin-2.11.1 lib/active_admin/collection_decorator.rb
activeadmin-2.11.0 lib/active_admin/collection_decorator.rb