rtiss_acts_as_versioned 0.9.3 → 0.10.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 +5 -5
- data/README.md +304 -0
- data/Rakefile +7 -7
- data/init.rb +3 -2
- data/lib/rtiss_acts_as_versioned/act_methods.rb +289 -0
- data/lib/rtiss_acts_as_versioned/version.rb +7 -0
- data/lib/rtiss_acts_as_versioned.rb +18 -355
- data/mise.toml.example +2 -0
- data/rtiss_acts_as_versioned.gemspec +11 -11
- data/test/abstract_unit.rb +6 -7
- data/test/fixtures/good_thing.rb +8 -0
- data/test/fixtures/migrations/2_add_versioned_tables.rb +3 -3
- data/test/fixtures/page.rb +4 -4
- data/test/fixtures/widget.rb +2 -4
- data/test/migration_test.rb +23 -21
- data/test/schema.rb +15 -59
- data/test/tiss_test.rb +143 -177
- data/test/versioned_test.rb +14 -14
- metadata +16 -34
- data/CHANGELOG +0 -84
- data/README +0 -37
- data/test/fixtures/locked_rolle.rb +0 -8
- data/test/fixtures/rolle.rb +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ba48e14919e54879af25c9b9315d4f81a0dbf092779d90f284e545a931e4b215
|
|
4
|
+
data.tar.gz: 273f38f60e68d30454137d01be23a314a304a6b1939095aa101d551f1331267d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 270322882e1b8dd6ea5a34842289a206f44bb671132a8bddadc9cfb5a38e1559d72af58696db9120865350a5978833765e2abecd408643f284d5d520405c6241
|
|
7
|
+
data.tar.gz: 54abff7098b77b267f8b16a3f3471e1ebaa609417640b09002ad1fcb9a7278d311a34f745ee9ebdac005c3f428fb4dc23fea97fefb9fcd49b7fc0be01c4a0854
|
data/README.md
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# rtiss_acts_as_versioned
|
|
2
|
+
|
|
3
|
+
Simple versioning for ActiveRecord models. Every change to a record is saved as a
|
|
4
|
+
copy in a separate history table.
|
|
5
|
+
|
|
6
|
+
This is a fork of [technoweenie/acts_as_versioned](https://github.com/technoweenie/acts_as_versioned)
|
|
7
|
+
(Rick Olson, 2005), used by [TISS](https://tiss.tuwien.ac.at) (TU Wien). It adds a few changes on top
|
|
8
|
+
of the original. The most important one: **destroyed records stay in the history table and can be
|
|
9
|
+
restored.**
|
|
10
|
+
|
|
11
|
+
> **Note:** These changes were made for the needs of TISS only. The gem is used and tested only with
|
|
12
|
+
> **Oracle** (via `activerecord-oracle_enhanced-adapter`) and **SQLite**. Other databases may work,
|
|
13
|
+
> but we do not support them. If you need a general-purpose versioning gem, consider the original
|
|
14
|
+
> or one of the actively maintained alternatives.
|
|
15
|
+
|
|
16
|
+
| Gem version | Rails | Ruby |
|
|
17
|
+
|-------------|----------------|-----------|
|
|
18
|
+
| 0.9.x | 4.x – 6.x | 2.x |
|
|
19
|
+
| 0.10.x | >= 7.1 (activerecord >= 7.1.6) | >= 3.1.6 |
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# Gemfile
|
|
25
|
+
gem 'rtiss_acts_as_versioned', '~> 0.10.2'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Differences from the original acts_as_versioned
|
|
31
|
+
|
|
32
|
+
If you know the original gem, these are the changes in the TISS fork.
|
|
33
|
+
|
|
34
|
+
### 1. History tables use the `_h` suffix
|
|
35
|
+
|
|
36
|
+
| | Original | TISS fork |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| Default table of the version model | `page_versions` | `pages_h` (`"#{table_name}_h"`) |
|
|
39
|
+
| Version class | `Page::Version` | `Page::Version` (unchanged) |
|
|
40
|
+
|
|
41
|
+
You can still change both with `:table_name` and `:class_name`.
|
|
42
|
+
|
|
43
|
+
### 2. Destroyed records stay in the history (`deleted_in_original_table`)
|
|
44
|
+
|
|
45
|
+
In the original gem, `destroy` also deleted all versions (`:dependent => :delete_all`).
|
|
46
|
+
In this fork:
|
|
47
|
+
|
|
48
|
+
* the `versions` association has **no** `:dependent` option, so versions survive `destroy`;
|
|
49
|
+
* an `after_destroy` callback (`set_deleted_flag`) writes **one more version**
|
|
50
|
+
(`highest_version + 1`) with `deleted_in_original_table = true`. The record's last state
|
|
51
|
+
is therefore always kept;
|
|
52
|
+
* `:association_options => { :dependent => :nullify }` is **forbidden** and raises an error,
|
|
53
|
+
because it would leave orphaned versions behind.
|
|
54
|
+
|
|
55
|
+
Note: `delete` / `delete_all` skip callbacks, so they do **not** create a deletion version.
|
|
56
|
+
|
|
57
|
+
### 3. Restoring destroyed records
|
|
58
|
+
|
|
59
|
+
New methods let you bring a destroyed record back from its history:
|
|
60
|
+
|
|
61
|
+
* `Version#restore` – restores the original record from this version (with the **same `id`**)
|
|
62
|
+
* `Model.restore_deleted(id)` – restores from the newest version
|
|
63
|
+
* `Model.restore_deleted_version(id, version)` – restores from a specific version
|
|
64
|
+
* `Version#original_record_exists?` – checks whether the original record still exists
|
|
65
|
+
|
|
66
|
+
A restore always creates a **new** version. That version stores, in the `record_restored`
|
|
67
|
+
column, which version the record was restored from.
|
|
68
|
+
|
|
69
|
+
### 4. `record_restored` column
|
|
70
|
+
|
|
71
|
+
The column holds the version number the record was restored from (`restore`) or reverted to
|
|
72
|
+
(`revert_to!`). It is `nil` for normal changes.
|
|
73
|
+
|
|
74
|
+
* `Version#record_restored?` / `Version#record_restored` → `true`/`false`
|
|
75
|
+
* `Version#record_restored_from_version` → `Integer` or `nil`
|
|
76
|
+
|
|
77
|
+
### 5. Version numbers only ever go up
|
|
78
|
+
|
|
79
|
+
In the original, `revert_to!(3)` set the record back to version number 3 and did not create a
|
|
80
|
+
new version. In the fork:
|
|
81
|
+
|
|
82
|
+
* `revert_to!(n)` copies the data from version `n` and saves it as a **new version**
|
|
83
|
+
(`max + 1`), with `record_restored = n`;
|
|
84
|
+
* `restore` also always gives the record a new, higher version number;
|
|
85
|
+
* the version column is **always separate from `lock_version`**. Do not set
|
|
86
|
+
`:version_column => 'lock_version'`. Optimistic locking and versioning work side by side.
|
|
87
|
+
|
|
88
|
+
The history therefore has no gaps and is append-only (an audit trail).
|
|
89
|
+
|
|
90
|
+
### 6. Extra lookup methods on the instance
|
|
91
|
+
|
|
92
|
+
* `find_version(n)` – returns version `n`. Raises a `RuntimeError` if it does not exist.
|
|
93
|
+
* `find_newest_version` – returns the newest version, including a deletion version.
|
|
94
|
+
* `highest_version` – the highest version number, or `-1` if there is none or the record is unsaved.
|
|
95
|
+
|
|
96
|
+
### 7. Other changes
|
|
97
|
+
|
|
98
|
+
* `:non_versioned_columns` option to exclude columns directly in the `acts_as_versioned` call.
|
|
99
|
+
* `:deleted_in_original_table_flag` and `:record_restored_column` options to rename the extra columns.
|
|
100
|
+
* `updated_at` on a version is set to `Time.now` when it is saved.
|
|
101
|
+
* `save_without_revision(perform_validation = true)` lets you skip validation.
|
|
102
|
+
* `create_versioned_table` also creates the `deleted_in_original_table` and
|
|
103
|
+
`record_restored` columns.
|
|
104
|
+
* The `belongs_to` from the version to the original model is `optional: true`
|
|
105
|
+
(the original may already be gone).
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Quick start
|
|
110
|
+
|
|
111
|
+
### 1. Migration
|
|
112
|
+
|
|
113
|
+
The main table needs a `version` column (integer). The history table holds the foreign key,
|
|
114
|
+
`version`, the two fork columns and every column you want versioned.
|
|
115
|
+
|
|
116
|
+
Easiest way, with the built-in helper:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
class AddVersioningToPages < ActiveRecord::Migration[7.1]
|
|
120
|
+
def up
|
|
121
|
+
Page.create_versioned_table # creates pages_h (and pages.version if missing)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def down
|
|
125
|
+
Page.drop_versioned_table
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Or by hand:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
create_table :pages do |t|
|
|
134
|
+
t.string :title
|
|
135
|
+
t.text :body
|
|
136
|
+
t.integer :version
|
|
137
|
+
t.integer :lock_version, default: 0 # optional, for optimistic locking
|
|
138
|
+
t.timestamps
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
create_table :pages_h do |t|
|
|
142
|
+
t.integer :page_id # foreign key to the original
|
|
143
|
+
t.integer :version
|
|
144
|
+
t.string :title
|
|
145
|
+
t.text :body
|
|
146
|
+
t.timestamps
|
|
147
|
+
t.boolean :deleted_in_original_table, default: false
|
|
148
|
+
t.integer :record_restored # nil, or a version number
|
|
149
|
+
end
|
|
150
|
+
add_index :pages_h, [:page_id, :version], unique: true
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
> `create_versioned_table` reads the columns from the model, so the main table must exist first.
|
|
154
|
+
> If you change the schema later, you have to update the `_h` table yourself, and restart the app:
|
|
155
|
+
> the version class reads its columns only once.
|
|
156
|
+
|
|
157
|
+
### 2. Model
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
class Page < ApplicationRecord
|
|
161
|
+
acts_as_versioned
|
|
162
|
+
end
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### 3. Creating and changing records
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
page = Page.create!(title: 'Hello')
|
|
169
|
+
page.version # => 1
|
|
170
|
+
page.versions.count # => 1
|
|
171
|
+
|
|
172
|
+
page.update!(title: 'Hello World')
|
|
173
|
+
page.version # => 2
|
|
174
|
+
|
|
175
|
+
page.update!(title: 'Hello World') # nothing changed, so no new version
|
|
176
|
+
page.version # => 2
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 4. Reading versions
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
page.versions # all versions (has_many, Page::Version)
|
|
183
|
+
page.versions.earliest # first version
|
|
184
|
+
page.versions.latest # newest version
|
|
185
|
+
page.versions.where(title: 'Hello') # works like any normal relation
|
|
186
|
+
|
|
187
|
+
page.find_version(1).title # => 'Hello'
|
|
188
|
+
page.find_version(99) # => RuntimeError
|
|
189
|
+
page.find_newest_version.version # => 2
|
|
190
|
+
page.highest_version # => 2
|
|
191
|
+
|
|
192
|
+
v = page.find_version(2)
|
|
193
|
+
v.previous # version 1
|
|
194
|
+
v.next # nil
|
|
195
|
+
v.page # the original record (nil if destroyed)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 5. Reverting to an older version
|
|
199
|
+
|
|
200
|
+
```ruby
|
|
201
|
+
page.revert_to(1) # changes attributes in memory only, does not save
|
|
202
|
+
page.revert_to!(1) # saves it as a NEW version
|
|
203
|
+
page.version # => 3
|
|
204
|
+
page.title # => 'Hello'
|
|
205
|
+
page.find_version(3).record_restored_from_version # => 1
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 6. Destroying and restoring
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
id = page.id
|
|
212
|
+
page.destroy
|
|
213
|
+
Page.exists?(id) # => false
|
|
214
|
+
|
|
215
|
+
v = Page::Version.where(page_id: id).order(:version).last
|
|
216
|
+
v.version # => 4
|
|
217
|
+
v.deleted_in_original_table # => true
|
|
218
|
+
v.original_record_exists? # => false
|
|
219
|
+
|
|
220
|
+
Page.restore_deleted(id) # restore from the newest version
|
|
221
|
+
# or: Page.restore_deleted_version(id, 2) # restore from version 2
|
|
222
|
+
# or: v.restore
|
|
223
|
+
|
|
224
|
+
page = Page.find(id) # same id as before
|
|
225
|
+
page.version # => 5
|
|
226
|
+
page.find_version(5).record_restored # => true
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`restore` raises a `RuntimeError` if the original record still exists.
|
|
230
|
+
|
|
231
|
+
### 7. Saving without a new version
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
page.title = 'Typo fix'
|
|
235
|
+
page.save_without_revision # saves, no new version
|
|
236
|
+
page.save_without_revision(false) # same, but skips validation
|
|
237
|
+
|
|
238
|
+
Page.without_revision do
|
|
239
|
+
Page.find(id).update!(title: 'Silent')
|
|
240
|
+
end
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
> ⚠️ `without_revision` swaps the callback methods **on the class** for the length of the block.
|
|
244
|
+
> This is not thread-safe. Other threads that save the same model during the block will not
|
|
245
|
+
> get versions either. `without_locking` likewise changes `ActiveRecord::Base.lock_optimistically`
|
|
246
|
+
> globally.
|
|
247
|
+
|
|
248
|
+
### 8. Options
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
class Page < ApplicationRecord
|
|
252
|
+
acts_as_versioned \
|
|
253
|
+
table_name: :page_history, # default: pages_h
|
|
254
|
+
class_name: 'Revision', # default: Version -> Page::Revision
|
|
255
|
+
foreign_key: :page_id, # default: page_id
|
|
256
|
+
version_column: :version, # never use :lock_version!
|
|
257
|
+
non_versioned_columns: [:view_count], # these columns are not versioned
|
|
258
|
+
if_changed: [:title, :body], # new version only if one of these changed
|
|
259
|
+
if: :versionable?, # Symbol or Proc(record) -> true/false
|
|
260
|
+
limit: 50, # keep at most N versions (older ones are deleted!)
|
|
261
|
+
sequence_name: 'page_history_seq', # e.g. for Oracle
|
|
262
|
+
association_options: { order: 'version' },
|
|
263
|
+
extend: PageVersionHelpers # module mixed into Page and Page::Revision
|
|
264
|
+
end
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Columns that are never versioned: primary key, `type` (STI), the version column,
|
|
268
|
+
`lock_version` and `versioned_type`. For STI models the `type` value is stored in
|
|
269
|
+
`versioned_type` (see `:inheritance_column`).
|
|
270
|
+
|
|
271
|
+
> ⚠️ `:limit` deletes old versions with `delete_all`. That also removes their deletion and
|
|
272
|
+
> restore information. In an audit setting you usually should not use it.
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Tests
|
|
277
|
+
|
|
278
|
+
```sh
|
|
279
|
+
bundle exec rake test
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
The tests run against SQLite (`test/database.yml`).
|
|
283
|
+
|
|
284
|
+
* `test/versioned_test.rb` – the original acts_as_versioned tests, adapted to the fork's behaviour
|
|
285
|
+
* `test/tiss_test.rb` – the fork features (destroy, restore, `record_restored`, `find_version`, …),
|
|
286
|
+
using the test model `GoodThing` (`name`, `why_good`), e.g. `GoodThing.create!(name: 'Sun', why_good: 'Keeps us warm')`
|
|
287
|
+
* `test/migration_test.rb` – `create_versioned_table`
|
|
288
|
+
|
|
289
|
+
## Release
|
|
290
|
+
|
|
291
|
+
```sh
|
|
292
|
+
rake build # updates the gemspec manifest and builds pkg/*.gem
|
|
293
|
+
rake release # commit, tag, push, gem push
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Resources
|
|
297
|
+
|
|
298
|
+
* RubyGems: https://rubygems.org/gems/rtiss_acts_as_versioned
|
|
299
|
+
* GitHub (fork): https://github.com/rtiss/rtiss_acts_as_versioned
|
|
300
|
+
* Original: https://github.com/technoweenie/acts_as_versioned
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
MIT, see [MIT-LICENSE](MIT-LICENSE).
|
data/Rakefile
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'rake'
|
|
3
3
|
require 'date'
|
|
4
|
+
require 'rtiss_acts_as_versioned/version'
|
|
4
5
|
|
|
5
6
|
#############################################################################
|
|
6
7
|
#
|
|
@@ -13,8 +14,7 @@ def name
|
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def version
|
|
16
|
-
|
|
17
|
-
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
|
17
|
+
ActiveRecord::Acts::Versioned::VERSION
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def date
|
|
@@ -122,11 +122,11 @@ task :gemspec => :validate do
|
|
|
122
122
|
end
|
|
123
123
|
|
|
124
124
|
task :validate do
|
|
125
|
-
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
|
126
|
-
unless libfiles.empty?
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
end
|
|
125
|
+
# libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
|
126
|
+
# unless libfiles.empty?
|
|
127
|
+
# puts "Directory `lib` should only contain a `/#{name}.rb` file and `/#{name}` dir."
|
|
128
|
+
# exit!
|
|
129
|
+
# end
|
|
130
130
|
unless Dir['VERSION*'].empty?
|
|
131
131
|
puts "A `VERSION` file at root level violates Gem best practices."
|
|
132
132
|
exit!
|
data/init.rb
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
require
|
|
1
|
+
Dir.glob(File.expand_path("lib/**/*.rb")).each do |file|
|
|
2
|
+
require file
|
|
3
|
+
end
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Acts
|
|
3
|
+
module Versioned
|
|
4
|
+
module ActMethods
|
|
5
|
+
def self.included(base) # :nodoc:
|
|
6
|
+
base.extend ClassMethods
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Saves a version of the model in the versioned table. This is called in the after_save callback by default
|
|
10
|
+
def save_version(save_this=false, deleted_flag=false, restored_from_version=nil)
|
|
11
|
+
if @saving_version || save_this
|
|
12
|
+
@saving_version = nil
|
|
13
|
+
rev = self.class.versioned_class.new
|
|
14
|
+
clone_versioned_model(self, rev)
|
|
15
|
+
rev.send("#{self.class.version_column}=", send(self.class.version_column))
|
|
16
|
+
rev.send("#{self.class.versioned_foreign_key}=", id)
|
|
17
|
+
rev.send("#{self.class.deleted_in_original_table_flag}=", deleted_flag)
|
|
18
|
+
rev.send("#{self.class.record_restored_column}=", restored_from_version)
|
|
19
|
+
if rev.respond_to? :updated_at=
|
|
20
|
+
rev.updated_at = Time.now
|
|
21
|
+
end
|
|
22
|
+
rev.save
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def set_deleted_flag
|
|
27
|
+
return if self.id.nil?
|
|
28
|
+
|
|
29
|
+
rev = self.class.versioned_class.new
|
|
30
|
+
clone_versioned_model(self, rev)
|
|
31
|
+
rev.send("#{self.class.version_column}=", highest_version+1)
|
|
32
|
+
rev.send("#{self.class.versioned_foreign_key}=", id)
|
|
33
|
+
rev.send("#{self.class.deleted_in_original_table_flag}=", true)
|
|
34
|
+
rev.send("#{self.class.record_restored_column}=", nil)
|
|
35
|
+
if rev.respond_to? :updated_at=
|
|
36
|
+
rev.updated_at = Time.now
|
|
37
|
+
end
|
|
38
|
+
rev.save
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
|
|
42
|
+
# Override this method to set your own criteria for clearing old versions.
|
|
43
|
+
def clear_old_versions
|
|
44
|
+
return if self.class.max_version_limit == 0
|
|
45
|
+
excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
|
|
46
|
+
if excess_baggage > 0
|
|
47
|
+
self.class.versioned_class.where("#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id).delete_all
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Reverts a model to a given version. Takes either a version number or an instance of the versioned model
|
|
52
|
+
def revert_to(version)
|
|
53
|
+
if version.is_a?(self.class.versioned_class)
|
|
54
|
+
@reverted_from = version.send(self.class.version_column)
|
|
55
|
+
return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record?
|
|
56
|
+
else
|
|
57
|
+
@reverted_from = version
|
|
58
|
+
version = versions.where(self.class.version_column => version).first
|
|
59
|
+
return false unless version
|
|
60
|
+
end
|
|
61
|
+
self.clone_versioned_model(version, self)
|
|
62
|
+
send("#{self.class.version_column}=", version.send(self.class.version_column))
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Reverts a model to a given version and saves the model.
|
|
67
|
+
# Takes either a version number or an instance of the versioned model
|
|
68
|
+
def revert_to!(version)
|
|
69
|
+
if revert_to(version)
|
|
70
|
+
set_new_version
|
|
71
|
+
save_without_revision
|
|
72
|
+
save_version(true, false, @reverted_from)
|
|
73
|
+
else
|
|
74
|
+
false
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
|
|
79
|
+
def save_without_revision(perform_validation = true)
|
|
80
|
+
ret = false
|
|
81
|
+
without_locking do
|
|
82
|
+
without_revision do
|
|
83
|
+
ret = save(validate: perform_validation)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
ret
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def save_without_revision!
|
|
90
|
+
without_locking do
|
|
91
|
+
without_revision do
|
|
92
|
+
save!
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def altered?
|
|
98
|
+
track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Clones a model. Used when saving a new version or reverting a model's version.
|
|
102
|
+
def clone_versioned_model(orig_model, new_model)
|
|
103
|
+
self.class.versioned_columns.each do |col|
|
|
104
|
+
new_model.send("#{col.name}=", orig_model.send(col.name)) if orig_model.has_attribute?(col.name)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
clone_inheritance_column(orig_model, new_model)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def clone_inheritance_column(orig_model, new_model)
|
|
111
|
+
if orig_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(new_model.class.inheritance_column.to_s)
|
|
112
|
+
new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
|
|
113
|
+
elsif new_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(self.class.versioned_inheritance_column.to_s)
|
|
114
|
+
new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
|
|
119
|
+
def save_version?
|
|
120
|
+
version_condition_met? && altered?
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Checks condition set in the :if option to check whether a revision should be created or not. Override this for
|
|
124
|
+
# custom version condition checking.
|
|
125
|
+
def version_condition_met?
|
|
126
|
+
case
|
|
127
|
+
when version_condition.is_a?(Symbol)
|
|
128
|
+
send(version_condition)
|
|
129
|
+
when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
|
|
130
|
+
version_condition.call(self)
|
|
131
|
+
else
|
|
132
|
+
version_condition
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Executes the block with the versioning callbacks disabled.
|
|
137
|
+
#
|
|
138
|
+
# @foo.without_revision do
|
|
139
|
+
# @foo.save
|
|
140
|
+
# end
|
|
141
|
+
#
|
|
142
|
+
def without_revision(&block)
|
|
143
|
+
self.class.without_revision(&block)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Turns off optimistic locking for the duration of the block
|
|
147
|
+
#
|
|
148
|
+
# @foo.without_locking do
|
|
149
|
+
# @foo.save
|
|
150
|
+
# end
|
|
151
|
+
#
|
|
152
|
+
def without_locking(&block)
|
|
153
|
+
self.class.without_locking(&block)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def empty_callback() end #:nodoc:
|
|
157
|
+
|
|
158
|
+
def find_newest_version
|
|
159
|
+
return nil if self.id.nil?
|
|
160
|
+
|
|
161
|
+
self.class.versioned_class.where("#{self.class.versioned_foreign_key} = #{self.id}").order("#{self.version_column} DESC").first
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def highest_version
|
|
165
|
+
find_newest_version&.version || -1
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def find_version(version)
|
|
169
|
+
return nil if self.id.nil?
|
|
170
|
+
|
|
171
|
+
ret = self.class.versioned_class.where("#{self.class.versioned_foreign_key} = #{self.id} and #{self.class.version_column}=#{version}").first
|
|
172
|
+
raise "find_version: version #{version} not found in database" unless ret
|
|
173
|
+
ret
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
protected
|
|
177
|
+
# sets the new version before saving
|
|
178
|
+
def set_new_version
|
|
179
|
+
@saving_version = new_record? || save_version?
|
|
180
|
+
self.send("#{self.class.version_column}=", next_version) if new_record? || save_version?
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Gets the next available version for the current record, or 1 for a new record
|
|
184
|
+
def next_version
|
|
185
|
+
(new_record? ? 0 : versions.calculate(:maximum, version_column).to_i) + 1
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
module ClassMethods
|
|
189
|
+
# Returns an array of columns that are versioned. See non_versioned_columns
|
|
190
|
+
def versioned_columns
|
|
191
|
+
@versioned_columns ||= columns.select { |c| !non_versioned_columns.include?(c.name) }
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Returns an instance of the dynamic versioned model
|
|
195
|
+
def versioned_class
|
|
196
|
+
const_get versioned_class_name
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def restore_deleted(id)
|
|
200
|
+
version_record = versioned_class.where("#{versioned_foreign_key} = #{id}").order("#{self.version_column} DESC").first
|
|
201
|
+
version_record.restore
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def restore_deleted_version(id, version)
|
|
205
|
+
version_record = versioned_class.where("#{versioned_foreign_key} = #{id} and #{self.version_column} = #{version}").first
|
|
206
|
+
version_record.restore
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Executes the block with the versioning callbacks disabled.
|
|
210
|
+
#
|
|
211
|
+
# Foo.without_revision do
|
|
212
|
+
# @foo.save
|
|
213
|
+
# end
|
|
214
|
+
#
|
|
215
|
+
def without_revision(&block)
|
|
216
|
+
class_eval do
|
|
217
|
+
CALLBACKS.each do |attr_name|
|
|
218
|
+
alias_method "orig_#{attr_name}".to_sym, attr_name
|
|
219
|
+
alias_method attr_name, :empty_callback
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
block.call
|
|
223
|
+
ensure
|
|
224
|
+
class_eval do
|
|
225
|
+
CALLBACKS.each do |attr_name|
|
|
226
|
+
alias_method attr_name, "orig_#{attr_name}".to_sym
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Turns off optimistic locking for the duration of the block
|
|
232
|
+
#
|
|
233
|
+
# Foo.without_locking do
|
|
234
|
+
# @foo.save
|
|
235
|
+
# end
|
|
236
|
+
#
|
|
237
|
+
def without_locking(&block)
|
|
238
|
+
current = ActiveRecord::Base.lock_optimistically
|
|
239
|
+
ActiveRecord::Base.lock_optimistically = false if current
|
|
240
|
+
begin
|
|
241
|
+
block.call
|
|
242
|
+
ensure
|
|
243
|
+
ActiveRecord::Base.lock_optimistically = true if current
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Rake migration task to create the versioned table using options passed to acts_as_versioned
|
|
248
|
+
def create_versioned_table(create_table_options = {})
|
|
249
|
+
# create version column in main table if it does not exist
|
|
250
|
+
unless self.column_names.find { |c| [version_column.to_s, 'lock_version'].include? c }
|
|
251
|
+
self.connection.add_column table_name, version_column, :integer
|
|
252
|
+
self.reset_column_information
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
return if connection.table_exists?(versioned_table_name)
|
|
256
|
+
|
|
257
|
+
self.connection.create_table(versioned_table_name, **create_table_options) do |t|
|
|
258
|
+
t.column versioned_foreign_key, :integer
|
|
259
|
+
t.column version_column, :integer
|
|
260
|
+
t.column deleted_in_original_table_flag, :boolean, default: false
|
|
261
|
+
t.column record_restored_column, :integer, default: nil
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
self.versioned_columns.each do |col|
|
|
265
|
+
self.connection.add_column versioned_table_name, col.name, col.type,
|
|
266
|
+
limit: col.limit,
|
|
267
|
+
default: col.default,
|
|
268
|
+
scale: col.scale,
|
|
269
|
+
precision: col.precision
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
if type_col = self.columns_hash[inheritance_column]
|
|
273
|
+
self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
|
|
274
|
+
limit: type_col.limit,
|
|
275
|
+
default: type_col.default,
|
|
276
|
+
scale: type_col.scale,
|
|
277
|
+
precision: type_col.precision
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Rake migration task to drop the versioned table
|
|
282
|
+
def drop_versioned_table
|
|
283
|
+
self.connection.drop_table versioned_table_name
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|