Sha256: 1a602930343114df5d997b4c9606ebcd865adec14d84df69add39995b29dff40

Contents?: true

Size: 1.24 KB

Versions: 24

Compression:

Stored size: 1.24 KB

Contents

require 'active_support'

module CarrierWave
  module Uploader
    module FileSize
      extend ActiveSupport::Concern

      included do
        before :cache, :check_size!
      end

      ##
      # Override this method in your uploader to provide a Range of Size which
      # are allowed to be uploaded.
      # === Returns
      #
      # [NilClass, Range] a size range which are permitted to be uploaded
      #
      # === Examples
      #
      #     def size_range
      #       3256...5748
      #     end
      #
      def size_range; end

      private

      def check_size!(new_file)
        size = new_file.size
        expected_size_range = size_range
        if expected_size_range.is_a?(::Range)
          if size < expected_size_range.min
            raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.min_size_error", :min_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.min))
          elsif size > expected_size_range.max
            raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.max_size_error", :max_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.max))
          end
        end
      end

    end # FileSize
  end # Uploader
end # CarrierWave

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/carrierwave-2.2.6/lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.6 lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.5 lib/carrierwave/uploader/file_size.rb
carrierwave-1.3.4 lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.4 lib/carrierwave/uploader/file_size.rb
carrierwave-1.3.3 lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.3 lib/carrierwave/uploader/file_size.rb
carrierwave-3.0.0.beta lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.2 lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.1 lib/carrierwave/uploader/file_size.rb
carrierwave-2.2.0 lib/carrierwave/uploader/file_size.rb
carrierwave-2.1.1 lib/carrierwave/uploader/file_size.rb
carrierwave-1.3.2 lib/carrierwave/uploader/file_size.rb
carrierwave-2.1.0 lib/carrierwave/uploader/file_size.rb
carrierwave-2.0.2 lib/carrierwave/uploader/file_size.rb
carrierwave-2.0.1 lib/carrierwave/uploader/file_size.rb
carrierwave-2.0.0 lib/carrierwave/uploader/file_size.rb
carrierwave-2.0.0.rc lib/carrierwave/uploader/file_size.rb
carrierwave-1.3.1 lib/carrierwave/uploader/file_size.rb
carrierwave-1.3.0 lib/carrierwave/uploader/file_size.rb