Class: Mongoid::AuditLog::Changes

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/audit_log/changes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Changes

Returns a new instance of Changes.


46
47
48
# File 'lib/mongoid/audit_log/changes.rb', line 46

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


4
5
6
# File 'lib/mongoid/audit_log/changes.rb', line 4

def model
  @model
end

Class Method Details

.ch_ch_ch_ch_ch_changesObject


7
8
9
# File 'lib/mongoid/audit_log/changes.rb', line 7

def self.ch_ch_ch_ch_ch_changes
  puts "turn and face the strange changes"
end

.clean_fields(*disallowed_fields) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/audit_log/changes.rb', line 25

def self.clean_fields(*disallowed_fields)
  options = disallowed_fields.extract_options!

  unless options.has_key?(:from)
    raise ArgumentError, ':from is a required argument'
  end

  changes = options[:from]

  if changes.is_a?(Hash)
    changes.except(*disallowed_fields).inject({}) do |memo, t|
      key, value = *t
      memo.merge!(key => clean_fields(*disallowed_fields, :from => value))
    end
  elsif changes.is_a?(Enumerable)
    changes.map { |c| clean_fields(*disallowed_fields, :from => c) }
  else
    changes
  end
end

.extract_from(value) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongoid/audit_log/changes.rb', line 11

def self.extract_from(value)
  if value.is_a?(Hash)
    raise ArgumentError, 'does not support hashes'
  elsif value.is_a?(Enumerable)
    changes = value.map do |model|
      Mongoid::AuditLog::Changes.new(model).all
    end

    changes.reject(&:blank?)
  else
    Mongoid::AuditLog::Changes.new(value).all
  end
end

Instance Method Details

#allObject Also known as: read


50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/audit_log/changes.rb', line 50

def all
  @all ||= if model.blank? || !model.changed?
             {}
           else
             result = model.changes
             result.merge!(embedded_changes) unless embedded_changes.empty?
             Mongoid::AuditLog::Changes.clean_fields('_id', 'updated_at', :from => result)
           end
end