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.
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() = self.class.(version, self) # Override #attachments to return the original attachments for the current version. = class << self; self; end .send :define_method, :attachments do 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.
245 246 247 248 249 |
# File 'lib/cms/behaviors/attaching.rb', line 245 def after_build_new_version(new_version) .each do |a| a.attachable_version = new_version.version end end |
#after_publish ⇒ Object
219 220 221 |
# File 'lib/cms/behaviors/attaching.rb', line 219 def after_publish .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 .each do |a| a.revert_to(version_number, {:attachable_version => self.version+1}) end end |
#all_attachments ⇒ Object
234 235 236 |
# File 'lib/cms/behaviors/attaching.rb', line 234 def << end |
#attachable_type ⇒ Object
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
225 226 227 |
# File 'lib/cms/behaviors/attaching.rb', line 225 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.
215 216 217 |
# File 'lib/cms/behaviors/attaching.rb', line 215 def Cms::Attachment.definitions[self.class.name].keys end |
#attachments_were_updated? ⇒ Boolean
205 206 207 208 209 210 211 212 |
# File 'lib/cms/behaviors/attaching.rb', line 205 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)
199 200 201 202 203 |
# File 'lib/cms/behaviors/attaching.rb', line 199 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.
277 278 279 280 281 282 283 284 |
# File 'lib/cms/behaviors/attaching.rb', line 277 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>
287 288 289 |
# File 'lib/cms/behaviors/attaching.rb', line 287 def .select { |a| a.cardinality == Attachment::MULTIPLE } end |
#unassigned_attachments ⇒ Object
229 230 231 232 |
# File 'lib/cms/behaviors/attaching.rb', line 229 def return [] if .blank? Cms::Attachment.find .split(',').map(&:to_i) end |