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('*')

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!