Module: Tamaudit::AuditorBehavior::ClassMethods
- Defined in:
- lib/tamaudit/auditor_behavior.rb
Constant Summary collapse
- @@default_excluded =
%w(lock_version created_at updated_at created_on updated_on)
Instance Method Summary collapse
-
#as_user(user, &block) ⇒ Object
All audits made during the block called will be recorded as made by
user. - #auditable(options = {}) ⇒ Object
- #permited_columns ⇒ Object
Instance Method Details
#as_user(user, &block) ⇒ Object
All audits made during the block called will be recorded as made by user. This method is hopefully threadsafe, making it ideal for background operations that require audit information.
50 51 52 53 54 55 |
# File 'lib/tamaudit/auditor_behavior.rb', line 50 def as_user(user, &block) RequestStore.store[:audited_user] = user yield ensure RequestStore.store[:audited_user] = nil end |
#auditable(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tamaudit/auditor_behavior.rb', line 15 def auditable( = {}) self.audit_callbacks = [] self.audit_callbacks << [:on] unless [:on].blank? self.audit_callbacks.flatten! after_create :audit_create if self.audit_callbacks.blank? || self.audit_callbacks.include?(:create) before_update :audit_update if self.audit_callbacks.blank? || self.audit_callbacks.include?(:update) before_destroy :audit_destroy if self.audit_callbacks.blank? || self.audit_callbacks.include?(:destroy) self.excluded_cols = (@@default_excluded) if [:only] [:only] = [[:only]].flatten.map { |x| x.to_s } self.excluded_cols = (self.column_names - [:only] ) end if [:except] [:except] = [[:except]].flatten.map { |x| x.to_s } self.excluded_cols = (@@default_excluded) + [:except] end has_many :audits, :as => :auditable, :class_name => Tamaudit::Audit.name #attr_accessor :audited_user, :audited_ip accepts_nested_attributes_for :audits end |
#permited_columns ⇒ Object
43 44 45 |
# File 'lib/tamaudit/auditor_behavior.rb', line 43 def permited_columns self.column_names - self.excluded_cols.to_a end |