Module: FatJam::ActsAsRevisable::Revisable

Defined in:
lib/acts_as_revisable/acts/revisable.rb

Overview

This module is mixed into the revision classes.

Callbacks

  • before_revise is called before the record is revised.

  • after_revise is called after the record is revised.

  • before_revert is called before the record is reverted.

  • after_revert is called after the record is reverted.

  • before_changeset is called before a changeset block is called.

  • after_changeset is called after a changeset block is called.

  • after_branch_created is called on the new revisable instance created by branching after it’s been created.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/acts_as_revisable/acts/revisable.rb', line 17

def self.included(base) #:nodoc:
  base.send(:extend, ClassMethods)
          
  class << base
    alias_method_chain :find, :revisable
    alias_method_chain :with_scope, :revisable
    attr_accessor :revisable_revision_class, :revisable_columns
  end
  
  base.class_inheritable_hash :revisable_shared_objects
  base.revisable_shared_objects = {}
  
  base.instance_eval do
    attr_accessor :revisable_new_params, :revisable_revision
    
    define_callbacks :before_revise, :after_revise, :before_revert, :after_revert, :before_changeset, :after_changeset, :after_branch_created
    
    alias_method_chain :save, :revisable
    alias_method_chain :save!, :revisable
    
    before_create :before_revisable_create
    before_update :before_revisable_update
    after_update :after_revisable_update
    after_save :clear_revisable_shared_objects!, :unless => :is_reverting?
    
    acts_as_scoped_model :find => {:conditions => {:revisable_is_current => true}}
    
    [:revisions, revisions_association_name.to_sym].each do |assoc|
      has_many assoc, (revisable_options.revision_association_options || {}).merge({:class_name => revision_class_name, :foreign_key => :revisable_original_id, :order => "#{quoted_table_name}.#{connection.quote_column_name(:revisable_number)} DESC", :dependent => :destroy})
    end
  end
end

Instance Method Details

#after_revisable_updateObject

Checks if an initialized revision_class has been stored in the accessor. If it has been, this instance is saved.



317
318
319
320
321
322
323
324
325
# File 'lib/acts_as_revisable/acts/revisable.rb', line 317

def after_revisable_update #:nodoc:
  if self.revisable_revision          
    self.revisable_revision.save
    revisions.reload
    run_callbacks(:after_revise)
  end
  force_revision!(false)
  true
end

#before_revisable_createObject

Set some defaults for a newly created Revisable instance.



291
292
293
294
# File 'lib/acts_as_revisable/acts/revisable.rb', line 291

def before_revisable_create #:nodoc:
  self[:revisable_is_current] = true
  self[:revisable_number] = 1
end

#before_revisable_updateObject

Checks whether or not a revision should be stored. If it should be, it initialized the revision_class and stores it in an accessor for later saving.



308
309
310
311
312
313
# File 'lib/acts_as_revisable/acts/revisable.rb', line 308

def before_revisable_update #:nodoc:
  return unless should_revise?
  return false unless run_callbacks(:before_revise) { |r, o| r == false}
  
  self.revisable_revision = self.to_revision
end

#changeset(&block) ⇒ Object

Groups statements that could trigger several revisions into a single revision. The revision is created once #save is called.

Example

@project.revision_number # => 1
@project.changeset do |project|
  # each one of the following statements would 
  # normally trigger a revision
  project.update_attribute(:name, "new name")
  project.revise!
  project.revise!
end
@project.save
@project.revision_number # => 2

Callbacks

  • before_changeset is called before a changeset block is called.

  • after_changeset is called after a changeset block is called.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/acts_as_revisable/acts/revisable.rb', line 233

def changeset(&block)
  return unless block_given?
  
  return yield(self) if in_revision?
  
  unless run_callbacks(:before_changeset) { |r, o| r == false}
    raise ActiveRecord::RecordNotSaved
  end
  
  begin
    force_revision!
    in_revision!
  
    returning(yield(self)) do
      run_callbacks(:after_changeset)
    end
  ensure
    in_revision!(false)       
  end
end

#changeset!(&block) ⇒ Object

Same as changeset except it also saves the record.



255
256
257
258
259
260
# File 'lib/acts_as_revisable/acts/revisable.rb', line 255

def changeset!(&block)
  changeset do
    block.call(self)
    save!
  end
end

#clear_revisable_shared_objects!Object



388
389
390
391
# File 'lib/acts_as_revisable/acts/revisable.rb', line 388

def clear_revisable_shared_objects!
  key = self.read_attribute(self.class.primary_key)
  self.class.revisable_shared_objects.delete(key)
end

#current_revisionObject

This returns



363
364
365
# File 'lib/acts_as_revisable/acts/revisable.rb', line 363

def current_revision
  self
