module SimpleAudit # Сделать запись в аудит-лог из контроллера # записи автоматически присваевается user_id и прочие параметры # для дополнительной информации есть поле 'extra' def audit(extra='') return false unless current_user Audit.create(:user_id => current_user.id, :resource => params[:controller], :action => params[:action], :extra => extra) # logger.warn "Eeeei, it works!" end module ControllerMethods module Application def self.included(controller) controller.extend ClassMethods controller.class_inheritable_accessor :auditables controller.auditables = [] controller.before_filter :audit_actions end private # подсмотрено у simple_authorization, кажется def audit_actions return if !current_user self.class.auditables.each do |actions| options = actions.last.is_a?(Hash) ? actions.last : {} next if options[:if] && !options[:if].call(self) next if options[:unless] && options[:unless].call(self) next if options[:only] && !auditable_actions(options[:only]).include?(action_name) next if options[:except] && auditable_actions(options[:except]).include?(action_name) audit() end end def auditable_actions(actions) actions = [actions] unless actions.is_a?(Array) actions.map(&:to_s) end module ClassMethods # before_filter для контроллеров def audit_this(*actions) auditables << actions end end end end end %w{ models controllers helpers views views/audits }.each do |dir| path = File.join(File.dirname(__FILE__), 'app', dir) $LOAD_PATH << path ActiveSupport::Dependencies.load_paths << path ActiveSupport::Dependencies.load_once_paths.delete(path) end # optionally: # Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file| # require file # end