Module: Sequel::Plugins::Attachments::InstanceMethods

Defined in:
lib/cortex_reaver/support/attachments.rb

Instance Method Summary collapse

Instance Method Details

#attachment(name) ⇒ Object

Returns a named attachment



30
31
32
# File 'lib/cortex_reaver/support/attachments.rb', line 30

def attachment(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 attachment_path(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

#attachmentsObject

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 attachments
  # Unsaved new records, naturally, have no attachments.
  return [] if new?

  attachments = Array.new
  if path = local_attachment_path and File.directory? path
    begin
      attachments = Dir.open(path).reject do |name|
        # Don't include dotfiles
        name =~ /^\./
      end
      attachments.collect! do |name|
        Attachment.new self, name
      end
    rescue
      # Couldn't read the directory
    end
  end
  attachments
end

#before_deleteObject

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
  attachments.each do |attachment|
    attachment.delete
  end

  true
end

#create_attachment_directoryObject

Ensures the attachment directory exists.



89
90
91
92
93
94
# File 'lib/cortex_reaver/support/attachments.rb', line 89

def create_attachment_directory
  path = local_attachment_path
  unless File.directory? path
    FileUtils.mkdir_p path, :mode => DEFAULT_ATTACHMENT_DIRECTORY_MODE
  end
end

#local_attachment_pathObject

Returns the local directory which contains attachments for this record.



98
99
100
# File 'lib/cortex_reaver/support/attachments.rb', line 98

def local_attachment_path
  attachment_path :local  
end

#public_attachment_pathObject

Returns the public directory which contains attachments for this record.



104
105
106
# File 'lib/cortex_reaver/support/attachments.rb', line 104

def public_attachment_path
  attachment_path :public
end