felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-09 22:14 UTC
Help on drying code
Hi all,
Currently I''m having to do this:
def self.authorized_roles(controller, action)
specific = self.find_by_controller_and_action(controller, action)
all_actions = self.find_by_controller_and_action(controller,
''*'')
all_controllers = self.find_by_controller(''*'')
role_ids = []
specific.each do |role_item|
role_ids << role_item.role_id
end
all_actions.each do |role_item|
role_ids << role_item.role_id
end
all_controllers.each do |role_item|
role_ids << role_item.role_id
end
role_ids.flatten.uniq
end
which of course is not DRY at all.
I could not find how to merge class instances (specific, all_actions
and all_controllers) or how to make only one find to use the selection
statements above (at least not in a easy to read way)
It would be better if I could do something like:
def self.authorized_roles(controller, action)
specific = self.find_by_controller_and_action(controller, action)
all_actions = self.find_by_controller_and_action(controller,
''*'')
all_controllers = self.find_by_controller(''*'')
#
# MAGIC
#
role_ids = []
role_items do |role_item|
role_ids << role_item.role_id
end
role_ids.flatten.uniq
end
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hi there,
I don''t know if this will fulfill your needs, but maybe you could try
this:
def self.authorized_roles(controller, action)
specific = self.find_by_controller_and_action(controller, action)
all_actions = self.find_by_controller_and_action(controller,
''*'')
all_controllers = self.find_by_controller(''*'')
role_items = specific << all_actions << all_controllers
role_ids = []
role_items.each do |role_item|
role_ids << role_item.role_id
end
role_ids.flatten.uniq
end
Best,
kodama
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi all,
>
> Currently I''m having to do this:
>
> def self.authorized_roles(controller, action)
> specific = self.find_by_controller_and_action(controller, action)
> all_actions = self.find_by_controller_and_action(controller,
''*'')
> all_controllers = self.find_by_controller(''*'')
> role_ids = []
> specific.each do |role_item|
> role_ids << role_item.role_id
> end
> all_actions.each do |role_item|
> role_ids << role_item.role_id
> end
> all_controllers.each do |role_item|
> role_ids << role_item.role_id
> end
> role_ids.flatten.uniq
> end
>
> which of course is not DRY at all.
>
> I could not find how to merge class instances (specific, all_actions
> and all_controllers) or how to make only one find to use the selection
> statements above (at least not in a easy to read way)
>
> It would be better if I could do something like:
>
> def self.authorized_roles(controller, action)
> specific = self.find_by_controller_and_action(controller, action)
> all_actions = self.find_by_controller_and_action(controller,
''*'')
> all_controllers = self.find_by_controller(''*'')
> #
> # MAGIC
> #
> role_ids = []
> role_items do |role_item|
> role_ids << role_item.role_id
> end
> role_ids.flatten.uniq
> end
>
> Thanks!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-09 22:45 UTC
Re: Help on drying code
Thanks for your fast response. Unfortunately, it does not work for instances that contain only one object. If self.find_by_controller_and_action(...) returns only one object, then it does not have the method <<. (s and ri are RoleItems here)>> s << riNoMethodError: undefined method `<<'' for #<RoleItem:0x34b3a04> On Dec 9, 2:33 pm, Old Echo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > I don''t know if this will fulfill your needs, but maybe you could try > this: > > def self.authorized_roles(controller, action) > specific = self.find_by_controller_and_action(controller, action) > all_actions = self.find_by_controller_and_action(controller, ''*'') > all_controllers = self.find_by_controller(''*'') > role_items = specific << all_actions << all_controllers > role_ids = [] > role_items.each do |role_item| > role_ids << role_item.role_id > end > role_ids.flatten.uniq > end > > Best, > kodama > > > > felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Hi all, > > > Currently I''m having to do this: > > > def self.authorized_roles(controller, action) > > specific = self.find_by_controller_and_action(controller, action) > > all_actions = self.find_by_controller_and_action(controller, ''*'') > > all_controllers = self.find_by_controller(''*'') > > role_ids = [] > > specific.each do |role_item| > > role_ids << role_item.role_id > > end > > all_actions.each do |role_item| > > role_ids << role_item.role_id > > end > > all_controllers.each do |role_item| > > role_ids << role_item.role_id > > end > > role_ids.flatten.uniq > > end > > > which of course is not DRY at all. > > > I could not find how to merge class instances (specific, all_actions > > and all_controllers) or how to make only one find to use the selection > > statements above (at least not in a easy to read way) > > > It would be better if I could do something like: > > > def self.authorized_roles(controller, action) > > specific = self.find_by_controller_and_action(controller, action) > > all_actions = self.find_by_controller_and_action(controller, ''*'') > > all_controllers = self.find_by_controller(''*'') > > # > > # MAGIC > > # > > role_ids = [] > > role_items do |role_item| > > role_ids << role_item.role_id > > end > > role_ids.flatten.uniq > > end > > > Thanks! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Then do find_all_by_controller_and_action then? ---------- Forwarded message ---------- From: felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Date: Dec 10, 2007 9:15 AM Subject: [Rails] Re: Help on drying code To: "Ruby on Rails: Talk" <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Thanks for your fast response. Unfortunately, it does not work for instances that contain only one object. If self.find_by_controller_and_action(...) returns only one object, then it does not have the method <<. (s and ri are RoleItems here)>> s << riNoMethodError: undefined method `<<'' for #<RoleItem:0x34b3a04> On Dec 9, 2:33 pm, Old Echo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > I don''t know if this will fulfill your needs, but maybe you could try > this: > > def self.authorized_roles(controller, action) > specific = self.find_by_controller_and_action(controller, action) > all_actions = self.find_by_controller_and_action(controller, ''*'') > all_controllers = self.find_by_controller(''*'') > role_items = specific << all_actions << all_controllers > role_ids = [] > role_items.each do |role_item| > role_ids << role_item.role_id > end > role_ids.flatten.uniq > end > > Best, > kodama > > > > felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Hi all, > > > Currently I''m having to do this: > > > def self.authorized_roles(controller, action) > > specific = self.find_by_controller_and_action(controller, action) > > all_actions = self.find_by_controller_and_action(controller, ''*'') > > all_controllers = self.find_by_controller(''*'') > > role_ids = [] > > specific.each do |role_item| > > role_ids << role_item.role_id > > end > > all_actions.each do |role_item| > > role_ids << role_item.role_id > > end > > all_controllers.each do |role_item| > > role_ids << role_item.role_id > > end > > role_ids.flatten.uniq > > end > > > which of course is not DRY at all. > > > I could not find how to merge class instances (specific, all_actions > > and all_controllers) or how to make only one find to use the selection > > statements above (at least not in a easy to read way) > > > It would be better if I could do something like: > > > def self.authorized_roles(controller, action) > > specific = self.find_by_controller_and_action(controller, action) > > all_actions = self.find_by_controller_and_action(controller, ''*'') > > all_controllers = self.find_by_controller(''*'') > > # > > # MAGIC > > # > > role_ids = [] > > role_items do |role_item| > > role_ids << role_item.role_id > > end > > role_ids.flatten.uniq > > end > > > Thanks! > > -- > Posted viahttp://www.ruby-forum.com/.-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-09 23:14 UTC
Re: Help on drying code
I should have had that find_all there since the beginning...
Anyway, the new code, a little more DRY is
def self.authorized_roles(controller, action)
role_items = self.find_all_by_controller_and_action(controller,
action)
role_items << self.find_all_by_controller_and_action(controller,
''*'')
role_items << self.find_all_by_controller(''*'')
role_ids = []
begin
role_items.flatten.each do |role_item|
role_ids << role_item.role_id
end
rescue
nil
end
role_ids.uniq
end
I had to add that flatten on role_items.each, because it would not
work otherwise.
Thanks a lot!
On Dec 9, 2:46 pm, "Ryan Bigg"
<radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Then do find_all_by_controller_and_action then?
>
> ---------- Forwarded message ----------
> From: felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
<felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Date: Dec 10, 2007 9:15 AM
> Subject: [Rails] Re: Help on drying code
> To: "Ruby on Rails: Talk"
<rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
>
> Thanks for your fast response.
>
> Unfortunately, it does not work for instances that contain only one
> object.
>
> If self.find_by_controller_and_action(...) returns only one object,
> then it does not have the method <<.
>
> (s and ri are RoleItems here)
> >> s << ri
> NoMethodError: undefined method `<<'' for
#<RoleItem:0x34b3a04>
>
> On Dec 9, 2:33 pm, Old Echo
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
> > Hi there,
> > I don''t know if this will fulfill your needs, but maybe you
could try
> > this:
>
> > def self.authorized_roles(controller, action)
> > specific = self.find_by_controller_and_action(controller, action)
> > all_actions = self.find_by_controller_and_action(controller,
''*'')
> > all_controllers = self.find_by_controller(''*'')
> > role_items = specific << all_actions <<
all_controllers
> > role_ids = []
> > role_items.each do |role_item|
> > role_ids << role_item.role_id
> > end
> > role_ids.flatten.uniq
> > end
>
> > Best,
> > kodama
>
> > felip...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
> > > Hi all,
>
> > > Currently I''m having to do this:
>
> > > def self.authorized_roles(controller, action)
> > > specific = self.find_by_controller_and_action(controller,
action)
> > > all_actions = self.find_by_controller_and_action(controller,
''*'')
> > > all_controllers =
self.find_by_controller(''*'')
> > > role_ids = []
> > > specific.each do |role_item|
> > > role_ids << role_item.role_id
> > > end
> > > all_actions.each do |role_item|
> > > role_ids << role_item.role_id
> > > end
> > > all_controllers.each do |role_item|
> > > role_ids << role_item.role_id
> > > end
> > > role_ids.flatten.uniq
> > > end
>
> > > which of course is not DRY at all.
>
> > > I could not find how to merge class instances (specific,
all_actions
> > > and all_controllers) or how to make only one find to use the
selection
> > > statements above (at least not in a easy to read way)
>
> > > It would be better if I could do something like:
>
> > > def self.authorized_roles(controller, action)
> > > specific = self.find_by_controller_and_action(controller,
action)
> > > all_actions = self.find_by_controller_and_action(controller,
''*'')
> > > all_controllers =
self.find_by_controller(''*'')
> > > #
> > > # MAGIC
> > > #
> > > role_ids = []
> > > role_items do |role_item|
> > > role_ids << role_item.role_id
> > > end
> > > role_ids.flatten.uniq
> > > end
>
> > > Thanks!
>
> > --
> > Posted viahttp://www.ruby-forum.com/.
>
> --
> Ryan Bigghttp://www.frozenplague.net
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---