Helper to generate css-id from AR model. Do I have alzheimers?

On one of the many Rails projects I've worked on over the years, I seem to recall using either a helper or a method on a model to generate a css id based on the model class and id. It was very useful particularly in making views more testable in RSpec/Cucumber.

I can't remember if this was part of rails, or from a plugin or something specific to the project.

I've failed to find anything like this in the rails docs.

Any ideas what this might have been?

On one of the many Rails projects I've worked on over the years, I seem to recall using either a helper or a method on a model to generate a css id based on the model class and id. It was very useful particularly in making views more testable in RSpec/Cucumber.

I can't remember if this was part of rails, or from a plugin or something specific to the project.

Are you thinking of dom_id / dom_class ?

Fred

Yes indeed, thanks!

Rick Denatale wrote in post #964140:

Are you thinking of dom_id / dom_class ?

Yes indeed, thanks!

If you're using Haml, it's even easier: just use the [@object] syntax.

-- Rick DeNatale

Best,

On one of the many Rails projects I’ve worked on over the years, I

seem to recall using either a helper or a method on a model to

generate a css id based on the model class and id. It was very useful

particularly in making views more testable in RSpec/Cucumber.

I can’t remember if this was part of rails, or from a plugin or

something specific to the project.

Are you thinking of dom_id / dom_class ?

Interesting… was not aware of this. Just today was writing a helper method to create a friendly id for an object I am writing to a table row. The challenge with dom_id/class is that when using Cucumber you really do not know (without making a good guess) what the id of a record will be.

I am curious if anyone has done or thought of the following for models, something where one defines what fields would constitute a friendly field id which does not involve the index but fields that would be known in a Cucumber scenario? I seem to be writing helpers for this quite often as it seems like often the best strategy for checking for something on a page is to place the data with an id or as a child of some object with a specific id or class. I am not quite sure how to do this as it seems it would have to extend ActiveModel/ActiveRecord…or maybe be done in a module which can be aware of what constituted the friendly_id of a model:

include FriendlyId class Borrower < ActiveRecord::Base friendly_id :last_name, :first_name # so in each model you just define what fields of the model will constitute the friendly id end

module FriendlyId

def friendly_id # how do I access the ‘friendly_id’ settings from the model I am mixed in to? end end

borrower = Borrower.new(:last_name => “Kahn”, :first_name => "David) # using ActiveRecord/ActiveModel

borrower.friendly_id

“kahn_david”

or

friendly_id(borrower)

David