end

#find_revision(by) ⇒ Object

Finds a specific revision of self.

The by parameter can be a revision_class instance, the symbols :first, :previous or :last, a Time instance or an Integer.

When passed a revision_class instance, this method simply returns it. This is used primarily by revert_to!.

When passed :first it returns the first revision created.

When passed :previous or :last it returns the last revision created.

When passed a Time instance it returns the revision that was the current record at the given time.

When passed an Integer it returns the revision with that revision_number.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/acts_as_revisable/acts/revisable.rb', line 69

def find_revision(by)
  by = Integer(by) if by.is_a?(String) && by.match(/[0-9]+/)
    
  case by
  when self.class
    by
  when self.class.revision_class
    by
  when :first
    revisions.last
  when :previous
    revisions.first
  when Time
    revisions.find(:first, :conditions => ["? >= ? and ? <= ?", :revisable_revised_at, by, :revisable_current_at, by])
  when self[:revisable_number]
    self
  else
    revisions.find_by_revisable_number(by)
  end
end

#for_revisionObject



367
368
369
370
# File 'lib/acts_as_revisable/acts/revisable.rb', line 367

def for_revision
  key = self.read_attribute(self.class.primary_key)
  self.class.revisable_shared_objects[key] ||= {}
end

#force_revision!(val = true) ⇒ Object

Sets whether or not to force a revision.



174
175
176
# File 'lib/acts_as_revisable/acts/revisable.rb', line 174

def force_revision!(val=true) #:nodoc:
  set_revisable_state(:force_revision, val)
end

#force_revision?Boolean

Returns true if a revision should be forced.

Returns:

  • (Boolean)


179
180
181
# File 'lib/acts_as_revisable/acts/revisable.rb', line 179

def force_revision? #:nodoc:
  get_revisable_state(:force_revision) || false
end

#in_revision!(val = true) ⇒ Object

Manages the internal state of a Revisable controlling whether or not a record is being revised. This works across instances and is keyed on primary_key.



336
337
338
# File 'lib/acts_as_revisable/acts/revisable.rb', line 336

def in_revision!(val=true) #:nodoc:
  set_revisable_state(:revision, val)
end

#in_revision?Boolean

Returns true if the record (not just this instance of the record) is currently being revised.

Returns:

  • (Boolean)


329
330
331
# File 'lib/acts_as_revisable/acts/revisable.rb', line 329

def in_revision?
  get_revisable_state(:revision)
end

#is_reverting!(val = true) ⇒ Object

Globally sets the reverting state of this record.



163
164
165
# File 'lib/acts_as_revisable/acts/revisable.rb', line 163

def is_reverting!(val=true) #:nodoc:
  set_revisable_state(:reverting, val)
end

#is_reverting?Boolean

Returns true if the record (not just this instance of the record) is currently being reverted.

Returns:

  • (Boolean)


169
170
171
# File 'lib/acts_as_revisable/acts/revisable.rb', line 169

def is_reverting?
  get_revisable_state(:reverting) || false
end

#no_revision!(val = true) ⇒ Object

Sets whether or not a revision should be created.



184
185
186
# File 'lib/acts_as_revisable/acts/revisable.rb', line 184

def no_revision!(val=true) #:nodoc:
  set_revisable_state(:no_revision, val)
end

#no_revision?Boolean

Returns true if no revision should be created.

Returns:

  • (Boolean)


189
190
191
# File 'lib/acts_as_revisable/acts/revisable.rb', line 189

def no_revision? #:nodoc:
  get_revisable_state(:no_revision) || false
end

#revert_to(what, *args, &block) ⇒ Object

Returns a revisable_class instance initialized with the record found using find_revision.

The what parameter is simply passed to find_revision and the returned record forms the basis of the reverted record.

Callbacks

  • before_revert is called before the record is reverted.

  • after_revert is called after the record is reverted.

If :without_revision => true has not been passed the following callbacks are also called:

  • before_revise is called before the record is revised.

  • after_revise is called after the record is revised.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/acts_as_revisable/acts/revisable.rb', line 106

def revert_to(what, *args, &block) #:yields:
  is_reverting!
  
  unless run_callbacks(:before_revert) { |r, o| r == false}
    raise ActiveRecord::RecordNotSaved
  end

  options = args.extract_options!
    
  rev = find_revision(what)
  self.reverting_to, self.reverting_from = rev, self
  
  unless rev.run_callbacks(:before_restore) { |r, o| r == false}
    raise ActiveRecord::RecordNotSaved
  end
    
  self.class.column_names.each do |col|
    next unless self.class.revisable_should_clone_column? col
    self[col] = rev[col]
  end
    
  self.no_revision! if options.delete :without_revision
  self.revisable_new_params = options
  
  yield(self) if block_given?
  rev.run_callbacks(:after_restore)
  run_callbacks(:after_revert)
  self
