Module: RetinaRails::Strategies::Paperclip::Uploader::ClassMethods
- Defined in:
- lib/retina_rails/strategies/paperclip.rb
Instance Method Summary collapse
-
#has_attached_file(name, options = {}) ⇒ Object
Define an attachment with its options.
-
#optimize_retina!(name) ⇒ Object
Optimize attachment for retina displays.
Instance Method Details
#has_attached_file(name, options = {}) ⇒ Object
Define an attachment with its options
Parameters
- name (#to_sym)
-
name of the version
- options (Hash)
-
optional options hash
Examples
class Upload
retina!
has_attached_file :image,
:styles => {
:original => ["800x800", :jpg],
:big => ["125x125#", :jpg]
},
:retina => true # Or
:retina => { :quality => 25 } # Optional
end
51 52 53 54 55 |
# File 'lib/retina_rails/strategies/paperclip.rb', line 51 def has_attached_file(name, ={}) super optimize_retina! name unless [:retina] == false end |
#optimize_retina!(name) ⇒ Object
Optimize attachment for retina displays
Parameters
- name (Sym)
-
name of the attachment
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/retina_rails/strategies/paperclip.rb', line 64 def optimize_retina!(name) = [name] ## Check for style definitions styles = [:styles] = if [:retina].is_a?(Hash) [:retina] else { :quality => 60 } end ## Get retina quality retina_quality = [:quality] || 40 if styles retina_styles = {} = {} = [:convert_options] ## Iterate over styles and set optimzed dimensions styles.each_pair do |key, value| dimensions = value.kind_of?(Array) ? value[0] : value new_dimensions = dimensions.gsub(/\d+/) { |dimension| dimension.to_i * 2} retina_styles[key.to_sym] = value.kind_of?(Array) ? [new_dimensions, value[1]] : new_dimensions ## Set quality convert option convert_option = [key] if convert_option = convert_option ? "#{convert_option} -quality #{retina_quality}" : "-quality #{retina_quality}" [key.to_sym] = convert_option end ## Override styles with new retina dimensions [:styles].merge!(retina_styles) ## Set quality convert options [:convert_options] = {} if [:convert_options].nil? [:convert_options].merge!() ## Set save dimensions processor if [:processors] [:processors] << :save_dimensions [:processors] << :thumbnail else [:processors] = [:thumbnail, :save_dimensions] end end end |