Forum: Ruby on Rails Where to put method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lieven D. (Guest)
on 2006-01-21 18:01
I've got following code in GenresHelper.rb

  def album_list(genre)
    albums = genre.albums.sort
    list = albums.collect { |album| link = link_to album.title,
                                                   :controller =>
'albums',
                                                   :action => 'show',
                                                   :id => album.id
                                    "<li>#{link}</li>"}
    list.to_s
  end

and this piece of code resides in ArtistHelpers.rb

  def album_list(artist)
    albums = artist.albums.sort
    list = albums.collect { |album| link = link_to album.title,
                                                   :controller =>
'albums',
                                                   :action => 'show',
                                                   :id => album.id
                                    "<li>#{link}</li>"}
    list.to_s
  end

How can I factor out the common behavior? Should this be one method? And
where do I put it then?
Peter M. (Guest)
on 2006-01-21 19:38
(Received via mailing list)
I think this goes in app/helpers/application_helper.rb which should
already exist in your application.

I'm just guessing but could album_list be a model method? Then you can
do things like

@genre.album_list
@artist.album_list

I think you would still want to factor out the code into a file in the
lib/ directory and then mix it into each model. I've asked about
locating the factored out code and didn't get an answer that I liked.
You can see the thread here

http://www.ruby-forum.com/topic/51759#23291

Peter
Brian V. Hughes (Guest)
on 2006-01-21 19:50
(Received via mailing list)
Place it in ApplicationHelper, so all your views can access it. And
write it
this way:

def album_list( artist = nil, genre = nil )
   return "" unless artist or genre # need to have at least one...
   albums = artist ? artist.albums.sort : genre.albums.sort
   list = albums.collect { |album| link = link_to album.title,
                           :controller => 'albums',
                           :action => 'show', :id => album.id
                           "<li>#{link}</li>"}
   list.to_s
end

-Brian
Brian V. Hughes (Guest)
on 2006-01-21 19:53
(Received via mailing list)
Peter M. wrote:
> I think this goes in app/helpers/application_helper.rb which should
> already exist in your application.
>
> I'm just guessing but could album_list be a model method? Then you can
> do things like
>
> @genre.album_list
> @artist.album_list

You could go that way, but I'm not a huge fan of model methods returning
view
text, which is what his album_list helper method does...

> I think you would still want to factor out the code into a file in the
> lib/ directory and then mix it into each model. I've asked about
> locating the factored out code and didn't get an answer that I liked.
> You can see the thread here
>
> http://www.ruby-forum.com/topic/51759#23291

If you were adding the same method to multiple models, going the mixin
route
would make sense, but I'm not sure that's what he's doing here. Both of
his
helpers are working on a group of one model object, albums. He just has
different ways of getting the group.

-Brian
Peter M. (Guest)
on 2006-01-21 19:56
(Received via mailing list)
Oops. I just wasn't paying that much attention. ApplicationHelper
seems like right option. Thanks.

Peter
Tom M. (Guest)
on 2006-01-21 20:42
(Received via mailing list)
May I suggest a combination of the two?

DB access shortcut on the model, and a helper
to use the shortcut and format it?

--
-- Tom M.
Lieven D. (Guest)
on 2006-01-21 23:18
Tom M. wrote:
> May I suggest a combination of the two?
>
> DB access shortcut on the model, and a helper
> to use the shortcut and format it?
>
> --
> -- Tom M.

Sorry, could you elaborate on this? I don't get what you mean.
Thanks for the replies so far, everybody.
Tom M. (Guest)
on 2006-01-22 02:08
(Received via mailing list)
On Jan 21, 2006, at 1:18 PM, Lieven De Keyzer wrote:

> Tom M. wrote:
>> May I suggest a combination of the two?
>>
>> DB access shortcut on the model, and a helper
>> to use the shortcut and format it?

Here's an example from my code, to create a select list
for related objects. It's not a complete example, as it
doesn't eject HTML, but I think you'll get the idea.

In the helper:

module SellersHelper
   def get_units
     @user.get_units.map {|u| [u.name, u.id]}
   end
end

In the view code:

<%= select 'seller','unit_id', get_units %>


So, the @user object knows how to get the correct
objects (via the get_units method), and the helper
formats those objects for use in the view.

--
-- Tom M.
Douglas L. (Guest)
on 2006-01-22 02:53
(Received via mailing list)
2006/1/21, Brian V. Hughes <removed_email_address@domain.invalid>:
>                            "<li>#{link}</li>"}
>    list.to_s
> end

Won't this do the same?

def album_list( source )
  albums = source.albums.sort
  list = albums.collect { |album| link = link_to album.title,
                          :controller => 'albums',
                          :action => 'show', :id => album.id
                          "<li>#{link}</li>"}
  list.to_s
end
Lieven D. (Guest)
on 2006-01-22 04:09
Douglas L. wrote:
> 2006/1/21, Brian V. Hughes <removed_email_address@domain.invalid>:
>>                            "<li>#{link}</li>"}
>>    list.to_s
>> end
>
> Won't this do the same?
>
> def album_list( source )
>   albums = source.albums.sort
>   list = albums.collect { |album| link = link_to album.title,
>                           :controller => 'albums',
>                           :action => 'show', :id => album.id
>                           "<li>#{link}</li>"}
>   list.to_s
> end

Yes, that would be the same. If you pass a genre or an artist
as an argument.
This topic is locked and can not be replied to.