Sha256: b0ff3795239df95ecff5eaa7d259855c6bec6c7ca736077a84ada84d748fc379

Contents?: true

Size: 799 Bytes

Versions: 3

Compression:

Stored size: 799 Bytes

Contents

module Logga
  module ActiveRecord
    extend ActiveSupport::Concern

    class_methods do
      def add_log_entries_for(*actions)
        around_update :log_model_changes if actions.include?(:update)
      end
    end

    def log_model_changes
      field_changes = changes
      log_field_changes(field_changes) if yield
    end

    def log_field_changes(changes)
      changes.each { |field, values| log_field_change(field, *values) }
    end

    def log_field_change(field, old_value, new_value)
      author_data = Hash(try(:author))
      self.log_entries.create(
          body:      "changed #{field} from #{old_value} to #{new_value}",
          author_id:   author_data[:id],
          author_tupe: author_data[:type],
          author_name: author_data[:name]
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logga-0.1.3 lib/logga/active_record.rb
logga-0.1.2 lib/logga/active_record.rb
logga-0.1.1 lib/logga/active_record.rb