Class: LoyalCore::AssetUploader
- Inherits:
-
CarrierWave::Uploader::Base
- Object
- CarrierWave::Uploader::Base
- LoyalCore::AssetUploader
- Defined in:
- app/uploaders/loyal_core/asset_uploader.rb
Direct Known Subclasses
Constant Summary collapse
- LOYAL_CORE_UPLOADER_IMAGE_EXT =
%w[gif jpg jpeg png bmp]
- LOYAL_CORE_UPLOADER_FLASH_EXT =
%w[swf flv]
- LOYAL_CORE_UPLOADER_MEDIA_EXT =
%w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
- LOYAL_CORE_UPLOADER_FILE_EXT =
%w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
Instance Method Summary collapse
-
#default_url(*args) ⇒ Object
Provide a default URL as a default if there hasn’t been a file uploaded: def default_url # For Rails 3.1+ asset pipeline compatibility: # asset_path(“fallback/” + [version_name, “default.png”].compact.join(‘_’)).
-
#filename ⇒ Object
Override the filename of the uploaded files: Avoid using model.id or version_name here, see uploader/store.rb for details.
- #model_id_partition ⇒ Object
-
#store_dir ⇒ Object
Override the directory where uploaded files will be stored.
- #url(*args) ⇒ Object
Instance Method Details
#default_url(*args) ⇒ Object
Provide a default URL as a default if there hasn’t been a file uploaded: def default_url
# For Rails 3.1+ asset pipeline compatibility:
# asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/uploaders/loyal_core/asset_uploader.rb', line 48 def default_url *args if (version = args.first) && version.respond_to?(:to_sym) raise ArgumentError, "Version #{version} doesn't exist!" if versions[version.to_sym].nil? # recursively proxy to version versions[version.to_sym].default_url(*args[1..-1]) else ::ActionController::Base.helpers.asset_path( "loyal_core/fallbacks/#{impl_model_name_underscore}/#{mounted_as}/" + [version_name, "missing.gif"].compact.join('_') ) end # For Rails 3.1+ asset pipeline compatibility: # "/images/fallbacks/#{impl_model_name_underscore}/#{mounted_as}/" + ["missing.gif", version_name].compact.join('_') # "/images/fallback/" + [version_name, "default.png"].compact.join('_') end |
#filename ⇒ Object
Override the filename of the uploaded files: Avoid using model.id or version_name here, see uploader/store.rb for details. def filename
"something.jpg" if original_filename
end
103 104 105 106 107 |
# File 'app/uploaders/loyal_core/asset_uploader.rb', line 103 def filename if file "#{Digest::MD5.hexdigest(impl_model_hash_data)}.#{file.extension}" end end |
#model_id_partition ⇒ Object
36 37 38 |
# File 'app/uploaders/loyal_core/asset_uploader.rb', line 36 def model_id_partition impl_integer_partition(model.id) end |
#store_dir ⇒ Object
Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted: def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
31 32 33 34 |
# File 'app/uploaders/loyal_core/asset_uploader.rb', line 31 def store_dir # "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model_id_partition}" "uploads/first/#{impl_model_name_underscore}/#{mounted_as}/#{impl_integer_partition(model.created_at.to_i)}" end |
#url(*args) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/uploaders/loyal_core/asset_uploader.rb', line 65 def url *args stored_file_name = model.send(mounted_as) if stored_file_name.present? _url = super _url = "#{_url}?#{impl_generate_stmap_version}" unless _url.include?('?') _url else default_url *args end end |