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.
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2006-01-21 17: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?
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-21 18: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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-01-21 18: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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-01-21 18:53
(Received via mailing list)
Peter Michaux 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
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-21 18:56
(Received via mailing list)
Oops. I just wasn't paying that much attention. ApplicationHelper
seems like right option. Thanks.

Peter
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-01-21 19: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 Mornini
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2006-01-21 22:18
Tom Mornini 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 Mornini

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

> Tom Mornini 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 Mornini
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-01-22 01:53
(Received via mailing list)
2006/1/21, Brian V. Hughes <brianvh@alum.dartmouth.org>:
>                            "<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
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2006-01-22 03:09
Douglas Livingstone wrote:
> 2006/1/21, Brian V. Hughes <brianvh@alum.dartmouth.org>:
>>                            "<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.