Module: ChrnoAudit::ActiveRecordConcern::ClassMethods
- Defined in:
- lib/chrno_audit/active_record_concern.rb
Instance Method Summary collapse
-
#audit(*fields) ⇒ Object
Макрос добавляет в модель аудит изменений.
Instance Method Details
#audit(*fields) ⇒ Object
Макрос добавляет в модель аудит изменений.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chrno_audit/active_record_concern.rb', line 32 def audit( *fields ) # Если таблицы ещё нет, ничего не делаем (полезно для миграций) unless table_exists? self.logger.warn "Audit: try to audit model [#{name}] with non-existent table" if self.logger return end # Добавляем связь has_many :audit_records, :as => :auditable, :class_name => "ChrnoAudit::AuditRecord" # Добавляем обсервер ChrnoAudit::AuditObserver.attach( self ) # Добавляем необходимые параметры cattr_accessor :auditable_fields, :auditable_actions, :auditable_options = fields. # Настройки по умолчанию .reverse_merge! \ :except => [], :when => [ :create, :update, :destroy ], :ignore_empty_diff => true # Нормализуем параметры [ :except ] = Array.wrap( [ :except ] ).map( &:to_s ) [ :when ] = Array.wrap( [ :when ] ).map( &:to_sym ) # Получаем список полей. self.auditable_fields = # Аудит на всех полях? if fields.count == 1 and fields.first == :all # Всегда выкидываем timestamp и id. column_names - %W{ id created_at updated_at } - [ :except ] else ( fields - .delete( :except )).map( &:to_s ) end self.auditable_actions = .delete( :when ) self. = # Проверки if self.logger self.logger.warn "Audit: no fields to audit" if self.auditable_fields.empty? self.logger.warn "Audit: no actions to audit" if self.auditable_actions.empty? end end |