Open itspriddle opened 12 years ago
I don't think it's good idea.
First: I don't want to depend on ActiveRecord (Attrtastic doesn't depend on ActiveRecord, you can pass POROs),
Second: if you don't provide sane to_s
for your objects, then you have similar problem with link_to
helper and other ways where you output your associated object (you have to pass method name instead of defaulting on to_s
).
You can archive the same with either:
:format
option, when you provide proper formatter for value, i.e:
def ar_formatter(object)
method_name = %w( full_name name ).find {|m| object.respond_to?(:m) }
object.send(method_name || "to_s")
end
:value
option, where you can provide symbol used as method name to retrieve value from objectOf course you can combine both of these, Attrtastic first applies :value
if it's present to retrieve actual value of attribute and then it is formatted according to :format
option (it can be false to no format at all an only call to_s
, or is formatted with some defaults)
I've been working on adding better support to attrtastic for attributes that are ActiveRecord associations. Currently, attrtastic just calls
.to_s
on them, and you end up with something like#<Post:0x007f893f477a78>
.Here are the relevant pieces of an app to illustrate what I'm talking about.
Given the following models/associations:
and given this ERB:
My changes would generate this HTML:
Here's the code that provides this behavior:
If that's something you think might be useful for other users, I'd be happy to clean it up and submit a proper patch.