Class: Attach::Attachment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Attach::Attachment
- Defined in:
- lib/attach/attachment.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
writeonly
Sets the attribute backend.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_child(role, &block) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #blob ⇒ Object
- #blob=(blob) ⇒ Object
- #child(role) ⇒ Object
-
#copy_attributes_from_file(file) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #image? ⇒ Boolean
- #processor ⇒ Object
- #try(role) ⇒ Object
- #url ⇒ Object
Instance Attribute Details
#backend=(value) ⇒ Object
Sets the attribute backend
16 17 18 |
# File 'lib/attach/attachment.rb', line 16 def backend=(value) @backend = value end |
Class Method Details
.for(role) ⇒ Object
166 167 168 |
# File 'lib/attach/attachment.rb', line 166 def for(role) where(role: role).first end |
Instance Method Details
#add_child(role, &block) ⇒ Object
rubocop:disable Metrics/AbcSize
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/attach/attachment.rb', line 82 def add_child(role, &block) = children.build .role = role .owner = owner .file_name = file_name .file_type = file_type .disposition = disposition .cache_type = cache_type .cache_max_age = cache_max_age .serve = serve .type = type block.call() .save! end |
#blob ⇒ Object
41 42 43 44 45 46 |
# File 'lib/attach/attachment.rb', line 41 def blob return @blob if instance_variable_defined?('@blob') return nil unless persisted? @blob = backend.read(self) end |
#blob=(blob) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/attach/attachment.rb', line 48 def blob=(blob) unless blob.nil? || blob.is_a?(BlobTypes::File) || blob.is_a?(BlobTypes::Raw) raise ArgumentError, 'Only nil or a File/Raw blob type can be set as a blob for an attachment' end @blob = blob end |
#child(role) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/attach/attachment.rb', line 68 def child(role) @cached_children ||= {} if @cached_children.key?(role.to_sym) return @cached_children[role.to_sym] end @cached_children[role.to_sym] = children.where(role: role).first end |
#copy_attributes_from_file(file) ⇒ Object
rubocop:disable Metrics/AbcSize
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/attach/attachment.rb', line 99 def copy_attributes_from_file(file) case file.class.name when 'ActionDispatch::Http::UploadedFile' self.blob = BlobTypes::File.new(file.tempfile) self.file_name = file.original_filename self.file_type = file.content_type when 'Attach::File' self.blob = BlobTypes::Raw.new(file.data) self.file_name = file.name self.file_type = file.type else self.blob = BlobTypes::Raw.new(file) self.file_name = 'untitled' self.file_type = 'application/octet-stream' end end |
#image? ⇒ Boolean
60 61 62 |
# File 'lib/attach/attachment.rb', line 60 def image? file_type =~ /\Aimage\// end |
#processor ⇒ Object
64 65 66 |
# File 'lib/attach/attachment.rb', line 64 def processor @processor ||= Processor.new(self) end |
#try(role) ⇒ Object
77 78 79 |
# File 'lib/attach/attachment.rb', line 77 def try(role) child(role) || self end |
#url ⇒ Object
56 57 58 |
# File 'lib/attach/attachment.rb', line 56 def url backend.url(self) end |