Skip to content

Commit

Permalink
Lookup superclasses when determining #admin_for instance
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed May 14, 2018
1 parent 07c0e77 commit 0cad70f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/helpers/trestle/url_helper.rb
Expand Up @@ -49,7 +49,17 @@ def admin_url_for(instance, options={})
end

def admin_for(instance)
Trestle.admins[instance.class.name.underscore.pluralize]
klass = instance.class

while klass
admin = Trestle.admins[klass.name.underscore.pluralize]
return admin if admin

klass = klass.superclass
end

# No admin found
nil
end
end
end
16 changes: 14 additions & 2 deletions spec/trestle/helpers/url_helper_spec.rb
Expand Up @@ -120,10 +120,22 @@
end

describe "#admin_for" do
let(:instance) { double(class: double(name: "MyClass")) }
before(:each) do
allow(Trestle).to receive(:admins).and_return({ "my_classes" => admin})
end

it "returns the admin associated with an object's class type" do
expect(Trestle).to receive(:admins).and_return({ "my_classes" => admin})
klass = double(name: "MyClass", superclass: nil)
instance = double(class: klass)

expect(admin_for(instance)).to eq(admin)
end

it "tries the object class's ancestors" do
parent = double(name: "MyClass", superclass: nil)
klass = double(name: "Subclass", superclass: parent)
instance = double(class: klass)

expect(admin_for(instance)).to eq(admin)
end
end
Expand Down

0 comments on commit 0cad70f

Please sign in to comment.