Class: EspCommons::Image
- Inherits:
-
APISmith::Smash
- Object
- APISmith::Smash
- EspCommons::Image
- Defined in:
- app/models/esp_commons/image.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #build_url ⇒ Object
- #create_thumbnail(options) ⇒ Object
- #height? ⇒ Boolean
- #image? ⇒ Boolean
- #new_height ⇒ Object
- #new_width ⇒ Object
- #parse_url ⇒ Object
- #resize(aspect_ratio) ⇒ Object
- #width? ⇒ Boolean
Instance Attribute Details
#aspect_ratio ⇒ Object
38 39 40 |
# File 'app/models/esp_commons/image.rb', line 38 def aspect_ratio @aspect_ratio ||= width.to_f / height end |
Instance Method Details
#as_json(options = {}) ⇒ Object
74 75 76 |
# File 'app/models/esp_commons/image.rb', line 74 def as_json(={}) super(.merge(:only => %w[url width height description])) end |
#build_url ⇒ Object
20 21 22 23 24 |
# File 'app/models/esp_commons/image.rb', line 20 def build_url self.tap do | image | image.url = "#{Settings['vfs.url']}/files/#{image.id}/#{image.width}-#{image.height}/#{image.filename}" end end |
#create_thumbnail(options) ⇒ Object
67 68 69 70 71 72 |
# File 'app/models/esp_commons/image.rb', line 67 def create_thumbnail() return unless image? self.thumbnail = EspCommons::Image.new(.merge(:id => id, :filename => filename, :description => description)) .resize(aspect_ratio) .build_url end |
#height? ⇒ Boolean
34 35 36 |
# File 'app/models/esp_commons/image.rb', line 34 def height? height && height > 0 end |
#image? ⇒ Boolean
26 27 28 |
# File 'app/models/esp_commons/image.rb', line 26 def image? width && height end |
#new_height ⇒ Object
59 60 61 |
# File 'app/models/esp_commons/image.rb', line 59 def new_height @new_height ||= width / aspect_ratio end |
#new_width ⇒ Object
63 64 65 |
# File 'app/models/esp_commons/image.rb', line 63 def new_width @new_width ||= height * aspect_ratio end |
#parse_url ⇒ Object
14 15 16 17 18 |
# File 'app/models/esp_commons/image.rb', line 14 def parse_url self.tap do | image | image.id, image.width, image.height, image.filename = url.match(%r{files/(\d+)/(\d+)-(\d+)/(.*)})[1..-1] end end |
#resize(aspect_ratio) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/esp_commons/image.rb', line 42 def resize(aspect_ratio) self.aspect_ratio = aspect_ratio if width? && !height? self.height = new_height elsif !width? && height? self.width = new_width else self.width = self.height = 100 if !width? && !height? if height >= new_height self.height = new_height else self.width = new_width end end self end |
#width? ⇒ Boolean
30 31 32 |
# File 'app/models/esp_commons/image.rb', line 30 def width? width && width > 0 end |