recorder 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0c80f7cee2e38cf2926994bf24de8efd4db6cb5
4
- data.tar.gz: 57bf290b34bb02949f2ddfcd4bc560a9b4aa0d20
3
+ metadata.gz: 99e7c1bf50c6c1844ee0961756ffe1ab06128991
4
+ data.tar.gz: 51d81209e457c6d099f3ffb168ac261bcf559aed
5
5
  SHA512:
6
- metadata.gz: d25f3f23d9b3527a3e3a1fee809beb21bb5988079b9798221cd1607e04283445cdb13bd89cdb519e0e21687ca75eee2c6f6790bffbc5e8d180899e01b6fd24c6
7
- data.tar.gz: e733f79ed49cb43195eab6b06e53789a7f577bddd3bc1e7550533b058f4233b861743c64481e11e4d801e5446ef9592fe608599f1bf364f0752e52302ff88b4f
6
+ metadata.gz: f0d3fc8fb50bbb4bb2d87ad02cb2086669e6cced785e65a96a4f911a1ec6e33d0a4dcf81feecdc8bf6299e9fefe8601ae5fd54845f9ce0c8a100f256b26d278b
7
+ data.tar.gz: 4812c0ac7f9c24fb614f7eb9f038c42ab57b5a732e319674d89ad87fe374fb4dd513077a881330e3a496859a7f3d1677ee1108c0818c3c3829d064a051d734d1
@@ -6,24 +6,52 @@ module Recorder
6
6
  @item = item; @changes = changes
7
7
  end
8
8
 
9
- def attributes
9
+ def keys
10
10
  self.changes.keys
11
11
  end
12
12
 
13
13
  def human_attribute_name(attribute)
14
- self.item.class.human_attribute_name(attribute.to_s)
14
+ if defined?(Draper) && self.item.decorated?
15
+ self.item.source.class.human_attribute_name(attribute.to_s)
16
+ else
17
+ self.item.class.human_attribute_name(attribute.to_s)
18
+ end
19
+
20
+ end
21
+
22
+ def previous(attribute)
23
+ # self.changes[attribute.to_s][0]
24
+ # self.previous_version.try("display_#{attribute}") || self.previous_version.try(attribute)
25
+ self.try("previous_#{attribute}") || self.previous_version.try(attribute)
15
26
  end
16
27
 
17
- def before(attribute)
18
- self.changes[attribute.to_s][0]
28
+ def previous_version
29
+ return @previous_version if defined?(@previous_version)
30
+
31
+ @previous_version = self.item.dup
32
+
33
+ self.changes.each do |key, change|
34
+ @previous_version.send("#{key}=", change[0])
35
+ end
36
+
37
+ @previous_version
19
38
  end
20
39
 
21
- def after(attribute)
22
- self.changes[attribute.to_s][1]
40
+ def next(attribute)
41
+ self.try("next_#{attribute}") || self.next_version.send(attribute)
42
+ # self.next_version.try("display_#{attribute}") || self.next_version.try(attribute)
23
43
  end
24
44
 
25
- # def item_human_attribute_name(attribute)
26
- # self.item.source.class.human_attribute_name(attribute)
27
- # end
45
+ def next_version
46
+ return @next_version if defined?(@next_version)
47
+
48
+ @next_version = self.item.dup
49
+
50
+ self.changes.each do |key, change|
51
+ @next_version.send("#{key}=", change[1])
52
+ end
53
+
54
+ @next_version
55
+ end
28
56
  end
29
57
  end
@@ -6,18 +6,31 @@ module Recorder
6
6
  end
7
7
 
8
8
  def item_changeset
9
- @item_changeset ||= Recorder::Changeset.new(self.item.source, source.data['changes'])
9
+ return @item_changeset if defined?(@item_changeset)
10
+
11
+ @item_changeset ||= self.changeset_klass(self.item.source).new(self.item, source.data['changes'])
10
12
  end
11
13
 
12
14
  def changed_associations
13
- source.data['associations'].keys#.map{ |association| self.item_human_attribute_name(association) }
15
+ source.data['associations'].keys
14
16
  end
15
17
 
16
18
  def association_changeset(name)
17
19
  association = self.item.send(name)
18
- association = association.source if association.decorated?
20
+ # association = association.source if association.decorated?
21
+
22
+ self.changeset_klass(association).new(association, source.data['associations'].fetch(name.to_s).try(:fetch, 'changes'))
23
+ end
24
+
25
+ protected
26
+
27
+ def changeset_klass(object)
28
+ klass = object.decorated? ? object.source.class : object.class
29
+ klass = klass.base_class
30
+ klass = "#{klass}Changeset"
19
31
 
20
- Recorder::Changeset.new(association, source.data['associations'].fetch(name.to_s).try(:fetch, 'changes'))
32
+ klass = klass.constantize rescue nil
33
+ klass.present? ? klass : Recorder::Changeset
21
34
  end
22
35
  end
23
36
  end
@@ -10,17 +10,21 @@ module Recorder
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- def record_changes(options = {})
13
+ def recorder(options = {})
14
+ define_method 'recorder_option' do
15
+ options
16
+ end
17
+
14
18
  after_commit :on => :create do
15
- Recorder::Tape.new(self, options).record_create
19
+ Recorder::Tape.new(self).record_create
16
20
  end
17
21
 
18
22
  after_commit :on => :update do
19
- Recorder::Tape.new(self, options).record_update
23
+ Recorder::Tape.new(self).record_update
20
24
  end
21
25
 
22
26
  after_commit :on => :destroy do
23
- Recorder::Tape.new(self, options).record_destroy
27
+ Recorder::Tape.new(self).record_destroy
24
28
  end
25
29
  end
26
30
  end
data/lib/recorder/tape.rb CHANGED
@@ -1,10 +1,9 @@
1
1
  module Recorder
2
2
  class Tape
3
- attr_reader :item, :options
3
+ attr_reader :item
4
4
 
5
- def initialize(item, options = {})
6
- @item = item
7
- @options = options
5
+ def initialize(item)
6
+ @item = item;
8
7
  end
9
8
 
10
9
  def record_create
@@ -56,13 +55,12 @@ module Recorder
56
55
  end
57
56
 
58
57
  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)
58
+ attributes.symbolize_keys.except(self.item.recorder_options[:except])
61
59
  end
62
60
 
63
61
  def parse_associations_attributes
64
- if self.options[:associations].any?
65
- self.options[:associations].inject({}) do |hash, association|
62
+ if self.item.recorder_options[:associations].any?
63
+ self.item.recorder_options[:associations].inject({}) do |hash, association|
66
64
  reflection = self.item.class.reflect_on_association(association)
67
65
  if reflection.present?
68
66
  if reflection.collection?
@@ -3,7 +3,7 @@ module Recorder
3
3
  module VERSION
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 2
6
+ TINY = 3
7
7
  PRE = nil
8
8
 
9
9
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".").freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-06 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord