active_record_snapshot 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +107 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +20 -0
- data/README.md +15 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/active_record_snapshot.rb +10 -0
- data/lib/active_record_snapshot/snapped.rb +94 -0
- data/lib/active_record_snapshot/snapshot.rb +34 -0
- data/spec/active_record_snapshot_spec.rb +6 -0
- data/spec/functional/schema_creation_spec.rb +15 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/spec_simple_models.rb +37 -0
- data/spec/unit/snapped_spec.rb +131 -0
- data/spec/unit/snapshot_spec.rb +19 -0
- metadata +233 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NDVhNDFlODRhNTQ2NTQwMmZjMWE3MzBlYjg4MjE0OThhNzA2MzQwZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZDczNzYxMmUyZmUzZjgxNjgzMDkxMWZiNzlkYmU3MzY2NTUxNTU3Nw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjYwOGQwZTA5MmVmYTliNTZmZDE3YWFlNDA0M2M0ZTM5NThjMzExYTljYTNh
|
10
|
+
N2IxYzI1MTc4OThiNjY5NDA4YjBkMmNjZTNmNGE4YWQ2OTEzNzU3NDE3YzZk
|
11
|
+
MjE0ZjAzYTVkNTI4ODVlNDFkYWI0OTgyNjJlN2ZkZmYzMWUyYzU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTBhYzgxZTZiOTQ2ZTZjMTQxODVhMDgxOTQ1Njc2ZGQxNjUyNjJiODg5ZDkw
|
14
|
+
ZjhmNDNlYjcwZWMwMWE4MjMzNzMyYjkxMWZkZDY0MzkwZjAzOTExNWRhYTg0
|
15
|
+
NDJmNDU5ZTZmNWRhYzQwMTdhYTZlMGNjMzBlYTI1NTVkMTk5NWM=
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem "rspec", "~> 2.14.0"
|
5
|
+
gem "bundler"
|
6
|
+
gem "jeweler", "~> 1.8.4"
|
7
|
+
gem 'database_cleaner', "=1.0.1"
|
8
|
+
gem "pry"
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "delorean"
|
11
|
+
|
12
|
+
gem 'activesupport', ">=3.2.14"
|
13
|
+
gem 'activerecord', ">=3.2.14"
|
14
|
+
gem 'protected_attributes'
|
15
|
+
end
|
16
|
+
|
17
|
+
gem 'hashie'
|
18
|
+
gem 'sqlite3'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (4.0.0)
|
5
|
+
activesupport (= 4.0.0)
|
6
|
+
builder (~> 3.1.0)
|
7
|
+
activerecord (4.0.0)
|
8
|
+
activemodel (= 4.0.0)
|
9
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
10
|
+
activesupport (= 4.0.0)
|
11
|
+
arel (~> 4.0.0)
|
12
|
+
activerecord-deprecated_finders (1.0.3)
|
13
|
+
activesupport (4.0.0)
|
14
|
+
i18n (~> 0.6, >= 0.6.4)
|
15
|
+
minitest (~> 4.2)
|
16
|
+
multi_json (~> 1.3)
|
17
|
+
thread_safe (~> 0.1)
|
18
|
+
tzinfo (~> 0.3.37)
|
19
|
+
addressable (2.3.5)
|
20
|
+
arel (4.0.0)
|
21
|
+
atomic (1.1.12)
|
22
|
+
builder (3.1.4)
|
23
|
+
chronic (0.9.1)
|
24
|
+
coderay (1.0.9)
|
25
|
+
database_cleaner (1.0.1)
|
26
|
+
delorean (2.1.0)
|
27
|
+
chronic
|
28
|
+
diff-lcs (1.2.4)
|
29
|
+
faraday (0.8.8)
|
30
|
+
multipart-post (~> 1.2.0)
|
31
|
+
git (1.2.5)
|
32
|
+
github_api (0.10.1)
|
33
|
+
addressable
|
34
|
+
faraday (~> 0.8.1)
|
35
|
+
hashie (>= 1.2)
|
36
|
+
multi_json (~> 1.4)
|
37
|
+
nokogiri (~> 1.5.2)
|
38
|
+
oauth2
|
39
|
+
hashie (2.0.5)
|
40
|
+
highline (1.6.19)
|
41
|
+
httpauth (0.2.0)
|
42
|
+
i18n (0.6.4)
|
43
|
+
jeweler (1.8.7)
|
44
|
+
builder
|
45
|
+
bundler (~> 1.0)
|
46
|
+
git (>= 1.2.5)
|
47
|
+
github_api (= 0.10.1)
|
48
|
+
highline (>= 1.6.15)
|
49
|
+
nokogiri (= 1.5.10)
|
50
|
+
rake
|
51
|
+
rdoc
|
52
|
+
json (1.8.0)
|
53
|
+
jwt (0.1.8)
|
54
|
+
multi_json (>= 1.5)
|
55
|
+
method_source (0.8.2)
|
56
|
+
minitest (4.7.5)
|
57
|
+
multi_json (1.7.9)
|
58
|
+
multi_xml (0.5.5)
|
59
|
+
multipart-post (1.2.0)
|
60
|
+
nokogiri (1.5.10)
|
61
|
+
oauth2 (0.9.2)
|
62
|
+
faraday (~> 0.8)
|
63
|
+
httpauth (~> 0.2)
|
64
|
+
jwt (~> 0.1.4)
|
65
|
+
multi_json (~> 1.0)
|
66
|
+
multi_xml (~> 0.5)
|
67
|
+
rack (~> 1.2)
|
68
|
+
protected_attributes (1.0.3)
|
69
|
+
activemodel (>= 4.0.0, < 5.0)
|
70
|
+
pry (0.9.12.2)
|
71
|
+
coderay (~> 1.0.5)
|
72
|
+
method_source (~> 0.8)
|
73
|
+
slop (~> 3.4)
|
74
|
+
rack (1.5.2)
|
75
|
+
rake (10.1.0)
|
76
|
+
rdoc (3.12.2)
|
77
|
+
json (~> 1.4)
|
78
|
+
rspec (2.14.1)
|
79
|
+
rspec-core (~> 2.14.0)
|
80
|
+
rspec-expectations (~> 2.14.0)
|
81
|
+
rspec-mocks (~> 2.14.0)
|
82
|
+
rspec-core (2.14.4)
|
83
|
+
rspec-expectations (2.14.1)
|
84
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
85
|
+
rspec-mocks (2.14.3)
|
86
|
+
slop (3.4.6)
|
87
|
+
sqlite3 (1.3.7)
|
88
|
+
thread_safe (0.1.2)
|
89
|
+
atomic
|
90
|
+
tzinfo (0.3.37)
|
91
|
+
|
92
|
+
PLATFORMS
|
93
|
+
ruby
|
94
|
+
|
95
|
+
DEPENDENCIES
|
96
|
+
activerecord (>= 3.2.14)
|
97
|
+
activesupport (>= 3.2.14)
|
98
|
+
bundler
|
99
|
+
database_cleaner (= 1.0.1)
|
100
|
+
delorean
|
101
|
+
hashie
|
102
|
+
jeweler (~> 1.8.4)
|
103
|
+
protected_attributes
|
104
|
+
pry
|
105
|
+
rdoc (~> 3.12)
|
106
|
+
rspec (~> 2.14.0)
|
107
|
+
sqlite3
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 dannguyen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 dannguyen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
active_record_snapshot
|
2
|
+
======================
|
3
|
+
|
4
|
+
A simple gem for versioning selected fields of ActiveRecords.
|
5
|
+
|
6
|
+
|
7
|
+
````
|
8
|
+
class Book < ActiveRecord::Base
|
9
|
+
include ActiveRecordSnapshot::Snapped
|
10
|
+
|
11
|
+
end
|
12
|
+
````
|
13
|
+
|
14
|
+
NOTE: Just an alpha alpha alpha version. Schema migration has not been made yet
|
15
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "active_record_snapshot"
|
18
|
+
gem.homepage = "http://github.com/dannguyen/active_record_snapshot"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A system for storing snapshots of ActiveRecord objects}
|
21
|
+
gem.description = %Q{For data records that only need to be partially updated, this provides versioning for dirty attributes}
|
22
|
+
gem.email = "dansonguyen@gmail.com"
|
23
|
+
gem.authors = ["dannguyen"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "active_record_snapshot #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module ActiveRecordSnapshot
|
2
|
+
module Snapped
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_attribute :snapshot_config
|
8
|
+
self.snapshot_config = Hashie::Mash.new
|
9
|
+
|
10
|
+
has_many :snapshots,
|
11
|
+
class_name: self.snapshot_class_name,
|
12
|
+
foreign_key: 'snapped_id'
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
def snapshot_class_name
|
19
|
+
[self.name.singularize, 'Snapshot'].join
|
20
|
+
end
|
21
|
+
|
22
|
+
def snapshot_table_name
|
23
|
+
snapshot_class_name.tableize
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def snapped_attributes
|
28
|
+
snapshot_config
|
29
|
+
end
|
30
|
+
|
31
|
+
# usage:
|
32
|
+
# snapshot :view_count, when: :dirty
|
33
|
+
def snapshot(attr_name, opts={})
|
34
|
+
mash_opts = Hashie::Mash.new(opts)
|
35
|
+
|
36
|
+
if !self.attribute_names.include?(attr_name.to_s)
|
37
|
+
raise ActiveRecord::UnknownAttributeError, "#{attr_name} is not an attribute of #{self.name}"
|
38
|
+
end
|
39
|
+
|
40
|
+
att_sym = attr_name.to_sym
|
41
|
+
|
42
|
+
# code smell TODO, make SnapshotConfig its own class, rather than a managed Mash
|
43
|
+
config_mash = Hashie::Mash.new
|
44
|
+
|
45
|
+
|
46
|
+
val_when = case mash_opts[:when]
|
47
|
+
when :always
|
48
|
+
:always
|
49
|
+
when :dirty
|
50
|
+
:dirty
|
51
|
+
when nil
|
52
|
+
:dirty
|
53
|
+
else
|
54
|
+
raise ArgumentError, "#{mash_opts[:when]} is not a valid :when option. Only :dirty and :always allowed"
|
55
|
+
end
|
56
|
+
|
57
|
+
config_mash[:when] = val_when
|
58
|
+
|
59
|
+
self.snapshot_config[att_sym] = config_mash
|
60
|
+
end
|
61
|
+
end # classmethods
|
62
|
+
|
63
|
+
|
64
|
+
def save_snapshot
|
65
|
+
# this should be refactored somewhere...
|
66
|
+
record = self
|
67
|
+
snapshot_mashie = Hashie::Mash.new
|
68
|
+
|
69
|
+
|
70
|
+
record.class.snapped_attributes.each_pair do |att_name, att_config|
|
71
|
+
att_str = att_name.to_s
|
72
|
+
# if there is a change, this copies the prior version. If not, this stores the current attribute value
|
73
|
+
|
74
|
+
is_att_changed = !(record.changes[att_str].nil? )
|
75
|
+
previous_or_current_val = is_att_changed ? record.changes[att_str][-1] : record.attributes[att_str]
|
76
|
+
|
77
|
+
if att_config[:when] == :always
|
78
|
+
# when :always, we save either the previous value (if changed) or the current value
|
79
|
+
snapshot_mashie[att_name] = previous_or_current_val
|
80
|
+
elsif att_config[:when] == :dirty && is_att_changed
|
81
|
+
snapshot_mashie[att_name] = previous_or_current_val
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# now create snapshot_record with snapshot_mashie as the assigned attributes
|
86
|
+
# TODO: code smell, according to Demeter
|
87
|
+
new_snapshot = self.class.snapshot_class_name.constantize.create(snapshot_mashie)
|
88
|
+
|
89
|
+
return new_snapshot
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveRecordSnapshot
|
2
|
+
module Snapshot
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_save :set_snapped_at
|
7
|
+
|
8
|
+
# belongs_to :snapped,
|
9
|
+
# class_name: self.snapped_class_name,
|
10
|
+
# foreign_key: 'snapped_id'
|
11
|
+
|
12
|
+
# TODO: refactor safely
|
13
|
+
self.attribute_names.each do |att|
|
14
|
+
attr_accessible att
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def set_snapped_at
|
27
|
+
self.snapped_at = Time.now
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'ActiveRecordSnapshot::SchemaCreation' do
|
4
|
+
|
5
|
+
it 'should read from the snapshot_config'
|
6
|
+
it 'should have tablename of snapped_klass_snapshots'
|
7
|
+
it 'should create a snapped_at field'
|
8
|
+
it 'should create a snapped_id foreign_key field'
|
9
|
+
it 'should by default not have timestamps'
|
10
|
+
it 'should mimic column type'
|
11
|
+
it 'should mimic column name'
|
12
|
+
it 'should mimic column properties'
|
13
|
+
|
14
|
+
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'database_cleaner'
|
7
|
+
require 'sqlite3'
|
8
|
+
require 'delorean'
|
9
|
+
|
10
|
+
require 'active_record_snapshot'
|
11
|
+
require 'protected_attributes'
|
12
|
+
|
13
|
+
# Requires supporting files with custom matchers and macros, etc,
|
14
|
+
# in ./support/ and its subdirectories.
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
16
|
+
|
17
|
+
#DatabaseCleaner.strategy = :truncation
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# Use color in STDOUT
|
21
|
+
config.color_enabled = true
|
22
|
+
|
23
|
+
# Use color not only in STDOUT but also in pagers and files
|
24
|
+
config.tty = true
|
25
|
+
|
26
|
+
# Use the specified formatter
|
27
|
+
config.formatter = :documentation # :progress, :html, :textmate
|
28
|
+
|
29
|
+
config.before(:each) do
|
30
|
+
DatabaseCleaner.start
|
31
|
+
end
|
32
|
+
config.after(:each) do
|
33
|
+
DatabaseCleaner.clean
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ActiveRecord::Base.establish_connection(
|
38
|
+
:adapter => "sqlite3",
|
39
|
+
:database => ":memory:"
|
40
|
+
)
|
41
|
+
ActiveRecord::Migration.verbose = false
|
42
|
+
|
43
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
create_table :books do |t|
|
3
|
+
t.string :title
|
4
|
+
t.integer :edition
|
5
|
+
t.integer :year
|
6
|
+
t.float :price
|
7
|
+
t.integer :rank
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :book_snapshots do |t|
|
12
|
+
t.integer :snapped_id
|
13
|
+
t.datetime :snapped_at
|
14
|
+
|
15
|
+
t.string :title
|
16
|
+
t.integer :edition
|
17
|
+
t.float :price
|
18
|
+
t.integer :rank
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
class Book < ActiveRecord::Base
|
25
|
+
include ActiveRecordSnapshot::Snapped
|
26
|
+
|
27
|
+
snapshot :title # default: :dirty
|
28
|
+
snapshot :price, when: :always
|
29
|
+
snapshot :edition, when: :dirty
|
30
|
+
snapshot :rank, when: :always
|
31
|
+
end
|
32
|
+
|
33
|
+
class BookSnapshot < ActiveRecord::Base
|
34
|
+
include ActiveRecordSnapshot::Snapshot
|
35
|
+
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'spec_simple_models'
|
3
|
+
|
4
|
+
|
5
|
+
describe "ActiveRecordSnapshot::Snapped" do
|
6
|
+
it "passes" do
|
7
|
+
expect(true).to be true
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'class concerns' do
|
11
|
+
|
12
|
+
## Using Book and BookSnapshot from spec_simple_models
|
13
|
+
|
14
|
+
describe 'class configuration' do
|
15
|
+
it 'should have snapshot_class_name set to KlassSingularSnapshot' do
|
16
|
+
expect(Book.snapshot_class_name).to eq 'BookSnapshot'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should have :snapshot_table_name set to klass_singular_snapshots' do
|
20
|
+
expect(Book.snapshot_table_name).to eq 'book_snapshots'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#snapshot' do
|
25
|
+
it "should raise error if original attribute name doesn't exist" do
|
26
|
+
expect{Book.snapshot :not_attr}.to raise_error ActiveRecord::UnknownAttributeError
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
it 'should raise error if snapshot table does not exist'
|
31
|
+
|
32
|
+
|
33
|
+
context 'snapshot_config' do
|
34
|
+
it 'should create a new entry' do
|
35
|
+
# TODO: Book already exists as a class and has this set, but I'm lazy
|
36
|
+
# check spec_simple_models.rb to see snapshot config
|
37
|
+
|
38
|
+
# this has already been called: snapshot :title
|
39
|
+
expect(Book.snapshot_config.title).to be_a Hashie::Mash
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when:' do
|
43
|
+
it 'should set :dirty by default' do
|
44
|
+
expect(Book.snapshot_config[:title][:when]).to eq :dirty
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should allow :always' do
|
48
|
+
expect(Book.snapshot_config[:price][:when]).to eq :always
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should raise error if invalid symbol is passed in' do
|
52
|
+
expect{Book.snapshot :title, :when => :whatever}.to raise_error ArgumentError
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'named_scopes for instances' do
|
60
|
+
before(:each) do
|
61
|
+
# not testing actual construction, just building by hand
|
62
|
+
@book = Book.create(title: 'Moby Dick', price: 10.20)
|
63
|
+
BookSnapshot.create(title: 'Moby Dick', price: 5.00, snapped_id: @book.id)
|
64
|
+
BookSnapshot.create(title: 'Moby Dick', price: 2.00, snapped_id: @book.id)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should has_many :snapshots' do
|
68
|
+
expect(@book.snapshots.count).to eq 2
|
69
|
+
expect(Book.count).to eq 1 # not needed really
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'instance' do
|
76
|
+
|
77
|
+
describe '#save_snapshot' do
|
78
|
+
|
79
|
+
before(:each) do
|
80
|
+
|
81
|
+
Delorean.time_travel_to "1999-01-01 00:00:00" do
|
82
|
+
@book = Book.create(title: 'Gatsby', price: 42.30, edition: 1, rank: 3)
|
83
|
+
end
|
84
|
+
|
85
|
+
Delorean.time_travel_to "2000-01-01 00:00:00" do
|
86
|
+
@book.assign_attributes(title: 'Gatsby', price: 43.50, edition: 2)
|
87
|
+
@snapshot = @book.save_snapshot
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when :dirty attributes' do
|
93
|
+
it 'should snapshot when dirtied' do
|
94
|
+
expect(@snapshot.edition).to eq 2
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should not snapshot when clean' do
|
98
|
+
expect(@snapshot.title).to be_nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when :always attributes' do
|
103
|
+
it 'should snapshot when dirtied' do
|
104
|
+
expect(@snapshot.price).to eq 43.50
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should snapshot when clean' do
|
108
|
+
expect(@snapshot.rank).to eq 3
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
it 'should set :snapped_at to time of snapping' do
|
114
|
+
expect(@snapshot.snapped_at.year).to eq 2000
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'snapshot taken *after* save' do
|
118
|
+
it 'should not be any dirty attributes to save upon (by definition)'
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
describe 'optional before_save hook' do
|
127
|
+
it 'should save_snapshot with every record save'
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
describe "ActiveRecordSnapshot::Snapshot" do
|
2
|
+
|
3
|
+
it 'is true baby' do
|
4
|
+
expect(1).to be 1
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
describe 'def Snapshot' do
|
9
|
+
it 'should create new snapshot_record'
|
10
|
+
it 'should save proper snapped_id'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'scopes' do
|
14
|
+
describe '#latest' do
|
15
|
+
it 'should return snapped_at by DESC'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_snapshot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dannguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jeweler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.8.4
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.8.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rdoc
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.12'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ~>
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.12'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: delorean
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: activesupport
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 3.2.14
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 3.2.14
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: activerecord
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ! '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 3.2.14
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ! '>='
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 3.2.14
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: protected_attributes
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ! '>='
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: For data records that only need to be partially updated, this provides
|
182
|
+
versioning for dirty attributes
|
183
|
+
email: dansonguyen@gmail.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files:
|
187
|
+
- LICENSE
|
188
|
+
- LICENSE.txt
|
189
|
+
- README.md
|
190
|
+
files:
|
191
|
+
- .document
|
192
|
+
- .rspec
|
193
|
+
- Gemfile
|
194
|
+
- Gemfile.lock
|
195
|
+
- LICENSE
|
196
|
+
- LICENSE.txt
|
197
|
+
- README.md
|
198
|
+
- Rakefile
|
199
|
+
- VERSION
|
200
|
+
- lib/active_record_snapshot.rb
|
201
|
+
- lib/active_record_snapshot/snapped.rb
|
202
|
+
- lib/active_record_snapshot/snapshot.rb
|
203
|
+
- spec/active_record_snapshot_spec.rb
|
204
|
+
- spec/functional/schema_creation_spec.rb
|
205
|
+
- spec/spec_helper.rb
|
206
|
+
- spec/spec_simple_models.rb
|
207
|
+
- spec/unit/snapped_spec.rb
|
208
|
+
- spec/unit/snapshot_spec.rb
|
209
|
+
homepage: http://github.com/dannguyen/active_record_snapshot
|
210
|
+
licenses:
|
211
|
+
- MIT
|
212
|
+
metadata: {}
|
213
|
+
post_install_message:
|
214
|
+
rdoc_options: []
|
215
|
+
require_paths:
|
216
|
+
- lib
|
217
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
218
|
+
requirements:
|
219
|
+
- - ! '>='
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: '0'
|
222
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
223
|
+
requirements:
|
224
|
+
- - ! '>='
|
225
|
+
- !ruby/object:Gem::Version
|
226
|
+
version: '0'
|
227
|
+
requirements: []
|
228
|
+
rubyforge_project:
|
229
|
+
rubygems_version: 2.0.5
|
230
|
+
signing_key:
|
231
|
+
specification_version: 4
|
232
|
+
summary: A system for storing snapshots of ActiveRecord objects
|
233
|
+
test_files: []
|