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" } }


49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/attached.rb', line 49

def has_attached(name, options = {})

  include InstanceMethods

  self.attached_options ||= {}
  self.attached_options[name] = options

  before_save :save_attached
  before_destroy :destroy_attached

  define_method name do
    attachment_for(name)
  end

  define_method "#{name}=" do |file|
    attachment_for(name).assign(file)
  end

  define_method "#{name}?" do
    attachment_for(name).attached?
  end

  define_method "#{name}_url=" do |url|
    attachment_for(name).url = url
  end

  validates_each(name) do |record, attr, value|
    attachment = record.attachment_for(name)
    attachment.errors.each do |error|
      record.errors.add(name, error)
    end
  end

  after_validation do

    if self.errors.include?(:"#{name}_size")
      self.errors[:"#{name}_size"].each do |message|
        self.errors.add(name, message)
      end
      self.errors[:"#{name}_size"].clear
    end

    if self.errors.include?(:"#{name}_extension")
      self.errors[:"#{name}_extension"].each do |message|
        self.errors.add(name, message)
      end
      self.errors[:"#{name}_extension"].clear
    end

    if self.errors.include?(:"#{name}_identifier")
      self.errors[:"#{name}_identifier"].each do |message|
        self.errors.add(name, message)
      end
      self.errors[:"#{name}_identifier"].clear
    end

  end

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"


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/attached.rb', line 164

def validates_attached_extension(name, options = {})

  message = options[:message]
  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"


189
190
191
192
193
194
195
196
197
# File 'lib/attached.rb', line 189

def validates_attached_presence(name, options = {})

  message = options[:message]
  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"


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/attached.rb', line 125

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 = options[:message]
  message ||= "size must be specified" if minimum == zero && maximum == infi
  message ||= "size must be a minimum of :minimum" if maximum == infi
  message ||= "size must be a maximum of :maximum" if minimum == zero
  message ||= "size must be between :minimum and :maximum"

  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