Where to put method


#1

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

  • #{link}
  • ”}
    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

  • #{link}
  • ”}
    list.to_s
    end

    How can I factor out the common behavior? Should this be one method? And
    where do I put it then?


    #2

    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


    #3

    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

  • #{link}
  • ”}
    list.to_s
    end

    -Brian


    #4

    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


    #5

    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.


    #6

    Oops. I just wasn’t paying that much attention. ApplicationHelper
    seems like right option. Thanks.

    Peter


    #7

    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.


    #8

    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.


    #9

    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

  • #{link}
  • ”}
    list.to_s
    end

    Yes, that would be the same. If you pass a genre or an artist
    as an argument.


    #10

    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

  • #{link}
  • ”}
    list.to_s
    end