Class: Attach::Attachment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Attach::Attachment
- Defined in:
- lib/attach/attachment.rb
Instance Attribute Summary collapse
-
#binary ⇒ Object
Return the binary data for this attachment.
Class Method Summary collapse
-
.for(role) ⇒ Object
Return the attachment for a given role.
Instance Method Summary collapse
-
#add_child(role, &block) ⇒ Object
Add a child attachment.
-
#child(role) ⇒ Object
Return a child process.
-
#image? ⇒ Boolean
Is the attachment an image?.
-
#processor ⇒ Object
Return a processor for this attachment.
-
#try(role) ⇒ Object
Try to return a given otherwise revert to the parent.
-
#url ⇒ Object
Return the path to the attachment.
Instance Attribute Details
#binary ⇒ Object
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) = self.children.build .role = role .owner = self.owner .file_name = self.file_name .file_type = self.file_type .disposition = self.disposition .cache_type = self.cache_type .cache_max_age = self.cache_max_age .type = self.type block.call() .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?
79 80 81 |
# File 'lib/attach/attachment.rb', line 79 def image? file_type =~ /\Aimage\// end |
#processor ⇒ Object
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 |
#url ⇒ Object
Return the path to the attachment
74 75 76 |
# File 'lib/attach/attachment.rb', line 74 def url Attach.backend.url(self) end |