Ruby on Rails Saturday, November 27, 2010

On Fri, Nov 26, 2010 at 11:42 AM, Frederick Cheung <frederick.cheung@gmail.com> wrote:



On Nov 26, 4:13 pm, Rick DeNatale <rick.denat...@gmail.com> wrote:
> 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


 

Fred


> I've failed to find anything like this in the rails docs.
>
> Any ideas what this might have been?
>
> --
> Rick DeNatale
>
> Blog:http://talklikeaduck.denhaven2.com/
> Github:http://github.com/rubyredrick
> Twitter: @RickDeNatale
> WWR:http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn:http://www.linkedin.com/in/rickdenatale

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment