junebug-wiki 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,509 @@
1
+ # Copyright (c) 2005 Rick Olson
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.
21
+
22
+ module ActiveRecord #:nodoc:
23
+ module Acts #:nodoc:
24
+ # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a
25
+ # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version
26
+ # column is present as well.
27
+ #
28
+ # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart
29
+ # your container for the changes to be reflected. In development mode this usually means restarting WEBrick.
30
+ #
31
+ # class Page < ActiveRecord::Base
32
+ # # assumes pages_versions table
33
+ # acts_as_versioned
34
+ # end
35
+ #
36
+ # Example:
37
+ #
38
+ # page = Page.create(:title => 'hello world!')
39
+ # page.version # => 1
40
+ #
41
+ # page.title = 'hello world'
42
+ # page.save
43
+ # page.version # => 2
44
+ # page.versions.size # => 2
45
+ #
46
+ # page.revert_to(1) # using version number
47
+ # page.title # => 'hello world!'
48
+ #
49
+ # page.revert_to(page.versions.last) # using versioned instance
50
+ # page.title # => 'hello world'
51
+ #
52
+ # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
53
+ module Versioned
54
+ CALLBACKS = [:set_new_version, :save_version_on_create, :save_version, :clear_changed_attributes]
55
+ def self.included(base) # :nodoc:
56
+ base.extend ClassMethods
57
+ end
58
+
59
+ module ClassMethods
60
+ # == Configuration options
61
+ #
62
+ # * <tt>class_name</tt> - versioned model class name (default: PageVersion in the above example)
63
+ # * <tt>table_name</tt> - versioned model table name (default: page_versions in the above example)
64
+ # * <tt>foreign_key</tt> - foreign key used to relate the versioned model to the original model (default: page_id in the above example)
65
+ # * <tt>inheritance_column</tt> - name of the column to save the model's inheritance_column value for STI. (default: versioned_type)
66
+ # * <tt>version_column</tt> - name of the column in the model that keeps the version number (default: version)
67
+ # * <tt>sequence_name</tt> - name of the custom sequence to be used by the versioned model.
68
+ # * <tt>limit</tt> - number of revisions to keep, defaults to unlimited
69
+ # * <tt>if</tt> - symbol of method to check before saving a new version. If this method returns false, a new version is not saved.
70
+ # For finer control, pass either a Proc or modify Model#version_condition_met?
71
+ #
72
+ # acts_as_versioned :if => Proc.new { |auction| !auction.expired? }
73
+ #
74
+ # or...
75
+ #
76
+ # class Auction
77
+ # def version_condition_met? # totally bypasses the <tt>:if</tt> option
78
+ # !expired?
79
+ # end
80
+ # end
81
+ #
82
+ # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes
83
+ # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have.
84
+ # Use this instead if you want to write your own attribute setters (and ignore if_changed):
85
+ #
86
+ # def name=(new_name)
87
+ # write_changed_attribute :name, new_name
88
+ # end
89
+ #
90
+ # * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block
91
+ # to create an anonymous mixin:
92
+ #
93
+ # class Auction
94
+ # acts_as_versioned do
95
+ # def started?
96
+ # !started_at.nil?
97
+ # end
98
+ # end
99
+ # end
100
+ #
101
+ # or...
102
+ #
103
+ # module AuctionExtension
104
+ # def started?
105
+ # !started_at.nil?
106
+ # end
107
+ # end
108
+ # class Auction
109
+ # acts_as_versioned :extend => AuctionExtension
110
+ # end
111
+ #
112
+ # Example code:
113
+ #
114
+ # @auction = Auction.find(1)
115
+ # @auction.started?
116
+ # @auction.versions.first.started?
117
+ #
118
+ # == Database Schema
119
+ #
120
+ # The model that you're versioning needs to have a 'version' attribute. The model is versioned
121
+ # into a table called #{model}_versions where the model name is singlular. The _versions table should
122
+ # contain all the fields you want versioned, the same version column, and a #{model}_id foreign key field.
123
+ #
124
+ # A lock_version field is also accepted if your model uses Optimistic Locking. If your table uses Single Table inheritance,
125
+ # then that field is reflected in the versioned model as 'versioned_type' by default.
126
+ #
127
+ # Acts_as_versioned comes prepared with the ActiveRecord::Acts::Versioned::ActMethods::ClassMethods#create_versioned_table
128
+ # method, perfect for a migration. It will also create the version column if the main model does not already have it.
129
+ #
130
+ # class AddVersions < ActiveRecord::Migration
131
+ # def self.up
132
+ # # create_versioned_table takes the same options hash
133
+ # # that create_table does
134
+ # Post.create_versioned_table
135
+ # end
136
+ #
137
+ # def self.down
138
+ # Post.drop_versioned_table
139
+ # end
140
+ # end
141
+ #
142
+ # == Changing What Fields Are Versioned
143
+ #
144
+ # By default, acts_as_versioned will version all but these fields:
145
+ #
146
+ # [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
147
+ #
148
+ # You can add or change those by modifying #non_versioned_columns. Note that this takes strings and not symbols.
149
+ #
150
+ # class Post < ActiveRecord::Base
151
+ # acts_as_versioned
152
+ # self.non_versioned_columns << 'comments_count'
153
+ # end
154
+ #
155
+ def acts_as_versioned(options = {}, &extension)
156
+ # don't allow multiple calls
157
+ return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
158
+
159
+ send :include, ActiveRecord::Acts::Versioned::ActMethods
160
+
161
+ cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
162
+ :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
163
+ :version_association_options
164
+
165
+ # legacy
166
+ alias_method :non_versioned_fields, :non_versioned_columns
167
+ alias_method :non_versioned_fields=, :non_versioned_columns=
168
+
169
+ class << self
170
+ alias_method :non_versioned_fields, :non_versioned_columns
171
+ alias_method :non_versioned_fields=, :non_versioned_columns=
172
+ end
173
+
174
+ send :attr_accessor, :changed_attributes
175
+
176
+ self.versioned_class_name = options[:class_name] || "Version"
177
+ self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
178
+ self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
179
+ self.versioned_inheritance_column = options[:inheritance_column] || "versioned_#{inheritance_column}"
180
+ self.version_column = options[:version_column] || 'version'
181
+ self.version_sequence_name = options[:sequence_name]
182
+ self.max_version_limit = options[:limit].to_i
183
+ self.version_condition = options[:if] || true
184
+ self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
185
+ self.version_association_options = {
186
+ :class_name => "#{self.to_s}::#{versioned_class_name}",
187
+ :foreign_key => "#{versioned_foreign_key}",
188
+ :order => 'version',
189
+ :dependent => :delete_all
190
+ }.merge(options[:association_options] || {})
191
+
192
+ if block_given?
193
+ extension_module_name = "#{versioned_class_name}Extension"
194
+ silence_warnings do
195
+ self.const_set(extension_module_name, Module.new(&extension))
196
+ end
197
+
198
+ options[:extend] = self.const_get(extension_module_name)
199
+ end
200
+
201
+ class_eval do
202
+ has_many :versions, version_association_options
203
+ before_save :set_new_version
204
+ after_create :save_version_on_create
205
+ after_update :save_version
206
+ after_save :clear_old_versions
207
+ after_save :clear_changed_attributes
208
+
209
+ unless options[:if_changed].nil?
210
+ self.track_changed_attributes = true
211
+ options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
212
+ options[:if_changed].each do |attr_name|
213
+ define_method("#{attr_name}=") do |value|
214
+ write_changed_attribute attr_name, value
215
+ end
216
+ end
217
+ end
218
+
219
+ include options[:extend] if options[:extend].is_a?(Module)
220
+ end
221
+
222
+ # create the dynamic versioned model
223
+ const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
224
+ def self.reloadable? ; false ; end
225
+ end
226
+
227
+ versioned_class.set_table_name versioned_table_name
228
+ versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
229
+ :class_name => "::#{self.to_s}",
230
+ :foreign_key => versioned_foreign_key
231
+ versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module)
232
+ versioned_class.set_sequence_name version_sequence_name if version_sequence_name
233
+ end
234
+ end
235
+
236
+ module ActMethods
237
+ def self.included(base) # :nodoc:
238
+ base.extend ClassMethods
239
+ end
240
+
241
+ # Saves a version of the model if applicable
242
+ def save_version
243
+ save_version_on_create if save_version?
244
+ end
245
+
246
+ # Saves a version of the model in the versioned table. This is called in the after_save callback by default
247
+ def save_version_on_create
248
+ rev = self.class.versioned_class.new
249
+ self.clone_versioned_model(self, rev)
250
+ rev.version = send(self.class.version_column)
251
+ rev.send("#{self.class.versioned_foreign_key}=", self.id)
252
+ rev.save
253
+ end
254
+
255
+ # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
256
+ # Override this method to set your own criteria for clearing old versions.
257
+ def clear_old_versions
258
+ return if self.class.max_version_limit == 0
259
+ excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
260
+ if excess_baggage > 0
261
+ sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
262
+ self.class.versioned_class.connection.execute sql
263
+ end
264
+ end
265
+
266
+ # Finds a specific version of this model.
267
+ def find_version(version)
268
+ return version if version.is_a?(self.class.versioned_class)
269
+ return nil if version.is_a?(ActiveRecord::Base)
270
+ find_versions(:conditions => ['version = ?', version], :limit => 1).first
271
+ end
272
+
273
+ # Finds versions of this model. Takes an options hash like <tt>find</tt>
274
+ def find_versions(options = {})
275
+ versions.find(:all, options)
276
+ end
277
+
278
+ # Reverts a model to a given version. Takes either a version number or an instance of the versioned model
279
+ def revert_to(version)
280
+ if version.is_a?(self.class.versioned_class)
281
+ return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
282
+ else
283
+ return false unless version = find_version(version)
284
+ end
285
+ self.clone_versioned_model(version, self)
286
+ self.send("#{self.class.version_column}=", version.version)
287
+ true
288
+ end
289
+
290
+ # Reverts a model to a given version and saves the model.
291
+ # Takes either a version number or an instance of the versioned model
292
+ def revert_to!(version)
293
+ revert_to(version) ? save_without_revision : false
294
+ end
295
+
296
+ # Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
297
+ def save_without_revision
298
+ save_without_revision!
299
+ true
300
+ rescue
301
+ false
302
+ end
303
+
304
+ def save_without_revision!
305
+ without_locking do
306
+ without_revision do
307
+ save!
308
+ end
309
+ end
310
+ end
311
+
312
+ # Returns an array of attribute keys that are versioned. See non_versioned_columns
313
+ def versioned_attributes
314
+ self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
315
+ end
316
+
317
+ # If called with no parameters, gets whether the current model has changed and needs to be versioned.
318
+ # If called with a single parameter, gets whether the parameter has changed.
319
+ def changed?(attr_name = nil)
320
+ attr_name.nil? ?
321
+ (!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) :
322
+ (changed_attributes && changed_attributes.include?(attr_name.to_s))
323
+ end
324
+
325
+ # keep old dirty? method
326
+ alias_method :dirty?, :changed?
327
+
328
+ # Clones a model. Used when saving a new version or reverting a model's version.
329
+ def clone_versioned_model(orig_model, new_model)
330
+ self.versioned_attributes.each do |key|
331
+ new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.has_attribute?(key)
332
+ end
333
+
334
+ if orig_model.is_a?(self.class.versioned_class)
335
+ new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
336
+ elsif new_model.is_a?(self.class.versioned_class)
337
+ new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
338
+ end
339
+ end
340
+
341
+ # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
342
+ def save_version?
343
+ version_condition_met? && changed?
344
+ end
345
+
346
+ # Checks condition set in the :if option to check whether a revision should be created or not. Override this for
347
+ # custom version condition checking.
348
+ def version_condition_met?
349
+ case
350
+ when version_condition.is_a?(Symbol)
351
+ send(version_condition)
352
+ when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
353
+ version_condition.call(self)
354
+ else
355
+ version_condition
356
+ end
357
+ end
358
+
359
+ # Executes the block with the versioning callbacks disabled.
360
+ #
361
+ # @foo.without_revision do
362
+ # @foo.save
363
+ # end
364
+ #
365
+ def without_revision(&block)
366
+ self.class.without_revision(&block)
367
+ end
368
+
369
+ # Turns off optimistic locking for the duration of the block
370
+ #
371
+ # @foo.without_locking do
372
+ # @foo.save
373
+ # end
374
+ #
375
+ def without_locking(&block)
376
+ self.class.without_locking(&block)
377
+ end
378
+
379
+ protected
380
+ # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
381
+ def set_new_version
382
+ self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?)
383
+ end
384
+
385
+ # Gets the next available version for the current record, or 1 for a new record
386
+ def next_version
387
+ return 1 if new_record?
388
+ (versions.calculate(:max, :version) || 0) + 1
389
+ end
390
+
391
+ # clears current changed attributes. Called after save.
392
+ def clear_changed_attributes
393
+ self.changed_attributes = []
394
+ end
395
+
396
+ def write_changed_attribute(attr_name, attr_value)
397
+ (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) or self.send(attr_name) == attr_value
398
+ write_attribute(attr_name.to_s, attr_value)
399
+ end
400
+
401
+ private
402
+ CALLBACKS.each do |attr_name|
403
+ alias_method "orig_#{attr_name}".to_sym, attr_name
404
+ end
405
+
406
+ def empty_callback() end #:nodoc:
407
+
408
+ module ClassMethods
409
+ # Finds a specific version of a specific row of this model
410
+ def find_version(id, version)
411
+ find_versions(id,
412
+ :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version],
413
+ :limit => 1).first
414
+ end
415
+
416
+ # Finds versions of a specific model. Takes an options hash like <tt>find</tt>
417
+ def find_versions(id, options = {})
418
+ versioned_class.find :all, {
419
+ :conditions => ["#{versioned_foreign_key} = ?", id],
420
+ :order => 'version' }.merge(options)
421
+ end
422
+
423
+ # Returns an array of columns that are versioned. See non_versioned_columns
424
+ def versioned_columns
425
+ self.columns.select { |c| !non_versioned_columns.include?(c.name) }
426
+ end
427
+
428
+ # Returns an instance of the dynamic versioned model
429
+ def versioned_class
430
+ const_get versioned_class_name
431
+ end
432
+
433
+ # Rake migration task to create the versioned table using options passed to acts_as_versioned
434
+ def create_versioned_table(create_table_options = {})
435
+ # create version column in main table if it does not exist
436
+ if !self.content_columns.find { |c| %w(version lock_version).include? c.name }
437
+ self.connection.add_column table_name, :version, :integer
438
+ end
439
+
440
+ self.connection.create_table(versioned_table_name, create_table_options) do |t|
441
+ t.column versioned_foreign_key, :integer
442
+ t.column :version, :integer
443
+ end
444
+
445
+ updated_col = nil
446
+ self.versioned_columns.each do |col|
447
+ updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
448
+ self.connection.add_column versioned_table_name, col.name, col.type,
449
+ :limit => col.limit,
450
+ :default => col.default
451
+ end
452
+
453
+ if type_col = self.columns_hash[inheritance_column]
454
+ self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type,
455
+ :limit => type_col.limit,
456
+ :default => type_col.default
457
+ end
458
+
459
+ if updated_col.nil?
460
+ self.connection.add_column versioned_table_name, :updated_at, :timestamp
461
+ end
462
+ end
463
+
464
+ # Rake migration task to drop the versioned table
465
+ def drop_versioned_table
466
+ self.connection.drop_table versioned_table_name
467
+ end
468
+
469
+ # Executes the block with the versioning callbacks disabled.
470
+ #
471
+ # Foo.without_revision do
472
+ # @foo.save
473
+ # end
474
+ #
475
+ def without_revision(&block)
476
+ class_eval do
477
+ CALLBACKS.each do |attr_name|
478
+ alias_method attr_name, :empty_callback
479
+ end
480
+ end
481
+ result = block.call
482
+ class_eval do
483
+ CALLBACKS.each do |attr_name|
484
+ alias_method attr_name, "orig_#{attr_name}".to_sym
485
+ end
486
+ end
487
+ result
488
+ end
489
+
490
+ # Turns off optimistic locking for the duration of the block
491
+ #
492
+ # Foo.without_locking do
493
+ # @foo.save
494
+ # end
495
+ #
496
+ def without_locking(&block)
497
+ current = ActiveRecord::Base.lock_optimistically
498
+ ActiveRecord::Base.lock_optimistically = false if current
499
+ result = block.call
500
+ ActiveRecord::Base.lock_optimistically = true if current
501
+ result
502
+ end
503
+ end
504
+ end
505
+ end
506
+ end
507
+ end
508
+
509
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned