Skip to content

Commit

Permalink
Remove resource_key, use resource_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Haller authored and gregbell committed Feb 25, 2012
1 parent b7bccff commit 76e7693
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
8 changes: 3 additions & 5 deletions lib/active_admin/resource/naming.rb
Expand Up @@ -2,6 +2,9 @@ module ActiveAdmin
class Resource
module Naming

# Returns a name used to uniquely identify this resource
# this should be an instance of ActiveMode::Name, which responds to
# #singular, #plural, #route_key, #human etc.
def resource_name
custom_name = @options[:as] && @options[:as].gsub(/\s/,'')
@resource_name ||= if custom_name || !resource_class.respond_to?(:model_name)
Expand All @@ -28,11 +31,6 @@ def plural_resource_label
(@plural_resource_label ||= {})[::I18n.locale] ||= resource_name.human(:count => 3, :default => resource_label.pluralize).titleize
end
end

# A name used internally to uniquely identify this resource
def resource_key
resource_name
end
end
end
end
20 changes: 10 additions & 10 deletions lib/active_admin/resource_collection.rb
Expand Up @@ -5,27 +5,27 @@ class ResourceMismatchError < StandardError; end
# Holds on to a collection of Resources. Is an Enumerable object
# so it has some Array like qualities.
#
# Adding a resource assumes that the object responds to #resource_key
# Adding a resource assumes that the object responds to #resource_name
class ResourceCollection
include Enumerable

def initialize
@resource_hash = {}
end

# Add a new resource to the collection. If the resource_key already
# Add a new resource to the collection. If the resource_name already
# exists, the exiting resource is returned.
#
# @param [Resource, Page] resource The resource to add to the collection
#
# @returns [Resource, Page] Either the existing resource or the new one
def add(resource)
if has_key?(resource.resource_key)
existing_resource = find_by_key(resource.resource_key)
if has_key?(resource.resource_name)
existing_resource = find_by_key(resource.resource_name)
ensure_resource_classes_match!(existing_resource, resource)
existing_resource
else
@resource_hash[resource.resource_key] = resource
@resource_hash[resource.resource_name] = resource
end
end

Expand All @@ -45,13 +45,13 @@ def keys
end

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

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

# Finds a resource based on it's class. Looks up the class Heirarchy if its
Expand Down Expand Up @@ -80,7 +80,7 @@ def ensure_resource_classes_match!(existing_resource, resource)

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}"
"Tried to register #{resource.resource_class} as #{resource.resource_name} but already registered to #{existing_resource.resource_class}"
end
end

Expand Down
22 changes: 11 additions & 11 deletions spec/unit/resource_collection_spec.rb
Expand Up @@ -12,19 +12,19 @@
end

it "should be enumerable" do
resource = mock(:resource_key => "MyResource")
resource = mock(:resource_name => "MyResource")
collection.add(resource)
collection.each{|r| r.should == resource }
end

it "should return the available keys" do
resource = mock(:resource_key => "MyResource")
resource = mock(:resource_name => "MyResource")
collection.add resource
collection.keys.should == [resource.resource_key]
collection.keys.should == [resource.resource_name]
end

describe "adding a new resource" do
let(:resource){ mock(:resource_key => "MyResource") }
let(:resource){ mock(:resource_name => "MyResource") }

it "should return the resource" do
collection.add(resource).should == resource
Expand All @@ -37,13 +37,13 @@

it "should be available by name" do
collection.add(resource)
collection.find_by_key(resource.resource_key).should == resource
collection.find_by_key(resource.resource_name).should == resource
end
end

describe "adding a new resource when the key already exists" do
let(:stored_resource){ mock(:resource_key => "MyResource") }
let(:resource){ mock(:resource_key => "MyResource") }
let(:stored_resource){ mock(:resource_name => "MyResource") }
let(:resource){ mock(:resource_name => "MyResource") }

before do
collection.add(stored_resource)
Expand All @@ -60,8 +60,8 @@
end

describe "adding an existing resource key with a different resource class" do
let(:stored_resource){ mock(:resource_key => "MyResource", :resource_class => mock) }
let(:resource){ mock(:resource_key => "MyResource", :resource_class => mock) }
let(:stored_resource){ mock(:resource_name => "MyResource", :resource_class => mock) }
let(:resource){ mock(:resource_name => "MyResource", :resource_class => mock) }

it "should raise a ActiveAdmin::ResourceMismatchError" do
collection.add(stored_resource)
Expand All @@ -75,9 +75,9 @@
describe "#find_by_resource_class" do

let(:base_class){ mock(:to_s => "BaseClass")}
let(:resource_from_base_class){ mock(:resource_key => "MyBaseClassResource", :resource_class => base_class )}
let(:resource_from_base_class){ mock(:resource_name => "MyBaseClassResource", :resource_class => base_class )}
let(:resource_class){ mock(:base_class => base_class, :to_s => "ResourceClass") }
let(:resource){ mock(:resource_key => "MyResource", :resource_class => resource_class) }
let(:resource){ mock(:resource_name => "MyResource", :resource_class => resource_class) }

it "should find a resource when it's in the collection" do
collection.add resource
Expand Down

0 comments on commit 76e7693

Please sign in to comment.