Class: Udongo::Assets::Resizer
- Inherits:
-
Object
- Object
- Udongo::Assets::Resizer
- Defined in:
- lib/udongo/assets/resizer.rb
Instance Method Summary collapse
- #actual_path(calculated_filename) ⇒ Object
- #actual_url(calculated_filename) ⇒ Object
- #filename(width = nil, height = nil, options = {}) ⇒ Object
-
#initialize(asset) ⇒ Resizer
constructor
A new instance of Resizer.
- #path(width = nil, height = nil, options = {}) ⇒ Object
- #url(width = nil, height = nil, options = {}) ⇒ Object
Constructor Details
#initialize(asset) ⇒ Resizer
Returns a new instance of Resizer.
6 7 8 |
# File 'lib/udongo/assets/resizer.rb', line 6 def initialize(asset) @asset = asset end |
Instance Method Details
#actual_path(calculated_filename) ⇒ Object
54 55 56 |
# File 'lib/udongo/assets/resizer.rb', line 54 def actual_path(calculated_filename) "#{Rails.root}/public/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{calculated_filename}" end |
#actual_url(calculated_filename) ⇒ Object
45 46 47 |
# File 'lib/udongo/assets/resizer.rb', line 45 def actual_url(calculated_filename) "/uploads/assets/_cache/#{main_dir}/#{second_dir}/#{calculated_filename}" end |
#filename(width = nil, height = nil, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/udongo/assets/resizer.rb', line 10 def filename(width = nil, height = nil, = {}) action = .key?(:action) ? [:action] : :resize_to_limit quality = [:quality] gravity = [:gravity].to_s.underscore.split('_').map { |s| s[0,1] }.join background = [:background].to_s.parameterize str = action.to_s.split('_').last str << "-q#{quality}" if quality.present? str << "-g#{gravity}" if gravity.present? str << "-b#{background}" if background.present? str << "-#{width}x#{height}-#{@asset.actual_filename}" end |
#path(width = nil, height = nil, options = {}) ⇒ Object
49 50 51 52 |
# File 'lib/udongo/assets/resizer.rb', line 49 def path(width = nil, height = nil, = {}) url(width, height, ) # Trigger the actual resize (if needed) actual_path(filename(width, height, )) end |
#url(width = nil, height = nil, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/udongo/assets/resizer.rb', line 23 def url(width = nil, height = nil, = {}) [:action] = :resize_to_limit unless .key?(:action) if width.nil? && height.nil? return "/uploads/assets/#{main_dir}/#{second_dir}/#{@asset.actual_filename}" end name = filename(width, height, ) unless File.exists?(actual_path(name)) FileUtils.mkpath(File.dirname(actual_path(name))) unless resize_action_allowed? [:action] raise "No such resize action '#{[:action].to_s}'. Available are: resize_to_limit, resize_to_fit, resize_to_fill and resize_and_pad." end trigger_resize(width, height, ) end actual_url(name) end |