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