Module: Cms::Behaviors::Attaching::InstanceMethods
- Defined in:
- lib/cms/behaviors/attaching.rb
Instance Method Summary collapse
-
#after_as_of_version ⇒ Object
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.
-
#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.
- #after_publish ⇒ Object
-
#after_revert(version) ⇒ Object
Callback - Ensure attachments get reverted whenver a block does.
- #all_attachments ⇒ Object
- #attachable_type ⇒ Object
-
#attachment_named(name) ⇒ Object
Locates the attachment with a given name.
-
#attachment_names ⇒ Array<String>
Returns a list of all attachments this content type has defined.
- #attachments_were_updated? ⇒ Boolean
-
#check_for_updated_attachments ⇒ Object
This ensures that if a change is made to an attachment, that this model is also marked as changed.
-
#ensure_attachment_exists ⇒ Object
Ensures that attachments exist for form, since it uses attachments.each to iterate over them.
- #multiple_attachments ⇒ Array<Cms::Attachment>
- #unassigned_attachments ⇒ Object
Instance Method Details
#after_as_of_version ⇒ Object
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.(version, self) # Override #attachments to return the original attachments for the current version. = class << self; self; end .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.
269 270 271 272 273 |
# File 'lib/cms/behaviors/attaching.rb', line 269 def after_build_new_version(new_version) .each do |a| a.attachable_version = new_version.version end end |
#after_publish ⇒ Object
243 244 245 |
# File 'lib/cms/behaviors/attaching.rb', line 243 def after_publish .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 .each do |a| a.revert_to(version_number, {:attachable_version => self.version+1}) end end |
#all_attachments ⇒ Object
258 259 260 |
# File 'lib/cms/behaviors/attaching.rb', line 258 def << end |
#attachable_type ⇒ Object
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
249 250 251 |
# File 'lib/cms/behaviors/attaching.rb', line 249 def (name) .select { |item| item..to_sym == name }.first end |
#attachment_names ⇒ Array<String>
Returns a list of all attachments this content type has defined.
239 240 241 |
# File 'lib/cms/behaviors/attaching.rb', line 239 def Cms::Attachment.definitions[self.class.name].keys end |
#attachments_were_updated? ⇒ Boolean
229 230 231 232 233 234 235 236 |
# File 'lib/cms/behaviors/attaching.rb', line 229 def .each do |a| if a.changed? return true end end false end |
#check_for_updated_attachments ⇒ Object
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 if == "true" || changed_attributes['attachments'] = "Uploaded new files" end end |
#ensure_attachment_exists ⇒ Object
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 .each do |n| unless (n.to_sym) # Can't use attachments.build because sometimes its an array << Attachment.new(:attachment_name => n, :attachable => self) end end end |
#multiple_attachments ⇒ Array<Cms::Attachment>
311 312 313 |
# File 'lib/cms/behaviors/attaching.rb', line 311 def .select { |a| a.cardinality == Attachment::MULTIPLE } end |
#unassigned_attachments ⇒ Object
253 254 255 256 |
# File 'lib/cms/behaviors/attaching.rb', line 253 def return [] if .blank? Cms::Attachment.find .split(',').map(&:to_i) end |