Module: EchoUploads::Model::ClassMethods
- Defined in:
- lib/echo_uploads/model.rb
Instance Method Summary collapse
-
#echo_upload(attr, options = {}) ⇒ Object
Options: -
key: A Proc that takes an ActionDispatch::UploadedFile and returns a key uniquely identifying the file.
Instance Method Details
#echo_upload(attr, options = {}) ⇒ Object
Options:
-
key: A Proc that takes an ActionDispatch::UploadedFile and returns a key uniquely identifying the file. If this option is not specified, the key is computed as the SHA-512 hash of the file contents. A digest of the file’s contents should always be at least a part of the key. -
expires: Length of time temporary files will be persisted. Defaults to1.day. -
storage: A class that persists uploaded files to disk, to the cloud, or to wherever else you want. Defaults toRails.configuration.echo_uploads.storage, which in turn isEchoUploads::FilesystemStoreby default. -
map: A Proc that accepts an ActionDispatch::Htttp::UploadedFile and an instance ofEchoUploads::Mapper. It should transform the file data (e.g. scaling an image). It should then write the transformed data to one of more temporary files. To get the temporary file path(s), call#writeon theMapper. See readme.md for an example. The:mapoption can also accept a symbol naming an an instance method that works the same way as the previously described Proc. -
multiple: You use the:mapoption to write multiple versions of the file. E.g. multiple thumbnail sizes. If you do so, you must pass multiple: true. This will make the association withEchoUploads::Fileahas_manyinstead of ahas_one. The first file you write in the map function becomes the default. E.g.: Your model is calledWidget, and the upload file attribute is calledphoto. You pass:mapwith a method that writes three files. If you call Widget#photo_path, it will return the path to the first of the three files.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/echo_uploads/model.rb', line 93 def echo_upload(attr, = {}) = { expires: 1.day, storage: Rails.configuration.echo_uploads.storage, key: ::EchoUploads::File.default_key_proc }.merge() # Init the config object. We can't use [] syntax to set the hash key because # class_attribute expects you to call the setter method every time the # attribute value changes. (Merely calling [] would just mutate the referenced # object, and wouldn't invoke the setter.) self.echo_uploads_config ||= {} self.echo_uploads_config = echo_uploads_config.merge attr => {} # Define reader method for the file attribute. attr_reader attr # Define the writer method for the file attribute. define_method("#{attr}=") do |file| if [:map] mapper = ::EchoUploads::Mapper.new file if [:map].is_a? Proc [:map].call file, mapper else send([:map], file, mapper) end # Write an array of ActionDispatch::Http::UploadedFile objects to the instance # variable. send "mapped_#{attr}=", mapper.outputs end instance_variable_set "@#{attr}", file end # Define the accessor methods for the mapped version(s) of the file. Returns # an array. attr_accessor "mapped_#{attr}" # Define the original filename method. define_method("#{attr}_original_filename") do (attr, , &:original_filename) end # Define the path method. This method will raise if the given storage # class doesn't support the #path method. define_method("#{attr}_path") do (attr, ) do || .path end end # Define the MIME type method. define_method("#{attr}_mime") do (attr, , &:mime_type) end alias_method "#{attr}_mime_type", "#{attr}_mime" # Define the key method define_method("#{attr}_key") do (attr, , &:key) end # Define the has_x? method. Returns true if a permanent or temporary file has been # persisted, or if a file (which may not be valid) has been uploaded this request # cycle. define_method("has_#{attr}?") do # Does this record have a permanent file? send("has_prm_#{attr}?") or # Did the submitted form "remember" a previously saved metadata record? send("has_tmp_#{attr}?") or # Has a new file been uploaded in this request cycle? send(attr).present? end # Define the has_prm_x? method. Returns true if the permanent metadata record # exists and has its owner set to this object. define_method("has_prm_#{attr}?") do send("#{attr}_metadata").present? and send("#{attr}_metadata").persisted? end # Define the has_tmp_x? method. Returns true if the record "remembers" # a a temporary metadata record. (Typically because validation errors caused # the form to be redisplayed.) define_method("has_tmp_#{attr}?") do send("#{attr}_tmp_metadata").present? end # Define the read_x method. Delegates to the #read method of the store (e.g. # FilesystemStore). define_method("read_#{attr}") do (attr, , &:read) end # Define the association with the metadata model. if [:multiple] has_many("#{attr}_metadatas".to_sym, ->() { where(owner_attr: attr) }, as: :owner, dependent: :destroy, class_name: '::EchoUploads::File' ) alias_method attr.to_s.pluralize, "#{attr}_metadatas" define_method("#{attr}_metadata") do send("#{attr}_metadatas").first end define_method("#{attr}_metadata=") do |val| send("#{attr}_metadatas") << val end else has_one("#{attr}_metadata".to_sym, ->() { where(owner_attr: attr) }, as: :owner, dependent: :destroy, class_name: '::EchoUploads::File' ) end # Define the temp attribute for the metadata model. attr_accessor "#{attr}_tmp_metadata" configure_temp_file_saving attr, configure_perm_file_saving attr, end |