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.



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cms/behaviors/attaching.rb', line 253

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)


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

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

#after_publishObject



219
220
221
# File 'lib/cms/behaviors/attaching.rb', line 219

def after_publish
  attachments.each &:publish
end

#after_revert(version) ⇒ Object

Callback - Ensure attachments get reverted whenver a block does.



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

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



234
235
236
# File 'lib/cms/behaviors/attaching.rb', line 234

def all_attachments
  attachments << unassigned_attachments
end

#attachable_typeObject



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

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



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

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



215
216
217
# File 'lib/cms/behaviors/attaching.rb', line 215

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

#attachments_were_updated?Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
# File 'lib/cms/behaviors/attaching.rb', line 205

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)



199
200
201
202
203
# File 'lib/cms/behaviors/attaching.rb', line 199

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.


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

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:



287
288
289
# File 'lib/cms/behaviors/attaching.rb', line 287

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

#unassigned_attachmentsObject



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

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