Module: Cms::Behaviors::Attaching::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#after_as_of_versionObject

Version Callback - Reconstruct this object exactly as it was as of a particularly version Called after the object is ‘reset’ to the specific version in question.



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/cms/behaviors/attaching.rb', line 277

def after_as_of_version()
  @attachments_as_of = self.class.attachments_as_of_version(version, self)


  # Override #attachments to return the original attachments for the current version.
  metaclass = class << self;
    self;
  end
  metaclass.send :define_method, :attachments do
    @attachments_as_of
  end
end

#after_build_new_version(new_version) ⇒ Object

Versioning Callback - This will result in a new version of attachments being created every time the attachable is updated.

Allows a complete version history to be reconstructed.

Parameters:

  • new_version (Versionable)


269
270
271
272
273
# File 'lib/cms/behaviors/attaching.rb', line 269

def after_build_new_version(new_version)
  attachments.each do |a|
    a.attachable_version = new_version.version
  end
end

#after_publishObject



243
244
245
# File 'lib/cms/behaviors/attaching.rb', line 243

def after_publish
  attachments.each &:publish
end

#after_revert(version) ⇒ Object

Callback - Ensure attachments get reverted whenver a block does.



291
292
293
294
295
296
# File 'lib/cms/behaviors/attaching.rb', line 291

def after_revert(version)
  version_number = version.version
  attachments.each do |a|
    a.revert_to(version_number, {:attachable_version => self.version+1})
  end
end

#all_attachmentsObject



258
259
260
# File 'lib/cms/behaviors/attaching.rb', line 258

def all_attachments
  attachments << unassigned_attachments
end

#attachable_typeObject



262
263
264
# File 'lib/cms/behaviors/attaching.rb', line 262

def attachable_type
  self.class.name
end

#attachment_named(name) ⇒ Object

Locates the attachment with a given name

Parameters:

  • name (Symbol)

    The name of the attachment



249
250
251
# File 'lib/cms/behaviors/attaching.rb', line 249

def attachment_named(name)
  attachments.select { |item| item.attachment_name.to_sym == name }.first
end

#attachment_namesArray<String>

Returns a list of all attachments this content type has defined.

Returns:

  • (Array<String>)

    Names



239
240
241
# File 'lib/cms/behaviors/attaching.rb', line 239

def attachment_names
  Cms::Attachment.definitions[self.class.name].keys
end

#attachments_were_updated?Boolean

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
# File 'lib/cms/behaviors/attaching.rb', line 229

def attachments_were_updated?
  attachments.each do |a|
    if a.changed?
      return true
    end
  end
  false
end

#check_for_updated_attachmentsObject

This ensures that if a change is made to an attachment, that this model is also marked as changed. Otherwise, if the change isn’t detected, this record won’t save a new version (since updates are rejected if no changes were made)



223
224
225
226
227
# File 'lib/cms/behaviors/attaching.rb', line 223

def check_for_updated_attachments
  if attachments_changed == "true" || attachments_were_updated?
    changed_attributes['attachments'] = "Uploaded new files"
  end
end

#ensure_attachment_existsObject

Ensures that attachments exist for form, since it uses attachments.each to iterate over them.

Design Qualm: I don't like that this method has to exist, since its basically obscuring the fact that
individual attachments don't exist when an object is created.


301
302
303
304
305
306
307
308
# File 'lib/cms/behaviors/attaching.rb', line 301

def ensure_attachment_exists
  attachment_names.each do |n|
    unless attachment_named(n.to_sym)
      # Can't use attachments.build because sometimes its an array
      attachments << Attachment.new(:attachment_name => n, :attachable => self)
    end
  end
end

#multiple_attachmentsArray<Cms::Attachment>

Returns:



311
312
313
# File 'lib/cms/behaviors/attaching.rb', line 311

def multiple_attachments
  attachments.select { |a| a.cardinality == Attachment::MULTIPLE }
end

#unassigned_attachmentsObject



253
254
255
256
# File 'lib/cms/behaviors/attaching.rb', line 253

def unassigned_attachments
  return [] if attachment_id_list.blank?
  Cms::Attachment.find attachment_id_list.split(',').map(&:to_i)
end