Title / Description
Code # Examples: # validates_file_format_of :field, :in => ["gif", "png", "jpg"] # validates_file_format_of :field, :in => ["image/jpeg"] def validates_file_format_of(*attrs) options = attrs.pop if attrs.last.is_a?Hash raise ArgumentError, I18n.t(:include_option) if !options || !options[:in] options[:in] = [options[:in]] if options[:in].is_a?String raise ArgumentError, I18n.t(:invalid_value_for) unless options[:in].is_a?Array validates_each(attrs, options) do |record, attr, value| unless value.blank? mime_extensions = record.send("#{attr}_options")[:mime_extensions] extensions = options[:in].map{|o| mime_extensions[o] || o } record.errors.add attr, I18n.t(:not_a_valid_format) unless extensions.include?(value.scan(EXT_REGEXP).flatten.first) end end end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code