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? -- Posted via http://www.ruby-forum.com/.
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 On 1/21/06, Lieven De Keyzer <lieven_dekeyzer@hotmail.com> wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 Lieven De Keyzer wrote:> 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 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_listYou 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#23291If 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
Oops. I just wasn''t paying that much attention. ApplicationHelper seems like right option. Thanks. Peter On 1/21/06, Brian V. Hughes <brianvh@alum.dartmouth.org> wrote:> 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
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 On Jan 21, 2006, at 9:55 AM, Peter Michaux wrote:> Oops. I just wasn''t paying that much attention. ApplicationHelper > seems like right option. Thanks. > > Peter > > > On 1/21/06, Brian V. Hughes <brianvh@alum.dartmouth.org> wrote: >> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 MorniniSorry, could you elaborate on this? I don''t get what you mean. Thanks for the replies so far, everybody. -- Posted via http://www.ruby-forum.com/.
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
2006/1/21, Brian V. Hughes <brianvh@alum.dartmouth.org>:> > 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 > endWon''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
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 > endYes, that would be the same. If you pass a genre or an artist as an argument. -- Posted via http://www.ruby-forum.com/.