class ActiveAdmin::ResourceCollection

Adding a resource assumes that the object responds to #resource_key
so it has some Array like qualities.
Holds on to a collection of Resources. Is an Enumerable object

def add(resource)

Parameters:
  • resource (Resource, Page) -- The resource to add to the collection
def add(resource)
  if has_key?(resource.resource_key)
    existing_resource = find_by_key(resource.resource_key)
    ensure_resource_classes_match!(existing_resource, resource)
    existing_resource
  else
    @resource_hash[resource.resource_key] = resource
  end
end

def each(&block)

For enumerable
def each(&block)
  @resource_hash.values.each(&block)
end

def ensure_resource_classes_match!(existing_resource, resource)

def ensure_resource_classes_match!(existing_resource, resource)
  return unless existing_resource.respond_to?(:resource_class) && resource.respond_to?(:resource_class)
  if existing_resource.resource_class != resource.resource_class
    raise ActiveAdmin::ResourceMismatchError, 
      "Tried to register #{resource.resource_class} as #{resource.resource_key} but already registered to #{existing_resource.resource_class}"
  end
end

def find_by_key(resource_key)

Finds a resource by a given key
def find_by_key(resource_key)
  @resource_hash[resource_key]
end

def find_by_resource_class(resource_class)

a subclass of an Active Record class (ie: implementes base_class)
Finds a resource based on it's class. Looks up the class Heirarchy if its
def find_by_resource_class(resource_class)
  resource_class_name = resource_class.to_s
  match = resources_with_a_resource_class.find{|r| r.resource_class.to_s == resource_class_name }
  return match if match
  if resource_class.respond_to?(:base_class)
    base_class_name = resource_class.base_class.to_s
    resources_with_a_resource_class.find{|r| r.resource_class.to_s == base_class_name }
  else
    nil
  end
end

def has_key?(resource_key)

@returns [Boolean] If the key has been registered in the collection
def has_key?(resource_key)
  @resource_hash.has_key?(resource_key)
end

def initialize

def initialize
  @resource_hash = {}
end

def keys

@returns [Array] An array of all the keys registered in the collection
def keys
  @resource_hash.keys
end

def resources

@returns [Array] An array of all the resources
def resources
  @resource_hash.values
end

def resources_with_a_resource_class

def resources_with_a_resource_class
  select{|resource| resource.respond_to?(:resource_class) }
end