Module: WithoutScope::ActsAsRevisable::Revision

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

Overview

This module is mixed into the revision classes.

Callbacks

  • before_restore is called on the revision class before it is restored as the current record.

  • after_restore is called on the revision class after it is restored as the current record.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/acts_as_revisable/acts/revision.rb', line 12

def self.included(base) #:nodoc:
  base.send(:extend, ClassMethods)
  
  class << base
    attr_accessor :revisable_revisable_class, :revisable_cloned_associations
  end
  
  base.instance_eval do
    set_table_name(revisable_class.table_name)
    default_scope :conditions => {:revisable_is_current => false}
            
    define_callbacks :before_restore, :after_restore
    before_create :revision_setup
    after_create :grab_my_branches
    
    named_scope :deleted, :conditions => ["? is not null", :revisable_deleted_at]
    
    [:current_revision, revisable_association_name.to_sym].each do |a|
      belongs_to a, :class_name => revisable_class_name, :foreign_key => :revisable_original_id
    end
    
    [[:ancestors, "<"], [:descendants, ">"]].each do |a|
      # Jumping through hoops here to try and make sure the
      # :finder_sql is cross-database compatible. :finder_sql
      # in a plugin is evil but, I see no other option.
      has_many a.first, :class_name => revision_class_name, :finder_sql => "select * from #{quoted_table_name} where #{quote_bound_value(:revisable_original_id)} = \#{revisable_original_id} and #{quote_bound_value(:revisable_number)} #{a.last} \#{revisable_number} and #{quote_bound_value(:revisable_is_current)} = #{quote_value(false)} order by #{quote_bound_value(:revisable_number)} #{(a.last.eql?("<") ? "DESC" : "ASC")}"
    end
  end
end

Instance Method Details

#find_revision(*args) ⇒ Object



42
43
44
# File 'lib/acts_as_revisable/acts/revision.rb', line 42

def find_revision(*args)
  current_revision.find_revision(*args)
end

#from_revisableObject



81
82
83
# File 'lib/acts_as_revisable/acts/revision.rb', line 81

def from_revisable
  current_revision.for_revision
end

#grab_my_branchesObject



77
78
79
# File 'lib/acts_as_revisable/acts/revision.rb', line 77

def grab_my_branches
  self.class.revisable_class.update_all(["revisable_branched_from_id = ?", self[:id]], ["revisable_branched_from_id = ?", self[:revisable_original_id]])
end

#next_revisionObject

Return the revision after this one.



52
53
54
# File 'lib/acts_as_revisable/acts/revision.rb', line 52

def next_revision
  self.class.find(:first, :conditions => {:revisable_original_id => revisable_original_id, :revisable_number => revisable_number + 1})
end

#previous_revisionObject

Return the revision prior to this one.



47
48
49
# File 'lib/acts_as_revisable/acts/revision.rb', line 47

def previous_revision
  self.class.find(:first, :conditions => {:revisable_original_id => revisable_original_id, :revisable_number => revisable_number - 1})
end

#reverting_fromObject



85
86
87
# File 'lib/acts_as_revisable/acts/revision.rb', line 85

def reverting_from
  from_revisable[:reverting_from]
end

#reverting_from=(val) ⇒ Object



89
90
91
# File 'lib/acts_as_revisable/acts/revision.rb', line 89

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

#reverting_toObject



93
94
95
# File 'lib/acts_as_revisable/acts/revision.rb', line 93

def reverting_to
  from_revisable[:reverting_to]
end

#reverting_to=(val) ⇒ Object



97
98
99
# File 'lib/acts_as_revisable/acts/revision.rb', line 97

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

#revision_nameObject

Accessor for revisable_name just to make external API more pleasant.



62
63
64
# File 'lib/acts_as_revisable/acts/revision.rb', line 62

def revision_name #:nodoc:
  self[:revisable_name]
end

#revision_name=(val) ⇒ Object

Setter for revisable_name just to make external API more pleasant.



57
58
59
# File 'lib/acts_as_revisable/acts/revision.rb', line 57

def revision_name=(val) #:nodoc:
  self[:revisable_name] = val
end

#revision_setupObject

Sets some initial values for a new revision.



67
68
69
70
71
72
73
74
75
# File 'lib/acts_as_revisable/acts/revision.rb', line 67

def revision_setup #:nodoc:
  now = Time.current
  prev = current_revision.revisions.first
  prev.update_attribute(:revisable_revised_at, now) if prev
  self[:revisable_current_at] = now + 1.second
  self[:revisable_is_current] = false
  self[:revisable_branched_from_id] = current_revision[:revisable_branched_from_id]
  self[:revisable_type] = current_revision[:type] || current_revision.class.name
end