Class: EspCommons::Image

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Attributes, ActiveAttr::MassAssignment, ActiveAttr::QueryAttributes, ActiveAttr::TypecastedAttributes
Defined in:
app/models/esp_commons/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aspect_ratioObject



44
45
46
# File 'app/models/esp_commons/image.rb', line 44

def aspect_ratio
  @aspect_ratio ||= width.to_f / height
end

Instance Method Details

#as_json(options = {}) ⇒ Object



82
83
84
# File 'app/models/esp_commons/image.rb', line 82

def as_json(options={})
  attributes.as_json(options.merge(:only => %w[url width height description]))
end

#build_urlObject



30
31
32
33
34
35
36
37
38
# File 'app/models/esp_commons/image.rb', line 30

def build_url
  self.tap do | image |
    if image?
      image.url = "#{vfs_url}/files/#{image.id}/#{image.width}-#{image.height}#{image.crop ? '!' : ''}/#{image.filename}"
    else
      image.url = "#{vfs_url}/files/#{image.id}/#{image.filename}"
    end
  end
end

#create_thumbnail(options) ⇒ Object



75
76
77
78
79
80
# File 'app/models/esp_commons/image.rb', line 75

def create_thumbnail(options)
  return unless image?
  self.thumbnail = EspCommons::Image.new(options.reverse_merge(:vfs_url => vfs_url,  :id => id, :filename => filename, :description => description))
                                    .resize(aspect_ratio)
                                    .build_url
end

#image?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/esp_commons/image.rb', line 40

def image?
  width? && height?
end

#new_heightObject



67
68
69
# File 'app/models/esp_commons/image.rb', line 67

def new_height
  @new_height ||= width / aspect_ratio
end

#new_widthObject



71
72
73
# File 'app/models/esp_commons/image.rb', line 71

def new_width
  @new_width ||= height * aspect_ratio
end

#parse_urlObject



23
24
25
26
27
28
# File 'app/models/esp_commons/image.rb', line 23

def parse_url
  self.tap do | image |
    image.vfs_url, image.id, image.width, image.height, image.crop, image.filename =
      url.match(%r{(.*)/files/(\d+)/(?:(\d+)-(\d+)(\!)?/)?(.*)})[1..-1]
  end if url.present?
end

#resize(aspect_ratio) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/esp_commons/image.rb', line 48

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?
    unless crop
      if height >= new_height
        self.height = new_height
      else
        self.width = new_width
      end
    end
  end
  self
end