I am working on a project that uses quite a bit of conditional logic to display information about groups in a list. The partial that renders the group attributes will display different info based on whether the user is a) logged in, b) the creator of the group c) a member of the group, etc. I find myself using a lot of if/else conditions to render things as they should be. The thing I dislike the most is that I''m writing the same logic in different places. For instance, if a user is an admin I''m doing: <% if current_user.admin? %> something... <% end %> ...and I do that two or three times in the same template. What are some other strategies. Can I use respond_to? How do people make their view code more readable? If there are any good resources people know about for this, please respond! Thanks, -A -- 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.
Take a look at: http://github.com/stffn/declarative_authorization Darian Shimy -- http://www.darianshimy.com http://twitter.com/dshimy On Wed, Nov 18, 2009 at 1:29 PM, ressister <ressister-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am working on a project that uses quite a bit of conditional logic > to display information about groups in a list. The partial that > renders the group attributes will display different info based on > whether the user is a) logged in, b) the creator of the group c) a > member of the group, etc. > > I find myself using a lot of if/else conditions to render things as > they should be. The thing I dislike the most is that I''m writing the > same logic in different places. For instance, if a user is an admin > I''m doing: > > <% if current_user.admin? %> > something... > <% end %> > > ...and I do that two or three times in the same template. What are > some other strategies. Can I use respond_to? How do people make > their view code more readable? If there are any good resources people > know about for this, please respond! > > Thanks, > -A > > -- > > 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. > > >-- 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=.
On Nov 18, 4:29 pm, ressister <ressis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am working on a project that uses quite a bit of conditional logic > to display information about groups in a list. The partial that > renders the group attributes will display different info based on > whether the user is a) logged in, b) the creator of the group c) a > member of the group, etc. > > I find myself using a lot of if/else conditions to render things as > they should be. The thing I dislike the most is that I''m writing the > same logic in different places. For instance, if a user is an admin > I''m doing: > > <% if current_user.admin? %> > something... > <% end %> > > ...and I do that two or three times in the same template. What are > some other strategies. Can I use respond_to? How do people make > their view code more readable? If there are any good resources people > know about for this, please respond! > > Thanks, > -AFor simple cases you could just create a helper like this: def admin_only &blk yield if current_user.admin? end <% admin_only do %> some ISH <% end %> -- 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=.
Definitely take a look at Florian Hanke''s representer-plugin at http://github.com/floere/representer. It gives you the ability to have ''object oriented'' views. You put all the logic (conditionals etc.) inside methods of the view-object. Much much easier to test and it results in extra clean views. If you need more info (in case the documentation is not enough), let me know. Andi On Nov 19, 12:28 am, pharrington <xenogene...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 18, 4:29 pm, ressister <ressis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I am working on a project that uses quite a bit of conditional logic > > to display information about groups in a list. The partial that > > renders the group attributes will display different info based on > > whether the user is a) logged in, b) the creator of the group c) a > > member of the group, etc. > > > I find myself using a lot of if/else conditions to render things as > > they should be. The thing I dislike the most is that I''m writing the > > same logic in different places. For instance, if a user is an admin > > I''m doing: > > > <% if current_user.admin? %> > > something... > > <% end %> > > > ...and I do that two or three times in the same template. What are > > some other strategies. Can I use respond_to? How do people make > > their view code more readable? If there are any good resources people > > know about for this, please respond! > > > Thanks, > > -A > > For simple cases you could just create a helper like this: > def admin_only &blk > yield if current_user.admin? > end > > <% admin_only do %> > some ISH > <% end %>-- 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=.
2009/11/19 pharrington <xenogenesis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > For simple cases you could just create a helper like this: > def admin_only &blk > yield if current_user.admin? > end > > <% admin_only do %> > some ISH > <% end %> > >or a helper like: def show_user_info if (current_user.admin?) do ''User is admin.'' elsif (current_user.loggedin?) do ''User is logged in.'' else ''Another case'' end end and on the templates simply: <%= show_user_info %> regards -- ------------------------------------ Oliver Hernàndez Valls http://codit.wikidot.com http://bolets.negocis.cat http://wiki.tramuntanal.cat -- 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=.