samplerrrr

Ruby code posted
created at 10 Dec 02:31

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
931 Bytes in 5 ms with coderay