ruby on rails - HAML: tr[@user] translates to class and ID, but how? -
as know, haml translates tr[@user]
<tr class="user" id="user_123">...
. how achieve this? through internal magic? or using functionality of @user
object?
what want achieve following. have contact
model company
, person
subclass. when using tr[@company]
, i'd haml following:
<tr class="contact company" id="company_123">
...instead of only:
<tr class="company" id="company_123">
is there easy way achieve this?
thank you.
you can implement haml_object_ref
method in model override haml uses value of class
attribute.
if use direct subclasses of contact
simple should work in contact
model:
def haml_object_ref "#{self.class.superclass.to_s.underscore} #{self.class.to_s.underscore}" end
(this uses activesupport underscore
method.)
if want use contact
instances directly, or subclasses of person
or company
, this:
def haml_object_ref classes = [] klass = self.class while klass <= contact classes << klass klass = klass.superclass end classes.map{|c| c.to_s.underscore}.join(' ') end
this produce class="contact"
instance of contact
, class="contact person'
instance of person
, class="contact person employee"
(hypothetical) employee
subclass of person
.
Comments
Post a Comment