snapshotable 0.0.3 → 0.1.0

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: 295739bcb71078e20aa642a39b554663b3eba3ed
4
- data.tar.gz: 1aa27734ea1eceb4f18ef96e251cde73cc2b87a4
3
+ metadata.gz: 7040b8e2dbf8340101b338b1ba076e33a73137d4
4
+ data.tar.gz: 8545fcb80172d4062b0f8d7926da4e4088d0ff1b
5
5
  SHA512:
6
- metadata.gz: e26506fecd06d327690dcda5a88a9a1ef557b3153e47bf4ca93ffd2b1fc95bb61dd3e657f2ef2c36325dcd9adda7c793975832ec8067a9a7ef1a49a45c1cfd4a
7
- data.tar.gz: 5b6084efc5ff846800c97689f2cc8d3f28e05cf21cf86ea5a9bf53ce42572198763928bf7137cddef87c22b8cfd9e74b9bcfed4363397a9765833bdeb00ac612
6
+ metadata.gz: d454e050918822be5e32ea4656ed06252b0444b8765cde2f8f2e317dde9c6d6cc37e16fcb8343ef88a7f228368359bd7d7b1e1bfecf8f29dca87b6ee2dfdf7f6
7
+ data.tar.gz: 9b313144a07ff5690ee5ebefb4a33bf7e4d6a4ba9aa794df493c8d7b69704291960f0a52ebbcefb45fe27144ded265a0d578d7cfac80641415fb004b4ff4dab8
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
-
1
+ [![Build Status](https://travis-ci.com/QultureRocks/snapshotable.svg?branch=master)](https://travis-ci.com/QultureRocks/snapshotable)
2
2
 
3
3
  # Snapshotable
4
4
 
@@ -4,12 +4,12 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
4
4
  # model to be snapshoted
5
5
  t.references :<%= model_underscored %>, index: true, null: false, foreign_key: true
6
6
 
7
- # snapshoted_attributes
8
- t.jsonb :attributes, null: false
7
+ # snapshoted attributes
8
+ t.jsonb :object, null: false
9
9
  <% relations_has_one.each do |relation| %>
10
- t.jsonb :<%= relation.underscore %>_attributes, null: false<% end %>
10
+ t.jsonb :<%= relation.underscore %>_object<% end %>
11
11
  <% relations_has_many.each do |relation| %>
12
- t.jsonb :<%= relation.underscore %>_attributes, null: false, array: true, default: []<% end %>
12
+ t.jsonb :<%= relation.underscore %>_object, array: true, default: []<% end %>
13
13
 
14
14
  t.timestamps null: false
15
15
  end
@@ -2,5 +2,5 @@ class <%= model_camelcased %>Snapshot < <%= active_record_class %>
2
2
  belongs_to :<%= model_underscored %>
3
3
 
4
4
  validates :<%= model_underscored %>, presence: true
5
- validates :attributes, presence: true
5
+ validates :object, presence: true
6
6
  end
@@ -17,16 +17,23 @@ module Snapshotable
17
17
  def snapshot_attrs
18
18
  snapshot = {}
19
19
 
20
- add_custom_attributes(snapshot) if custom_snapshot_attributes&.any?
21
-
22
20
  snapshot[:object] = extract_attributes(record_snapshot_attrs, record) if record_snapshot_attrs.any?
23
21
 
22
+ add_foreign_key(snapshot)
23
+ add_custom_attributes(snapshot)
24
24
  add_deep_snapshot_objects(snapshot)
25
25
 
26
26
  snapshot
27
27
  end
28
28
 
29
+ def add_foreign_key(snapshot)
30
+ return false if record.snapshot_foreign_key.nil?
31
+ snapshot[record.snapshot_foreign_key.to_s] = record.id
32
+ end
33
+
29
34
  def add_custom_attributes(snapshot)
35
+ return false unless custom_snapshot_attributes&.any?
36
+
30
37
  custom_snapshot_attributes.each do |key, attribute|
31
38
  snapshot[key] = record.send(attribute)
32
39
  end
data/lib/snapshotable.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'snapshotable/snapshots'
3
+ require 'snapshotable/model'
4
4
 
5
5
  module Snapshotable
6
6
  class << self
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'services/snapshot_creator'
4
4
  require 'snapshotable/model_config'
5
+ require 'hashdiff'
5
6
 
6
7
  module Snapshotable
7
8
  module Model
@@ -37,12 +38,19 @@ module Snapshotable
37
38
  send(snapshot_association_name)
38
39
  end
39
40
 
41
+ def last_snapshot_before(time = Time.now)
42
+ snapshots.order(created_at: :desc).where('created_at < ?', time).first
43
+ end
44
+
45
+ def last_snapshot
46
+ snapshots.order(created_at: :desc).first
47
+ end
48
+
40
49
  def snapshot_class
41
50
  Object.const_get(snapshot_class_name)
42
51
  end
43
52
 
44
53
  def should_create_new_snapshot?(snapshot)
45
- last_snapshot = snapshots.last
46
54
  return true if last_snapshot.nil?
47
55
 
48
56
  snapshot_to_compare = last_snapshot
@@ -54,4 +62,4 @@ module Snapshotable
54
62
  end
55
63
  end
56
64
  end
57
- end
65
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Snapshotable
2
4
  class ModelConfig
3
5
  DEFAULT_ATTRIBUTES = [].freeze
@@ -12,6 +14,7 @@ module Snapshotable
12
14
  setup_snapshot_names
13
15
  setup_variables
14
16
  setup_association(@model)
17
+ setup_callback(@model)
15
18
 
16
19
  @model.send :include, ::Snapshotable::Model::InstanceMethods
17
20
  end
@@ -36,21 +39,31 @@ module Snapshotable
36
39
  end
37
40
 
38
41
  def setup_variables
42
+ setup_attributes_to_save_on_snapshot
43
+ setup_attributes_to_ignore_on_diff
44
+ setup_custom_snapshot_attributes
45
+
46
+ @model.class_attribute :snapshot_configured
47
+ @model.snapshot_configured = true
48
+ @model.send :attr_accessor, :snapshot_configured
49
+ end
50
+
51
+ def setup_attributes_to_save_on_snapshot
39
52
  @model.class_attribute :attributes_to_save_on_snapshot, instance_writer: false
40
53
  @model.attributes_to_save_on_snapshot = DEFAULT_ATTRIBUTES
41
54
  @model.send :attr_accessor, :attributes_to_save_on_snapshot
55
+ end
42
56
 
57
+ def setup_attributes_to_ignore_on_diff
43
58
  @model.class_attribute :attributes_to_ignore_on_diff, instance_writer: false
44
59
  @model.attributes_to_ignore_on_diff = DEFAULT_IGNORE_ATTRIBUTES
45
60
  @model.send :attr_accessor, :attributes_to_ignore_on_diff
61
+ end
46
62
 
63
+ def setup_custom_snapshot_attributes
47
64
  @model.class_attribute :custom_snapshot_attributes, instance_writer: false
48
65
  @model.custom_snapshot_attributes = DEFAULT_CUSTOM_ATTRIBUTES
49
66
  @model.send :attr_accessor, :custom_snapshot_attributes
50
-
51
- @model.class_attribute :snapshot_configured
52
- @model.snapshot_configured = true
53
- @model.send :attr_accessor, :snapshot_configured
54
67
  end
55
68
 
56
69
  def setup_snapshot_names
@@ -59,6 +72,9 @@ module Snapshotable
59
72
 
60
73
  @model.class_attribute :snapshot_class_name
61
74
  @model.snapshot_class_name = snapshot_class_name
75
+
76
+ @model.class_attribute :snapshot_foreign_key
77
+ @model.snapshot_foreign_key = snapshot_foreign_key
62
78
  end
63
79
 
64
80
  def setup_association(klass)
@@ -69,6 +85,12 @@ module Snapshotable
69
85
  )
70
86
  end
71
87
 
88
+ def setup_callback(klass)
89
+ klass.after_create(
90
+ :take_snapshot!
91
+ )
92
+ end
93
+
72
94
  def snapshot_class_name
73
95
  "#{@model.name}Snapshot"
74
96
  end
@@ -81,4 +103,4 @@ module Snapshotable
81
103
  "#{@model.name.downcase}_id"
82
104
  end
83
105
  end
84
- end
106
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Snapshotable
4
- VERSION = '0.0.3'.freeze
4
+ VERSION = '0.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapshotable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Batista Marinho
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-08-10 00:00:00.000000000 Z
12
+ date: 2018-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -85,6 +85,34 @@ dependencies:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
87
  version: '1.16'
88
+ - !ruby/object:Gem::Dependency
89
+ name: factory_bot_rails
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '4.0'
95
+ type: :development
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '4.0'
102
+ - !ruby/object:Gem::Dependency
103
+ name: pg
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.21.0
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: 0.21.0
88
116
  - !ruby/object:Gem::Dependency
89
117
  name: pry-rails
90
118
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +161,20 @@ dependencies:
133
161
  - - "~>"
134
162
  - !ruby/object:Gem::Version
135
163
  version: '3.0'
164
+ - !ruby/object:Gem::Dependency
165
+ name: rspec-rails
166
+ requirement: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - "~>"
169
+ - !ruby/object:Gem::Version
170
+ version: '3.0'
171
+ type: :development
172
+ prerelease: false
173
+ version_requirements: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: '3.0'
136
178
  - !ruby/object:Gem::Dependency
137
179
  name: rubocop
138
180
  requirement: !ruby/object:Gem::Requirement
@@ -153,7 +195,7 @@ dependencies:
153
195
  - - "<"
154
196
  - !ruby/object:Gem::Version
155
197
  version: '1'
156
- description: Caches a model in a time period
198
+ description: Stores a model attributes in a time period
157
199
  email: engineering@qulture.rocks
158
200
  executables: []
159
201
  extensions: []
@@ -166,10 +208,9 @@ files:
166
208
  - lib/generators/snapshotable/templates/model.rb
167
209
  - lib/services/snapshot_creator.rb
168
210
  - lib/snapshotable.rb
211
+ - lib/snapshotable/model.rb
169
212
  - lib/snapshotable/model_config.rb
170
- - lib/snapshotable/snapshots.rb
171
213
  - lib/snapshotable/version.rb
172
- - lib/tasks/cacheable_models.rake
173
214
  homepage: https://github.com/QultureRocks/snapshotable
174
215
  licenses:
175
216
  - MIT
@@ -193,5 +234,5 @@ rubyforge_project:
193
234
  rubygems_version: 2.6.14
194
235
  signing_key:
195
236
  specification_version: 4
196
- summary: Caches a model in a time period.
237
+ summary: Stores a model attributes in a time period.
197
238
  test_files: []
File without changes