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!

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

[email protected] 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!

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

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!

Then do find_all_by_controller_and_action then?

---------- Forwarded message ----------
From: [email protected] [email protected]
Date: Dec 10, 2007 9:15 AM
Subject: [Rails] Re: Help on drying code
To: “Ruby on Rails: Talk” [email protected]

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 E. [email protected] wrote:

 role_items.each do |role_item|

[email protected] wrote:

  role_ids << role_item.role_id

which of course is not DRY at all.
all_controllers = self.find_by_controller(’*’)
Thanks!


Posted viahttp://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs