Module: Cms::Behaviors::Versioning::InstanceMethods

Defined in:
lib/cms/behaviors/versioning.rb

Instance Method Summary collapse

Instance Method Details

#as_of_version(version) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cms/behaviors/versioning.rb', line 130

def as_of_version(version)
  v = find_version(version)
  raise ActiveRecord::RecordNotFound.new("version #{version.inspect} does not exist for <#{self.class}:#{id}>") unless v
  obj = self.class.new

  (self.class.versioned_columns + [:version, :updated_at]).each do |a|
    obj.send("#{a}=", v.send(a))
  end
  obj.id = id
  obj.lock_version = lock_version
  
  #Need to do this so associations can be loaded
  obj.instance_variable_set("@new_record", false)

  #Callback to allow us to load other data when an older version is loaded
  obj.after_as_of_version if obj.respond_to?(:after_as_of_version)

  obj      
end

#build_new_versionObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cms/behaviors/versioning.rb', line 72

def build_new_version
  if build_new_version?
    increment_version            
    attrs = versioned_attributes
    attrs[:version_comment] = @version_comment || default_version_comment
    @version_comment = nil            
    new_version = versions.build(attrs)

    after_build_new_version(new_version) if respond_to?(:after_build_new_version)
  end          
  true
end

#build_new_version?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/cms/behaviors/versioning.rb', line 85

def build_new_version?
  !!(new_record? || !@version_comment.blank? || self.class.versioned_columns.detect{|c| __send__ "#{c}_changed?" })
end

#current_versionObject



109
110
111
# File 'lib/cms/behaviors/versioning.rb', line 109

def current_version
  find_version(version)
end

#current_version?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/cms/behaviors/versioning.rb', line 105

def current_version?
  self.class.find(id).version == version
end

#default_version_commentObject



97
98
99
100
101
102
103
# File 'lib/cms/behaviors/versioning.rb', line 97

def default_version_comment
  if new_record?
    "Created"
  else
    "Changed #{(changes.keys - %w[version created_by_id updated_by_id]).sort.join(', ')}"
  end
end

#find_version(number) ⇒ Object



113
114
115
# File 'lib/cms/behaviors/versioning.rb', line 113

def find_version(number)
  versions.first(:conditions => { :version => number })
end

#increment_versionObject



89
90
91
# File 'lib/cms/behaviors/versioning.rb', line 89

def increment_version
  self.version = version.to_i + 1
end

#live_versionObject



117
118
119
120
121
122
123
124
# File 'lib/cms/behaviors/versioning.rb', line 117

def live_version
  if self.class.publishable?    
    live_version = versions.first(:conditions => {:published => true}, :order => "version desc, id desc")
    live_version ? as_of_version(live_version.version) : nil
  else
    self
  end
end

#live_version?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/cms/behaviors/versioning.rb', line 126

def live_version?
  live_version && version == live_version.version
end

#revertObject



150
151
152
# File 'lib/cms/behaviors/versioning.rb', line 150

def revert
  revert_to(version - 1) unless version == 1
end

#revert_to(version) ⇒ Object



165
166
167
168
# File 'lib/cms/behaviors/versioning.rb', line 165

def revert_to(version)
  revert_to_without_save(version)
  save
end

#revert_to_without_save(version) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/cms/behaviors/versioning.rb', line 154

def revert_to_without_save(version)
  raise "Version parameter missing" if version.blank?
  revert_to_version = find_version(version)
  raise "Could not find version #{version}" unless revert_to_version
  (self.class.versioned_columns - ["version"]).each do |a|
    send("#{a}=", revert_to_version.send(a))
  end  
  self.version_comment = "Reverted to version #{version}"
  self            
end

#versioned_attributesObject



93
94
95
# File 'lib/cms/behaviors/versioning.rb', line 93

def versioned_attributes
  self.class.versioned_columns.inject({}){|attrs, col| attrs[col] = send(col); attrs }
end