Hans
2010-Jul-24 15:00 UTC
Refactoring views ? Should public class methods be used in views?
I am refactoring my views and have learned that to separate view controller and models and that the view is feed from the controller that defines the instance variables related to a the models that is to be used in views- I have also learned that the number of inctace variables should be limited to 1-2 in each action. My question is about how to handle public class methods. In some actions I have many such methods for objects as Person, User etc and I could either define instance variables (but there will then be to many) or specific presenters for these variables or putting them directly into the views to define i.e select_tags with different option lists What solution is to be preferred ? select_tag(:person_id, options_for_select(@people_list.insert(0, [''Select a person'',0]),@selected_person.to_s),... select_tag(:person_id, options_for_select(Person.all_people.insert(0, [''Select a person'',0]),Person.current.to_s),.. select_tag(:person_id, options_for_select(PeoplePresenter.all_people.insert(0,[''Select a person'',0]),PeoplePresenter.current_person.to_s),.. etc -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Leonardo Mateo
2010-Jul-24 16:23 UTC
Re: Refactoring views ? Should public class methods be used in views?
On Sat, Jul 24, 2010 at 12:00 PM, Hans <Hans.Marmolin-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote:> I am refactoring my views and have learned that to separate view > controller and models and that the view is feed from the controller > that defines the instance variables related to a the models that is to > be used in views- > I have also learned that the number of inctace variables should be > limited to 1-2 in each action. > > My question is about how to handle public class methods. > In some actions I have many such methods for objects as Person, User > etc and I could either define instance variables (but there will then > be to many) or specific presenters for these variables or putting them > directly into the views to define i.e select_tags with different > option lists > > What solution is to be preferred ? > select_tag(:person_id, options_for_select(@people_list.insert(0, > [''Select a person'',0]),@selected_person.to_s),... > select_tag(:person_id, options_for_select(Person.all_people.insert(0, > [''Select a person'',0]),Person.current.to_s),.. > select_tag(:person_id, > options_for_select(PeoplePresenter.all_people.insert(0,[''Select a > person'',0]),PeoplePresenter.current_person.to_s),.. > etc >You should not use your models within your views. Use controller''s instance variables in your views. -- Leonardo Mateo. There''s no place like ~ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Hans
2010-Jul-24 21:15 UTC
Re: Refactoring views ? Should public class methods be used in views?
Thanks for the advice As that means a lot of controller instance variables I then had to define a presenter with methods calling public class methods as Person.all_people, Person.current etc On 24 Juli, 18:23, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sat, Jul 24, 2010 at 12:00 PM, Hans <Hans.Marmo...-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote: > > I am refactoring my views and have learned that to separate view > > controller and models and that the view is feed from the controller > > that defines the instance variables related to a the models that is to > > be used in views- > > I have also learned that the number of inctace variables should be > > limited to 1-2 in each action. > > > My question is about how to handle public class methods. > > In some actions I have many such methods for objects as Person, User > > etc and I could either define instance variables (but there will then > > be to many) or specific presenters for these variables or putting them > > directly into the views to define i.e select_tags with different > > option lists > > > What solution is to be preferred ? > > select_tag(:person_id, options_for_select(@people_list.insert(0, > > [''Select a person'',0]),@selected_person.to_s),... > > select_tag(:person_id, options_for_select(Person.all_people.insert(0, > > [''Select a person'',0]),Person.current.to_s),.. > > select_tag(:person_id, > > options_for_select(PeoplePresenter.all_people.insert(0,[''Select a > > person'',0]),PeoplePresenter.current_person.to_s),.. > > etc > > You should not use your models within your views. Use controller''s > instance variables in your views. > > -- > Leonardo Mateo. > There''s no place like ~-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Leonardo Mateo
2010-Jul-25 19:49 UTC
Re: Re: Refactoring views ? Should public class methods be used in views?
On Sat, Jul 24, 2010 at 6:15 PM, Hans <Hans.Marmolin-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote:> Thanks for the advice > As that means a lot of controller instance variables I then had to > define a presenterI''m not sure what you mean with this, but you can use helper methods to clean up your views and make them easier to maintain.> with methods calling public class methods as Person.all_people, > Person.current etcI can''t tell you about this, since that''s just a matter of your application design.> > On 24 Juli, 18:23, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On Sat, Jul 24, 2010 at 12:00 PM, Hans <Hans.Marmo...-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote: >> > I am refactoring my views and have learned that to separate view >> > controller and models and that the view is feed from the controller >> > that defines the instance variables related to a the models that is to >> > be used in views- >> > I have also learned that the number of inctace variables should be >> > limited to 1-2 in each action. >> >> > My question is about how to handle public class methods. >> > In some actions I have many such methods for objects as Person, User >> > etc and I could either define instance variables (but there will then >> > be to many) or specific presenters for these variables or putting them >> > directly into the views to define i.e select_tags with different >> > option lists >> >> > What solution is to be preferred ? >> > select_tag(:person_id, options_for_select(@people_list.insert(0, >> > [''Select a person'',0]),@selected_person.to_s),... >> > select_tag(:person_id, options_for_select(Person.all_people.insert(0, >> > [''Select a person'',0]),Person.current.to_s),.. >> > select_tag(:person_id, >> > options_for_select(PeoplePresenter.all_people.insert(0,[''Select a >> > person'',0]),PeoplePresenter.current_person.to_s),.. >> > etc >> >> You should not use your models within your views. Use controller''s >> instance variables in your views. >> >> -- >> Leonardo Mateo. >> There''s no place like ~ > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > >-- Leonardo Mateo. There''s no place like ~ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.