Module: Sequel::Plugins::Attachments::InstanceMethods
- Defined in:
- lib/cortex_reaver/support/attachments.rb
Instance Method Summary collapse
-
#attachment(name) ⇒ Object
Returns a named attachment.
-
#attachment_path(type = :local, force_save = true) ⇒ Object
Returns the directory which contains attachments for this record.
-
#attachments ⇒ Object
Returns an array of attachments.
-
#before_delete ⇒ Object
When we delete a record with attachments, delete the attachments first.
-
#create_attachment_directory ⇒ Object
Ensures the attachment directory exists.
-
#local_attachment_path ⇒ Object
Returns the local directory which contains attachments for this record.
-
#public_attachment_path ⇒ Object
Returns the public directory which contains attachments for this record.
Instance Method Details
#attachment(name) ⇒ Object
Returns a named attachment
30 31 32 |
# File 'lib/cortex_reaver/support/attachments.rb', line 30 def (name) Attachment.new(self, name) end |
#attachment_path(type = :local, force_save = true) ⇒ Object
Returns the directory which contains attachments for this record. Forces a save of the record if an ID does not exist.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cortex_reaver/support/attachments.rb', line 36 def (type = :local, force_save = true) sep = '' case type when :local # We're interested in a local path on disk. sep = File::SEPARATOR path = CortexReaver.config.public_root.dup when :public # We're interested in a public (e.g. HTTP URL) path. sep = PUBLIC_PATH_SEPARATOR path = '' else raise ArgumentError.new('type must be either :local or :public') end # If we don't have an ID, save the record to obtain one. if force_save and id.nil? unless save # Save failed! return nil end end # Complete the path. path << sep + 'data' + sep + self.class.to_s.demodulize.underscore.pluralize + sep + self.id.to_s end |
#attachments ⇒ Object
Returns an array of attachments.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cortex_reaver/support/attachments.rb', line 67 def # Unsaved new records, naturally, have no attachments. return [] if new? = Array.new if path = and File.directory? path begin = Dir.open(path).reject do |name| # Don't include dotfiles name =~ /^\./ end .collect! do |name| Attachment.new self, name end rescue # Couldn't read the directory end end end |
#before_delete ⇒ Object
When we delete a record with attachments, delete the attachments first.
20 21 22 23 24 25 26 27 |
# File 'lib/cortex_reaver/support/attachments.rb', line 20 def before_delete return false if super == false .each do || .delete end true end |
#create_attachment_directory ⇒ Object
Ensures the attachment directory exists.
89 90 91 92 93 94 |
# File 'lib/cortex_reaver/support/attachments.rb', line 89 def path = unless File.directory? path FileUtils.mkdir_p path, :mode => DEFAULT_ATTACHMENT_DIRECTORY_MODE end end |
#local_attachment_path ⇒ Object
Returns the local directory which contains attachments for this record.
98 99 100 |
# File 'lib/cortex_reaver/support/attachments.rb', line 98 def :local end |
#public_attachment_path ⇒ Object
Returns the public directory which contains attachments for this record.
104 105 106 |
# File 'lib/cortex_reaver/support/attachments.rb', line 104 def :public end |