module Shrine::Plugins::ValidationHelpers

  1. lib/shrine/plugins/validation_helpers.rb

Documentation lives in doc/plugins/validation_helpers.md on GitHub.

Methods

Public Class

  1. configure

Constants

DEFAULT_MESSAGES = { max_size: ->(max) { "is too large (max is #{PRETTY_FILESIZE.call(max)})" }, min_size: ->(min) { "is too small (min is #{PRETTY_FILESIZE.call(min)})" }, max_width: ->(max) { "is too wide (max is #{max} px)" }, min_width: ->(min) { "is too narrow (min is #{min} px)" }, max_height: ->(max) { "is too tall (max is #{max} px)" }, min_height: ->(min) { "is too short (min is #{min} px)" }, mime_type_inclusion: ->(list) { "isn't of allowed type (allowed types: #{list.join(", ")})" }, mime_type_exclusion: ->(list) { "is of forbidden type" }, extension_inclusion: ->(list) { "isn't of allowed format (allowed formats: #{list.join(", ")})" }, extension_exclusion: ->(list) { "is of forbidden format" }, }  
FILESIZE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"].freeze  
PRETTY_FILESIZE = lambda do |bytes| return "0.0 B" if bytes == 0 exp = Math.log(bytes, 1024).floor max_exp = FILESIZE_UNITS.length - 1 exp = max_exp if exp > max_exp "%.1f %s" % [bytes.to_f / 1024 ** exp, FILESIZE_UNITS[exp]] end  

Returns filesize in a human readable format with units. Uses the binary JEDEC unit system, i.e. 1.0 KB = 1024 bytes

Public Class methods

configure (uploader, opts = {})
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
 9 def self.configure(uploader, opts = {})
10   uploader.opts[:validation_default_messages] ||= {}
11   uploader.opts[:validation_default_messages].merge!(opts[:default_messages] || {})
12 end