buenas, tengo que crear un sistema de roles y permisos, pero los
permisos tienen que generarse automaticamente de las acciones de los
controladores disponibles, el codigo que tengo es el siguiente:
class Permiso < ActiveRecord::Base
has_and_belongs_to_many :rol, :join_table => :rol_permisos
def self.synchronize_with_controllers
require ‘find’
# Load all the controller files
# ToDo: hunt sub-directories
Find.find( RAILS_ROOT + ‘/app/controllers’ ) do |file_name|
if File.basename(file_name)[0] == ?.
Find.prune
else
if /_controller.rb$/ =~ file_name
load file_name
#Object.const_get(file_name)
end
end
end
# Find the actions in each of the controllers,
# resulting in an array of strings named like "foo/bar"
# representing the controller/action
all_actions = ObjectSpace.subclasses_of( ApplicationController )
all_actions.collect! do |controller|
methods = controller.public_instance_methods -
controller.hidden_actions
methods.collect do |method_name|
controller.name.underscore.gsub( /_controller$/, ‘’ ) + ‘/’ +
method_name
end
end.flatten!
# Find all the 'action_path' columns currently in my table
all_records = self.find(:all)
known_actions = all_records.collect{ |permiso| permiso.name }
# If controllers/actions exist that aren't in the db
# then add new entries for them
missing_from_db = all_actions - known_actions
missing_from_db.each do |action_path|
self.new( :name => action_path ).save
end
# Clear out any entries in the table that do not
# correspond to an existing controller/action
bogus_db_actions = known_actions - all_actions
unless bogus_db_actions.empty?
#Create a mapping of path->Act instance for quick deletion lookup
records_by_action_path = { }
all_records.each do |permiso|
records_by_action_path[ permiso.name ] = permiso
end
bogus_db_actions.each do |action_path|
records_by_action_path[ action_path ].destroy
end
end
end
end
me funciona la primera vez que lo corro, pero la seguna me da un error
de
superclass mismatch for class PermisosController
estoy trabajando con rails 2.1.0