yolk-memento 0.2.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.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +85 -0
- data/Rakefile +32 -0
- data/TODO +3 -0
- data/VERSION.yml +4 -0
- data/generators/memento_migration/memento_migration_generator.rb +10 -0
- data/generators/memento_migration/templates/migration.rb +23 -0
- data/lib/memento.rb +56 -0
- data/lib/memento/action.rb +32 -0
- data/lib/memento/action/create.rb +41 -0
- data/lib/memento/action/destroy.rb +19 -0
- data/lib/memento/action/update.rb +55 -0
- data/lib/memento/action_controller_methods.rb +19 -0
- data/lib/memento/active_record_methods.rb +74 -0
- data/lib/memento/result.rb +36 -0
- data/lib/memento/session.rb +29 -0
- data/lib/memento/state.rb +71 -0
- data/memento.gemspec +75 -0
- data/rails/init.rb +1 -0
- data/spec/memento/action/create_spec.rb +96 -0
- data/spec/memento/action/destroy_spec.rb +43 -0
- data/spec/memento/action/update_spec.rb +209 -0
- data/spec/memento/action_controller_methods_spec.rb +55 -0
- data/spec/memento/active_record_methods_spec.rb +65 -0
- data/spec/memento/result_spec.rb +89 -0
- data/spec/memento/session_spec.rb +181 -0
- data/spec/memento/state_spec.rb +105 -0
- data/spec/memento_spec.rb +156 -0
- data/spec/spec_helper.rb +91 -0
- metadata +92 -0
@@ -0,0 +1,156 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Memento do
|
4
|
+
before do
|
5
|
+
setup_db
|
6
|
+
setup_data
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
shutdown_db
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be a singleton" do
|
14
|
+
Memento.instance.should be_kind_of(Singleton)
|
15
|
+
Memento.instance.should eql(Memento.instance)
|
16
|
+
lambda{ Memento.new }.should raise_error(NoMethodError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not be memento by default" do
|
20
|
+
Memento.instance.should_not be_active
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "start" do
|
24
|
+
|
25
|
+
before do
|
26
|
+
Memento.instance.start(@user)
|
27
|
+
@session = Memento.instance.instance_variable_get("@session")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should require user or user_id on start" do
|
31
|
+
lambda{ Memento.instance.start }.should raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should set an unsaved memento_session when starting" do
|
35
|
+
Memento::Session.count.should eql(0)
|
36
|
+
@session.should be_kind_of(Memento::Session)
|
37
|
+
@session.should be_new_record
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should set user on session" do
|
41
|
+
@session.user.should eql(User.first)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should set user when passing in id as integer" do
|
45
|
+
Memento.instance.start(User.create(:name => "MyUser2").id)
|
46
|
+
Memento.instance.instance_variable_get("@session").user.should eql(User.last)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not start memento when user does not exists/is invalid" do
|
50
|
+
Memento.instance.stop
|
51
|
+
Memento.instance.start(123333)
|
52
|
+
Memento.instance.should_not be_active
|
53
|
+
Memento.instance.start("123")
|
54
|
+
Memento.instance.should_not be_active
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should be memento" do
|
58
|
+
Memento.instance.should be_active
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "stop" do
|
64
|
+
before do
|
65
|
+
Memento.instance.start(@user)
|
66
|
+
Memento.instance.stop
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not be memento" do
|
70
|
+
Memento.instance.should_not be_active
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should remove session if no states created" do
|
74
|
+
Memento::Session.count.should eql(0)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "memento block" do
|
79
|
+
|
80
|
+
it "should record inside of block and stop after" do
|
81
|
+
Memento.instance.should_not be_active
|
82
|
+
Memento.instance.memento(@user) do
|
83
|
+
Memento.instance.should be_active
|
84
|
+
end
|
85
|
+
Memento.instance.should_not be_active
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should give back session when states created" do
|
89
|
+
Memento.instance.memento(@user) do
|
90
|
+
Project.create!
|
91
|
+
end.should be_a(Memento::Session)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should give back false when no states created" do
|
95
|
+
Memento.instance.memento(@user) do
|
96
|
+
1 + 1
|
97
|
+
end.should be_false
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should raise error in block and stop session" do
|
101
|
+
lambda {
|
102
|
+
Memento.instance.memento(@user) do
|
103
|
+
raise StandardError
|
104
|
+
end.should be_nil
|
105
|
+
}.should raise_error(StandardError)
|
106
|
+
Memento.instance.should_not be_active
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "when active" do
|
112
|
+
before do
|
113
|
+
@project = Project.create(:name => "P1")
|
114
|
+
Memento.instance.start(@user)
|
115
|
+
end
|
116
|
+
|
117
|
+
after do
|
118
|
+
Memento.instance.stop
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should create memento_state for ar-object with action_type" do
|
122
|
+
Memento::State.count.should eql(0)
|
123
|
+
Memento.instance.add_state :destroy, @project
|
124
|
+
Memento::State.count.should eql(1)
|
125
|
+
Memento::State.first.action_type.should eql("destroy")
|
126
|
+
Memento::State.first.record.should eql(Project.last)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should save session on first added state" do
|
130
|
+
Memento::Session.count.should eql(0)
|
131
|
+
Memento.instance.add_state :destroy, @project
|
132
|
+
Memento::Session.count.should eql(1)
|
133
|
+
end
|
134
|
+
|
135
|
+
describe "when ignoring" do
|
136
|
+
it "should NOT create memento_state for ar-object with action_type" do
|
137
|
+
Memento.instance.ignore do
|
138
|
+
Memento.instance.add_state :destroy, Project.create(:name => "P1")
|
139
|
+
end
|
140
|
+
|
141
|
+
Memento::State.count.should eql(0)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "when not active" do
|
148
|
+
|
149
|
+
it "should NOT create memento_state for ar-object with action_type" do
|
150
|
+
Memento.instance.add_state :destroy, Project.create(:name => "P1")
|
151
|
+
Memento::State.count.should eql(0)
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_record'
|
4
|
+
require 'action_controller'
|
5
|
+
require 'spec'
|
6
|
+
|
7
|
+
# Initialize time_zones from rails
|
8
|
+
Time.zone_default = Time.__send__(:get_zone, 'Berlin') || raise("Err")
|
9
|
+
ActiveRecord::Base.time_zone_aware_attributes = true
|
10
|
+
ActiveRecord::Base.default_timezone = :utc
|
11
|
+
|
12
|
+
$:.unshift(File.dirname(__FILE__))
|
13
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
require 'memento'
|
15
|
+
|
16
|
+
Spec::Runner.configure do |config|
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
21
|
+
# catch AR schema statements
|
22
|
+
$stdout = StringIO.new
|
23
|
+
|
24
|
+
def setup_db
|
25
|
+
ActiveRecord::Schema.define(:version => 1) do
|
26
|
+
create_table :projects do |t|
|
27
|
+
t.column :name, :string
|
28
|
+
t.column :closed_at, :datetime
|
29
|
+
t.column :notes, :string
|
30
|
+
t.references :customer
|
31
|
+
t.integer :ignore_this
|
32
|
+
t.timestamps
|
33
|
+
end
|
34
|
+
|
35
|
+
create_table :users do |t|
|
36
|
+
t.column :email, :string
|
37
|
+
t.column :name, :string
|
38
|
+
t.timestamps
|
39
|
+
end
|
40
|
+
|
41
|
+
create_table :customers do |t|
|
42
|
+
t.column :name, :string
|
43
|
+
t.timestamps
|
44
|
+
end
|
45
|
+
|
46
|
+
create_table :timestampless_objects do |t|
|
47
|
+
t.column :name, :string
|
48
|
+
end
|
49
|
+
|
50
|
+
create_table :memento_sessions do |t|
|
51
|
+
t.references :user
|
52
|
+
t.timestamps
|
53
|
+
end
|
54
|
+
|
55
|
+
create_table :memento_states do |t|
|
56
|
+
t.string :action_type
|
57
|
+
t.binary :record_data, :limit => 1.megabytes
|
58
|
+
t.references :record, :polymorphic => true
|
59
|
+
t.references :session
|
60
|
+
t.timestamps
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def setup_data
|
67
|
+
@user = User.create(:name => "MyUser")
|
68
|
+
end
|
69
|
+
|
70
|
+
def shutdown_db
|
71
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
72
|
+
ActiveRecord::Base.connection.drop_table(table)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class User < ActiveRecord::Base
|
77
|
+
end
|
78
|
+
|
79
|
+
class Customer < ActiveRecord::Base
|
80
|
+
has_many :projects
|
81
|
+
end
|
82
|
+
|
83
|
+
class Project < ActiveRecord::Base
|
84
|
+
belongs_to :customer
|
85
|
+
|
86
|
+
memento_changes :ignore => :ignore_this
|
87
|
+
end
|
88
|
+
|
89
|
+
class TimestamplessObject < ActiveRecord::Base
|
90
|
+
memento_changes
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yolk-memento
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yolk Sebastian Munz & Julia Soergel GbR
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-09 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: sebastian@yo.lk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- TODO
|
31
|
+
- VERSION.yml
|
32
|
+
- generators/memento_migration/memento_migration_generator.rb
|
33
|
+
- generators/memento_migration/templates/migration.rb
|
34
|
+
- lib/memento.rb
|
35
|
+
- lib/memento/action.rb
|
36
|
+
- lib/memento/action/create.rb
|
37
|
+
- lib/memento/action/destroy.rb
|
38
|
+
- lib/memento/action/update.rb
|
39
|
+
- lib/memento/action_controller_methods.rb
|
40
|
+
- lib/memento/active_record_methods.rb
|
41
|
+
- lib/memento/result.rb
|
42
|
+
- lib/memento/session.rb
|
43
|
+
- lib/memento/state.rb
|
44
|
+
- memento.gemspec
|
45
|
+
- rails/init.rb
|
46
|
+
- spec/memento/action/create_spec.rb
|
47
|
+
- spec/memento/action/destroy_spec.rb
|
48
|
+
- spec/memento/action/update_spec.rb
|
49
|
+
- spec/memento/action_controller_methods_spec.rb
|
50
|
+
- spec/memento/active_record_methods_spec.rb
|
51
|
+
- spec/memento/result_spec.rb
|
52
|
+
- spec/memento/session_spec.rb
|
53
|
+
- spec/memento/state_spec.rb
|
54
|
+
- spec/memento_spec.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/yolk/memento
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.2.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Undo for Rails/ActiveRecord - covers destroy, update and create
|
82
|
+
test_files:
|
83
|
+
- spec/memento/action/create_spec.rb
|
84
|
+
- spec/memento/action/destroy_spec.rb
|
85
|
+
- spec/memento/action/update_spec.rb
|
86
|
+
- spec/memento/action_controller_methods_spec.rb
|
87
|
+
- spec/memento/active_record_methods_spec.rb
|
88
|
+
- spec/memento/result_spec.rb
|
89
|
+
- spec/memento/session_spec.rb
|
90
|
+
- spec/memento/state_spec.rb
|
91
|
+
- spec/memento_spec.rb
|
92
|
+
- spec/spec_helper.rb
|