Class: Imgix::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/imgix/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, instance, options = {}) ⇒ Attachment

Returns a new instance of Attachment.



5
6
7
8
9
# File 'lib/imgix/attachment.rb', line 5

def initialize(name, instance, options = {})
  @name     = name
  @instance = instance
  @options  = options
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



3
4
5
# File 'lib/imgix/attachment.rb', line 3

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/imgix/attachment.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/imgix/attachment.rb', line 3

def options
  @options
end

#stylesObject (readonly)

Retrieve all styles provided for this attachment



77
78
79
# File 'lib/imgix/attachment.rb', line 77

def styles
  @styles
end

Class Method Details

.url(filename, style) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/imgix/attachment.rb', line 20

def self.url(filename, style)
  # url including source previx
  full_url = [@instance.base_url, @options[:prefix], filename].compact.join("/")

  # add the style options and return
  styled_filename(full_url, style)
end

Instance Method Details

#content_typeObject

Retrieve the content type from the instance provided



51
52
53
# File 'lib/imgix/attachment.rb', line 51

def content_type
  @instance.send(self.content_type_field)
end

#content_type_fieldObject

Determine the field/column name to find the content_type in



34
35
36
# File 'lib/imgix/attachment.rb', line 34

def content_type_field
  @options.fetch(:content_type_field, "content_type")
end

#file_sizeObject

Retrieve the file_size from the instance provided



56
57
58
# File 'lib/imgix/attachment.rb', line 56

def file_size
  @instance.send(self.file_size_field)
end

#file_size_fieldObject

Determine the field/column name to find the file size in



39
40
41
# File 'lib/imgix/attachment.rb', line 39

def file_size_field
  @options.fetch(:file_size_field, "file_size")
end

#filenameObject

Retrieve the filename from the instance provided



44
45
46
47
48
# File 'lib/imgix/attachment.rb', line 44

def filename
  # attempt to get the value from the database, but fall back
  # to a default image supplied
  @instance.send(self.filename_field) || @options[:default]
end

#filename_fieldObject

Determine the field/column name to find the filename in



29
30
31
# File 'lib/imgix/attachment.rb', line 29

def filename_field
  @options.fetch(:filename_field, "filename")
end

#style(style = nil) ⇒ Object

Retrieve a particular style of this attachment



71
72
73
74
# File 'lib/imgix/attachment.rb', line 71

def style(style = nil)
  # look for the provided style, return an empty has by default
  self.styles.fetch(style.to_sym, {})
end

#styled_filename(url, style) ⇒ Object

Append query parameters to the filename of this attachment



61
62
63
64
65
66
67
68
# File 'lib/imgix/attachment.rb', line 61

def styled_filename(url, style)
  if style.present? && self.styles.include?(style.to_sym)
    return [url, self.style(style).to_s].join("?")
  end

  # default to the basic url, without any options
  return url
end

#url(style = nil) ⇒ Object

generate the full imgix url for the provided style



12
13
14
15
16
17
18
# File 'lib/imgix/attachment.rb', line 12

def url(style = nil)
  # construct the pieces of the url
  full_url = [@instance.base_url, @options[:prefix], filename].compact.join("/")

  # append imgix params to the filename before returning
  styled_filename(full_url, style)
end