Class: Attach::Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/attach/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#binaryObject

Return the binary data for this attachment



68
69
70
71
# File 'lib/attach/attachment.rb', line 68

def binary
  @binary ||= persisted? ? Attach.backend.read(self) : nil
  @binary == :nil ? nil : @binary
end

Class Method Details

.for(role) ⇒ Object

Return the attachment for a given role



63
64
65
# File 'lib/attach/attachment.rb', line 63

def self.for(role)
  self.where(:role => role).first
end

Instance Method Details

#add_child(role, &block) ⇒ Object

Add a child attachment



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/attach/attachment.rb', line 101

def add_child(role, &block)
  attachment = self.children.build
  attachment.role = role
  attachment.owner = self.owner
  attachment.file_name = self.file_name
  attachment.file_type = self.file_type
  attachment.disposition = self.disposition
  attachment.cache_type = self.cache_type
  attachment.cache_max_age = self.cache_max_age
  attachment.type = self.type
  block.call(attachment)
  attachment.save!
end

#child(role) ⇒ Object

Return a child process



89
90
91
92
93
# File 'lib/attach/attachment.rb', line 89

def child(role)
  @cached_children ||= {}
  @cached_children[role.to_sym] ||= self.children.where(:role => role).first || :nil
  @cached_children[role.to_sym] == :nil ? nil : @cached_children[role.to_sym]
end

#image?Boolean

Is the attachment an image?

Returns:

  • (Boolean)


79
80
81
# File 'lib/attach/attachment.rb', line 79

def image?
  file_type =~ /\Aimage\//
end

#processorObject

Return a processor for this attachment



84
85
86
# File 'lib/attach/attachment.rb', line 84

def processor
  @processor ||= Processor.new(self)
end

#try(role) ⇒ Object

Try to return a given otherwise revert to the parent



96
97
98
# File 'lib/attach/attachment.rb', line 96

def try(role)
  child(role) || self
end

#urlObject

Return the path to the attachment



74
75
76
# File 'lib/attach/attachment.rb', line 74

def url
  Attach.backend.url(self)
end