Sha256: 8004fd3d383dd24ff07052bc68902864e93867d8bcad77eff30be5ba22f721c2
Contents?: true
Size: 1.64 KB
Versions: 2
Compression:
Stored size: 1.64 KB
Contents
module Thoth module Rails::Model def self.included(base) base.send :extend, ClassMethods end module ClassMethods def log_events(options={}) defaults = {on: [:create, :update, :destroy]} options = options.reverse_merge!(defaults) options[:on] = Array(options[:on]) options[:only] = Array(options[:only]) class_attribute :thoth_options self.thoth_options = options after_create :thoth_log_create if options[:on].include?(:create) before_update :thoth_log_update if options[:on].include?(:update) after_destroy :thoth_log_destroy if options[:on].include?(:destroy) end end def thoth_log_create return unless self.class.thoth_options[:on].include?(:create) thoth_log_model(:create) end def thoth_log_update return unless self.class.thoth_options[:on].include?(:update) except_options = self.class.thoth_options[:except] only_options = if except_options.present? self.class.columns.map(&:name) - except_options.map(&:to_s) else self.class.thoth_options[:only] end if only_options.empty? || !(only_options.map(&:to_s) & changed_attribute_names_to_save).empty? thoth_log_model(:update) end end def thoth_log_destroy return unless self.class.thoth_options[:on].include?(:destroy) thoth_log_model(:destroy) end def thoth_log_model(action) Thoth.logger.log("#{self.class.name} #{action}", changes: changes_to_log, attributes: attributes) end def changes_to_log has_changes_to_save? ? changes_to_save : saved_changes end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
thoth-3.0.0 | lib/thoth/rails/model.rb |
thoth-2.0.0 | lib/thoth/rails/model.rb |