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

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

Instance Method Summary collapse

Instance Method Details

#as_of_draft_versionObject



200
201
202
# File 'lib/cms/behaviors/versioning.rb', line 200

def as_of_draft_version
  as_of_version(draft.version)
end

#as_of_version(version) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cms/behaviors/versioning.rb', line 204

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, :created_at, :created_by_id, :updated_at, :updated_by_id]).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)

  # Last but not least, clear the changed attributes
  if changed_attrs = obj.send(:changed_attributes)
    changed_attrs.clear
  end

  obj      
end

#build_new_versionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cms/behaviors/versioning.rb', line 76

def build_new_version
  # First get the values from the draft
  attrs = draft_attributes

  # Now overwrite all values
  (self.class.versioned_columns - %w(version)).each do |col|
    attrs[col] = send(col)
  end

  attrs[:version_comment] = @version_comment || default_version_comment
  @version_comment = nil            
  new_version = versions.build(attrs)
  new_version.version = new_record? ? 1 : (draft.version.to_i + 1)
  after_build_new_version(new_version) if respond_to?(:after_build_new_version)
  new_version
end

#current_versionObject



192
193
194
# File 'lib/cms/behaviors/versioning.rb', line 192

def current_version
  find_version(self.version)
end

#default_version_commentObject



100
101
102
103
104
105
106
# File 'lib/cms/behaviors/versioning.rb', line 100

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

#different_from_last_draft?Boolean

Returns:

  • (Boolean)


259
260
261
262
263
264
265
266
267
# File 'lib/cms/behaviors/versioning.rb', line 259

def different_from_last_draft?
  return true if self.changed?
  last_draft = self.draft
  return true unless last_draft
  (self.class.versioned_columns - %w(version)).each do |col|
    return true if self.send(col) != last_draft.send(col)
  end
  return false
end

#draftObject



176
177
178
# File 'lib/cms/behaviors/versioning.rb', line 176

def draft
  versions.first(:order => "version desc")    
end

#draft_attributesObject



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

def draft_attributes
  # When there is no draft, we'll just copy the attibutes from this object
  # Otherwise we need to use the draft
  d = new_record? ? self : draft
  self.class.versioned_columns.inject({}){|attrs, col| attrs[col] = d.send(col); attrs }
end

#draft_version?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/cms/behaviors/versioning.rb', line 180

def draft_version?
  version == draft.version
end

#find_version(number) ⇒ Object



196
197
198
# File 'lib/cms/behaviors/versioning.rb', line 196

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

#initialize_versionObject



72
73
74
# File 'lib/cms/behaviors/versioning.rb', line 72

def initialize_version
  self.version = 1
end

#live_versionObject



184
185
186
# File 'lib/cms/behaviors/versioning.rb', line 184

def live_version
  find_version(self.class.find(id).version)
end

#live_version?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/cms/behaviors/versioning.rb', line 188

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

#revertObject



229
230
231
232
# File 'lib/cms/behaviors/versioning.rb', line 229

def revert
  draft_version = draft.version
  revert_to(draft_version - 1) unless draft_version == 1
end

#revert_to(version) ⇒ Object



245
246
247
248
# File 'lib/cms/behaviors/versioning.rb', line 245

def revert_to(version)
  revert_to_without_save(version)
  save
end

#revert_to_without_save(version) ⇒ Object



234
235
236
237
238
239
240
241
242
243
# File 'lib/cms/behaviors/versioning.rb', line 234

def revert_to_without_save(version)
  raise "Version parameter missing" if version.blank?
  self.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

#save(perform_validations = true) ⇒ Object



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cms/behaviors/versioning.rb', line 108

def save(perform_validations=true)
  transaction do
    #logger.info "..... Calling valid?"
    return false unless !perform_validations || valid?            
    
    if different_from_last_draft?
      #logger.info "..... Changes => #{changes.inspect}"
    else
      #logger.info "..... No Changes"
      return true
    end
    
    #logger.info "..... Calling before_save"
    return false if callback(:before_save) == false

    if new_record?
      #logger.info "..... Calling before_create"
      return false if callback(:before_create) == false
    else      
      #logger.info "..... Calling before_update"
      return false if callback(:before_update) == false
    end

    #logger.info "..... Calling build_new_version"
    new_version = build_new_version
    #logger.info "..... Is new version valid? #{new_version.valid?}"
    if new_record?
      self.version = 1
      #logger.info "..... Calling create_without_callbacks"
      if result = create_without_callbacks
        #logger.info "..... Calling after_create"
        if callback(:after_create) != false
          #logger.info "..... Calling after_save"
          callback(:after_save)
        end
        
        if @publish_on_save
          publish
          @publish_on_save = nil
        end                
        changed_attributes.clear                                   
      end
      result
    elsif new_version
      #logger.info "..... Calling save"
      if result = new_version.save
        #logger.info "..... Calling after_save"
        if callback(:after_update) != false
          #logger.info "..... Calling after_update"
          callback(:after_save)
        end
        
        if @publish_on_save
          publish
          @publish_on_save = nil
        end 
        changed_attributes.clear
      end
      result
    end
    true
  end
end

#save!(perform_validations = true) ⇒ Object



172
173
174
# File 'lib/cms/behaviors/versioning.rb', line 172

def save!(perform_validations=true)
  save(perform_validations) || raise(ActiveRecord::RecordNotSaved.new(errors.full_messages))
end

#version_commentObject



250
251
252
# File 'lib/cms/behaviors/versioning.rb', line 250

def version_comment
  @version_comment
end

#version_comment=(version_comment) ⇒ Object



254
255
256
257
# File 'lib/cms/behaviors/versioning.rb', line 254

def version_comment=(version_comment)
  @version_comment = version_comment
  send(:changed_attributes)["version_comment"] = @version_comment
end