recorder 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/recorder/changeset.rb +22 -22
- data/lib/recorder/observer.rb +17 -15
- data/lib/recorder/tape.rb +66 -65
- data/lib/recorder/version.rb +1 -1
- data/lib/recorder.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0c80f7cee2e38cf2926994bf24de8efd4db6cb5
|
4
|
+
data.tar.gz: 57bf290b34bb02949f2ddfcd4bc560a9b4aa0d20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d25f3f23d9b3527a3e3a1fee809beb21bb5988079b9798221cd1607e04283445cdb13bd89cdb519e0e21687ca75eee2c6f6790bffbc5e8d180899e01b6fd24c6
|
7
|
+
data.tar.gz: e733f79ed49cb43195eab6b06e53789a7f577bddd3bc1e7550533b058f4233b861743c64481e11e4d801e5446ef9592fe608599f1bf364f0752e52302ff88b4f
|
data/lib/recorder/changeset.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Recorder
|
2
|
+
class Changeset
|
3
|
+
attr_reader :item, :changes
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(item, changes)
|
6
|
+
@item = item; @changes = changes
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def attributes
|
10
|
+
self.changes.keys
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def human_attribute_name(attribute)
|
14
|
+
self.item.class.human_attribute_name(attribute.to_s)
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def before(attribute)
|
18
|
+
self.changes[attribute.to_s][0]
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# def
|
21
|
+
def after(attribute)
|
22
|
+
self.changes[attribute.to_s][1]
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
# def item_human_attribute_name(attribute)
|
26
|
+
# self.item.source.class.human_attribute_name(attribute)
|
27
|
+
# end
|
28
|
+
end
|
29
29
|
end
|
data/lib/recorder/observer.rb
CHANGED
@@ -1,25 +1,27 @@
|
|
1
1
|
require 'recorder/tape'
|
2
2
|
require 'active_support/concern'
|
3
3
|
|
4
|
-
module Recorder
|
5
|
-
|
4
|
+
module Recorder
|
5
|
+
module Observer
|
6
|
+
extend ::ActiveSupport::Concern
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
included do
|
9
|
+
has_many :revisions, :class_name => '::Recorder::Revision', :inverse_of => :item, :as => :item
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
module ClassMethods
|
13
|
+
def record_changes(options = {})
|
14
|
+
after_commit :on => :create do
|
15
|
+
Recorder::Tape.new(self, options).record_create
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
after_commit :on => :update do
|
19
|
+
Recorder::Tape.new(self, options).record_update
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
after_commit :on => :destroy do
|
23
|
+
Recorder::Tape.new(self, options).record_destroy
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
data/lib/recorder/tape.rb
CHANGED
@@ -1,84 +1,85 @@
|
|
1
|
-
|
1
|
+
module Recorder
|
2
|
+
class Tape
|
3
|
+
attr_reader :item, :options
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@options = options
|
8
|
-
end
|
5
|
+
def initialize(item, options = {})
|
6
|
+
@item = item
|
7
|
+
@options = options
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def record_create
|
11
|
+
data = {
|
12
|
+
:changes => self.sanitize_attributes(self.item.attributes)
|
13
|
+
}
|
14
14
|
|
15
|
-
|
15
|
+
# changes.merge!(self.parse_associations_attributes)
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
if data.any?
|
18
|
+
self.record(
|
19
|
+
Recorder.store.merge({
|
20
|
+
:event => :create,
|
21
|
+
:data => data
|
22
|
+
})
|
23
|
+
)
|
24
|
+
end
|
24
25
|
end
|
25
|
-
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
27
|
+
def record_update
|
28
|
+
data = {
|
29
|
+
:changes => self.sanitize_attributes(self.item.previous_changes)
|
30
|
+
}
|
31
|
+
|
32
|
+
associations = self.parse_associations_attributes
|
33
|
+
data.merge!(:associations => self.parse_associations_attributes) if associations.any?
|
34
|
+
|
35
|
+
if data.any?
|
36
|
+
self.record(
|
37
|
+
Recorder.store.merge({
|
38
|
+
:event => :update,
|
39
|
+
:data => data
|
40
|
+
})
|
41
|
+
)
|
42
|
+
end
|
42
43
|
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def record_destroy
|
46
|
-
end
|
47
44
|
|
48
|
-
|
49
|
-
|
50
|
-
def record(params)
|
51
|
-
params.merge!({
|
52
|
-
:action_date => Date.today
|
53
|
-
})
|
45
|
+
def record_destroy
|
46
|
+
end
|
54
47
|
|
55
|
-
|
56
|
-
end
|
48
|
+
protected
|
57
49
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
50
|
+
def record(params)
|
51
|
+
params.merge!({
|
52
|
+
:action_date => Date.today
|
53
|
+
})
|
62
54
|
|
63
|
-
|
64
|
-
|
65
|
-
self.options[:associations].inject({}) do |hash, association|
|
66
|
-
reflection = self.item.class.reflect_on_association(association)
|
67
|
-
if reflection.present?
|
68
|
-
if reflection.collection?
|
55
|
+
self.item.revisions.create(params)
|
56
|
+
end
|
69
57
|
|
70
|
-
|
71
|
-
|
58
|
+
def sanitize_attributes(attributes = {})
|
59
|
+
# attributes.symbolize_keys.delete_if{ |key, value| [:created_at, :updated_at, :delta].include?(key) || (value[0] == value[1]) }
|
60
|
+
attributes.symbolize_keys.except(:created_at, :updated_at, :delta)
|
61
|
+
end
|
72
62
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
63
|
+
def parse_associations_attributes
|
64
|
+
if self.options[:associations].any?
|
65
|
+
self.options[:associations].inject({}) do |hash, association|
|
66
|
+
reflection = self.item.class.reflect_on_association(association)
|
67
|
+
if reflection.present?
|
68
|
+
if reflection.collection?
|
69
|
+
|
70
|
+
else
|
71
|
+
object = self.item.send(association)
|
72
|
+
|
73
|
+
if object.present? && self.sanitize_attributes(object.previous_changes).any?
|
74
|
+
hash[reflection.name] = {
|
75
|
+
:changes => self.sanitize_attributes(object.previous_changes)
|
76
|
+
}
|
77
|
+
end
|
77
78
|
end
|
78
79
|
end
|
79
|
-
end
|
80
80
|
|
81
|
-
|
81
|
+
hash
|
82
|
+
end
|
82
83
|
end
|
83
84
|
end
|
84
85
|
end
|
data/lib/recorder/version.rb
CHANGED
data/lib/recorder.rb
CHANGED