diff --git a/doc-src/HAML_REFERENCE.md b/doc-src/HAML_REFERENCE.md
index caa961f1..cbbad929 100644
--- a/doc-src/HAML_REFERENCE.md
+++ b/doc-src/HAML_REFERENCE.md
@@ -581,6 +581,29 @@ is compiled to:
Hello!
+If you require that the class be something other than the underscored
+object's class, you can implement the `haml_object_ref` method on the object.
+
+ # file: app/models/crazy_user.rb
+
+ class CrazyUser < ActiveRecord::Base
+ def haml_object_ref
+ "a_crazy_user"
+ end
+ end
+
+ -# file: app/views/users/show.haml
+
+ %div[@user]
+ Hello!
+
+is compiled to:
+
+
+ Hello!
+
+
+
## Doctype: `!!!`
When describing HTML documents with Haml,
diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb
index a1247675..a15809e2 100644
--- a/lib/haml/buffer.rb
+++ b/lib/haml/buffer.rb
@@ -278,7 +278,12 @@ RUBY
ref = ref[0]
# Let's make sure the value isn't nil. If it is, return the default Hash.
return {} if ref.nil?
- class_name = underscore(ref.class)
+ class_name =
+ if ref.respond_to?(:haml_object_ref)
+ ref.haml_object_ref
+ else
+ underscore(ref.class)
+ end
id = "#{class_name}_#{ref.id || 'new'}"
if prefix
class_name = "#{ prefix }_#{ class_name}"
diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb
index c9ebed66..22eea349 100644
--- a/test/haml/engine_test.rb
+++ b/test/haml/engine_test.rb
@@ -69,6 +69,11 @@ class EngineTest < Test::Unit::TestCase
}
User = Struct.new('User', :id)
+ class CustomHamlClass < Struct.new(:id)
+ def haml_object_ref
+ "my_thing"
+ end
+ end
def render(text, options = {}, &block)
scope = options.delete(:scope) || Object.new
@@ -755,6 +760,12 @@ END
render("%p[user]{:style => 'width: 100px;'} New User", :locals => {:user => user}))
end
+ def test_object_ref_with_custom_haml_class
+ custom = CustomHamlClass.new 42
+ assert_equal("My Thing
\n",
+ render("%p[custom]{:style => 'width: 100px;'} My Thing", :locals => {:custom => custom}))
+ end
+
def test_non_literal_attributes
assert_equal("\n",
render("%p{a2, a1, :a3 => 'baz'}/",