Displaying associated records

1 view
Skip to first unread message

george.anderson

unread,
Aug 23, 2009, 9:27:15 PM8/23/09
to admin_assistant
I couldn't find this in the docs, but it's likely there and I missed
it. To display a custom attribute for associated records, define a
'name_for_admin_assistant' method. Otherwise, it will look for (in
order) a method named :name, :title, :login, or :username (likely
coming from the database courtesy of ActiveRecord), using the first
one that responds.

For example:

class Subtenant < ActiveRecord::Base
has_many :applications, :dependent => :destroy

def name_for_admin_assistant
sub_tenant_name
end
end

class Application < ActiveRecord::Base
belongs_to :subtenant
end

class Admin::ApplicationsController < ApplicationController
admin_assistant_for Application do |aa|
aa.actions << :destroy
end
end

Now the URL '/admin/applications' will use
Subtenant.name_for_admin_assistant to display the various Subtenant
values

Here's the admin_assistant code that handles this:
# association_target.rb
def assoc_value(assoc_value)
if assoc_value.respond_to?(:name_for_admin_assistant)
assoc_value.name_for_admin_assistant
elsif assoc_value && default_name_method
assoc_value.send default_name_method
end
end

def default_name_method
[:name, :title, :login, :username].detect { |m|
@associated_class.columns.any? { |column| column.name.to_s ==
m.to_s }
}
end

I hope this helps someone.

Thanks for admin_assistant!

/g

--

George Anderson

BenevolentCode LLC
geo...@benevolentcode.com

Francis Hwang

unread,
Sep 18, 2009, 8:09:12 AM9/18/09
to admin_a...@googlegroups.com
George, thanks for the note. I'd actually added this very early on,
then accidentally removed a spec for it without removing the feature,
then missed it entirely when I was writing the docs later. It's
documented now on the site.

Francis Hwang
http://fhwang.net/
Reply all
Reply to author
Forward
0 new messages