Module: Attached::ClassMethods

Defined in:
lib/attached.rb

Instance Method Summary collapse

Instance Method Details

#has_attached(name, options = {}) ⇒ Object

Add an attachment to a class.

Options:

  • :styles - a hash containing style names followed by parameters passed to processor

  • :storage - a symbol for a predefined storage or a custom storage class

  • :processor - a symbol for a predefined processor or a custom processor class

Usage:

has_attached :video
has_attached :video, storage: :aws
has_attached :video, styles: { mov: { size: "480p", format: "mov" } }


32
33
34
# File 'lib/attached.rb', line 32

def has_attached(name, options = {})
  Attatcher.define(self, name, options)
end

#validates_attached_extension(name, options = {}) ⇒ Object

Validates an attached extension in a specified set.

Options:

  • :in - allowed values for attached

Usage:

validates_attached_extension :avatar, is: 'png'
validates_attached_extension :avatar, in: %w(png jpg)
validates_attached_extension :avatar, in: [:png, :jpg]
validates_attached_extension :avatar, in: %w(png jpg), message: "extension must be :in"
validates_attached_extension :avatar, in: %w(png jpg), message: "extension must be :in"


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/attached.rb', line 91

def validates_attached_extension(name, options = {})

  message = options[:message] || "extension is invalid"

  options[:in] ||= [options[:is]] if options[:is]

  range = options[:in].map { |element| ".#{element}" }

  validates_inclusion_of :"#{name}_extension", in: range, message: message,
    if: options[:if], unless: options[:unless]
end

#validates_attached_presence(name, options = {}) ⇒ Object

Validates that an attachment is included.

Options:

  • :message - string to be displayed

Usage:

validates_attached_presence :avatar
validates_attached_presence :avatar, message: "must be attached"


114
115
116
117
118
119
120
121
# File 'lib/attached.rb', line 114

def validates_attached_presence(name, options = {})

  message = options[:message] || "must be attached"

  validates_presence_of :"#{name}_identifier", message: message,
    if: options[:if], unless: options[:unless]

end

#validates_attached_size(name, options = {}) ⇒ Object

Validates an attached size in a specified range or minimum and maximum.

Options:

  • :message - string to be displayed with :minimum and :maximum variables

  • :minimum - integer for the minimum byte size of the attached

  • :maximum - integer for the maximum byte size of teh attached

  • :in - range of bytes for file

Usage:

validates_attached_size :avatar, range: 10.megabytes .. 20.megabytes
validates_attached_size :avatar, minimum: 10.megabytes, maximum: 20.megabytes
validates_attached_size :avatar, message: "size must be between :minimum and :maximum bytes"


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/attached.rb', line 51

def validates_attached_size(name, options = {})

  zero = (0.0 / 1.0)
  infi = (1.0 / 0.0)

  minimum = options[:minimum] || options[:in] && options[:in].first || zero
  maximum = options[:maximum] || options[:in] && options[:in].last  || infi

  message = case
  when options[:message] then options[:message]
  when minimum == zero && maximum == infi then "size must be specified"
  when maximum == infi then "size must be a minimum of :minimum" 
  when minimum == zero then "size must be a maximum of :maximum"
  else "size must be between :minimum and :maximum"
  end

  range = minimum..maximum

  message.gsub!(/:minimum/, number_to_size(minimum)) unless minimum == zero
  message.gsub!(/:maximum/, number_to_size(maximum)) unless maximum == infi

  validates_inclusion_of :"#{name}_size", in: range, message: message,
    if: options[:if], unless: options[:unless]

end