ensure
  is_reverting!(false)
  clear_revisable_shared_objects!
end

#revert_to!(what, *args) ⇒ Object

Same as revert_to except it also saves the record.



140
141
142
143
144
# File 'lib/acts_as_revisable/acts/revisable.rb', line 140

def revert_to!(what, *args)
  revert_to(what, *args) do
    self.no_revision? ? save! : revise!
  end
end

#revert_to_without_revision(*args) ⇒ Object

Equivalent to:

revert_to(:without_revision => true)


148
149
150
151
152
# File 'lib/acts_as_revisable/acts/revisable.rb', line 148

def revert_to_without_revision(*args)
  options = args.extract_options!
  options.update({:without_revision => true})
  revert_to(*(args << options))
end

#revert_to_without_revision!(*args) ⇒ Object

Equivalent to:

revert_to!(:without_revision => true)


156
157
158
159
160
# File 'lib/acts_as_revisable/acts/revisable.rb', line 156

def revert_to_without_revision!(*args)
  options = args.extract_options!
  options.update({:without_revision => true})
  revert_to!(*(args << options))
end

#reverting_fromObject



380
381
382
# File 'lib/acts_as_revisable/acts/revisable.rb', line 380

def reverting_from
  for_revision[:reverting_from]
end

#reverting_from=(val) ⇒ Object



384
385
386
# File 'lib/acts_as_revisable/acts/revisable.rb', line 384

def reverting_from=(val)
  for_revision[:reverting_from] = val
end

#reverting_toObject



372
373
374
# File 'lib/acts_as_revisable/acts/revisable.rb', line 372

def reverting_to
  for_revision[:reverting_to]
end

#reverting_to=(val) ⇒ Object



376
377
378
# File 'lib/acts_as_revisable/acts/revisable.rb', line 376

def reverting_to=(val)
  for_revision[:reverting_to] = val
end

#revise!Object

Force an immediate revision whether or not any columns have been modified.

Callbacks

  • before_revise is called before the record is revised.

  • after_revise is called after the record is revised.



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/acts_as_revisable/acts/revisable.rb', line 200

def revise!
  return if in_revision?
  
  begin
    force_revision!
    in_revision!
    save!
  ensure
    in_revision!(false)
    force_revision!(false)
  end
end

#save_with_revisable(*args) ⇒ Object

acts_as_revisable’s override for ActiveRecord::Base’s #save



284
285
286
287
288
# File 'lib/acts_as_revisable/acts/revisable.rb', line 284

def save_with_revisable(*args) #:nodoc:
  self.revisable_new_params ||= args.extract_options!
  self.no_revision! if self.revisable_new_params.delete :without_revision
  save_without_revisable(args)
end

#save_with_revisable!(*args) ⇒ Object

acts_as_revisable’s override for ActiveRecord::Base’s #save!



277
278
279
280
281
# File 'lib/acts_as_revisable/acts/revisable.rb', line 277

def save_with_revisable!(*args) #:nodoc:
  self.revisable_new_params ||= args.extract_options!
  self.no_revision! if self.revisable_new_params.delete :without_revision
  save_without_revisable!
end

#should_revise?Boolean

Checks whether or not a Revisable should be revised.

Returns:

  • (Boolean)


297
298
299
300
301
302
303
# File 'lib/acts_as_revisable/acts/revisable.rb', line 297

def should_revise? #:nodoc:
  return false if new_record?
  return true if force_revision?
  return false if no_revision?
  return false unless self.changed?
  !(self.changed.map(&:downcase) & self.class.revisable_watch_columns).blank?
end

#to_revisionObject

This returns a new Revision instance with all the appropriate values initialized.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/acts_as_revisable/acts/revisable.rb', line 342

def to_revision #:nodoc:
  rev = self.class.revision_class.new(self.revisable_new_params)

  rev.revisable_original_id = self.id
  
  new_revision_number = revisions.maximum(:revisable_number) + 1 rescue self.revisable_number
  rev.revisable_number = new_revision_number
  self.revisable_number = new_revision_number + 1
  
  self.class.column_names.each do |col|
    next unless self.class.revisable_should_clone_column? col
    val = self.send("#{col}_changed?") ? self.send("#{col}_was") : self.send(col)
    rev.send("#{col}=", val)
  end

  self.revisable_new_params = nil

  rev
end

#without_revisions!Object



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/acts_as_revisable/acts/revisable.rb', line 262

def without_revisions!
  return if in_revision? || !block_given?
  
  begin
    no_revision!
    in_revision!
    yield
    save!
  ensure
    in_revision!(false)
    no_revision!(false)
  end
end