lib/attached.rb in attached-0.0.7 vs lib/attached.rb in attached-0.0.8

- old
+ new

@@ -102,19 +102,26 @@ # validates_attached_size :avatar, :minimum => 10.megabytes, :maximum => 20.megabytes # validates_attached_size :avatar, :message => "size must be between :minimum and :maximum bytes" def validates_attached_size(name, options = {}) - message = options[:message] || "size must be between :minimum and :maximum" + zero = (0.0 / 1.0) + infi = (1.0 / 0.0) - minimum = options[:minimum] || options[:in] && options[:in].first || (0.0 / 1.0) - maximum = options[:maximum] || options[:in] && options[:in].last || (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 bytes" range = minimum..maximum - message.gsub!(/:minimum/, number_to_human_size(minimum)) - message.gsub!(/:maximum/, number_to_human_size(maximum)) + message.gsub!(/:minimum/, number_to_human_size(minimum)) unless minimum == zero + message.gsub!(/:maximum/, number_to_human_size(maximum)) unless maximum == infi validates_inclusion_of :"#{name}_size", :in => range, :message => message, :if => options[:if], :unless => options[:unless] end @@ -136,9 +143,32 @@ message = options[:message] || "must be attached" validates_presence_of :"#{name}_identifier", :message => message, :if => options[:if], :unless => options[:unless] + end + + + private + + + SINGULAR = 1 + + def number_to_human_size(number, options = {}) + return if number == 0.0 / 1.0 + return if number == 1.0 / 0.0 + + base = 1024 + units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"] + + exponent = (Math.log(number) / Math.log(base)).floor + + number /= base ** exponent + unit = units[exponent] + + number == SINGULAR ? unit.gsub!(/s$/, '') : unit.gsub!(/$/, 's') + + "#{number} #{unit}" end end \ No newline at end of file