Module: ActiveResourceChangeable

Defined in:
lib/active_resource_changeable/version.rb,
lib/active_resource_changeable/active_resource_changeable.rb

Constant Summary collapse

VERSION =
"0.1.0"
@@hash =
HashWithIndifferentAccess.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/active_resource_changeable/active_resource_changeable.rb', line 3

def self.included(base)
  class << base
    def find(params)
      TraceChanges.instance_variable_set(:@original_objekt, self.new(super(params).attributes))
      super(params)
    end
  end
end

Instance Method Details

#changesObject



38
39
40
41
# File 'lib/active_resource_changeable/active_resource_changeable.rb', line 38

def changes
  prechanges
  @@hash.deep_dup()
end

#prechanges(_self = self, original_objekt = TraceChanges.instance_variable_get(:@original_objekt), keys = []) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_resource_changeable/active_resource_changeable.rb', line 14

def prechanges(_self=self, original_objekt=TraceChanges.instance_variable_get(:@original_objekt), keys = [])
  hash = _self.attributes
  hash.each do |key, val|
    if val.is_a? ActiveResource::Base
      prechanges(val, original_objekt.send(key), keys.push(key))
      keys.pop
    elsif original_objekt.send(key) != val
      temp_hash = HashWithIndifferentAccess.new
      if keys.present?
        keys.each_with_index do |kkey, i|
          if temp_hash[keys[i]].present?
            temp_hash[keys[i]].merge!({"#{keys[i+1]}": {"#{key}": [original_objekt.send(key), val]}})
          else
            temp_hash[keys[i]] = {"#{keys[i+1]}": {"#{key}": [original_objekt.send(key), val]}}
          end
        end
        @@hash = @@hash.deep_merge(temp_hash)
      else
        @@hash.merge!("#{key}": [original_objekt.send(key), val])
      end
    end
  end
end