Sha256: c960e6ed7b8a30d33ce3ef800335f247bb93eb94930816fd7a53aa18fa3037e0

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module ActiveAdmin
  # This is a container for resources, which acts much like a Hash.
  # It's assumed that an added resource responds to `resource_name`.
  class ResourceCollection
    include Enumerable
    extend Forwardable
    def_delegators :@resources, :empty?, :has_key?, :keys, :values, :[]=

    def initialize
      @resources = {}
    end

    def add(resource)
      self[resource.resource_name] ||= resource
    end

    # Changes `each` to pass in the value, instead of both the key and value.
    def each(&block)
      values.each &block
    end

    # Finds a resource based on the resource name, the resource class, or the base class.
    def [](klass)
      if match = @resources[klass]
        match
      elsif match = real_resources.detect{ |r| r.resource_class.to_s == klass.to_s }
        match
      elsif klass.respond_to? :base_class
        real_resources.detect{ |r| r.resource_class.to_s == klass.base_class.to_s }
      end
    end

    private

    # REFACTOR: ResourceCollection currently stores Resource and Page objects. That doesn't
    # make sense, because by definition a ResourceCollection is a collection of resources.
    def real_resources
      select{ |r| r.respond_to? :resource_class }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aa-rails4-0.6.0 lib/active_admin/resource_collection.rb