Module: Shrine::Plugins::ValidationHelpers::AttacherMethods

Defined in:
lib/shrine/plugins/validation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#validate_dimensions(width_range, height_range) ⇒ Object

Validates that the dimensions are in the given range.

validate_dimensions [100..5000, 100..5000]

161
162
163
164
165
166
# File 'lib/shrine/plugins/validation_helpers.rb', line 161

def validate_dimensions((width_range, height_range))
  min_dims = width_range.begin, height_range.begin
  max_dims = width_range.end,   height_range.end

  validate_min_dimensions(min_dims) && validate_max_dimensions(max_dims)
end

#validate_extension_exclusion(extensions, message: nil) ⇒ Object

Validates that the extension is not included in the given list. Comparison is case insensitive.

validate_extension_exclusion %[php jar]

207
208
209
210
211
212
# File 'lib/shrine/plugins/validation_helpers.rb', line 207

def validate_extension_exclusion(extensions, message: nil)
  validate_result(
    extensions.none? { |extension| extension.casecmp(file.extension.to_s) == 0 },
    :extension_exclusion, message, extensions
  )
end

#validate_extension_inclusion(extensions, message: nil) ⇒ Object Also known as: validate_extension

Validates that the extension is included in the given list. Comparison is case insensitive.

validate_extension_inclusion %w[jpg jpeg png gif]

195
196
197
198
199
200
# File 'lib/shrine/plugins/validation_helpers.rb', line 195

def validate_extension_inclusion(extensions, message: nil)
  validate_result(
    extensions.any? { |extension| extension.casecmp(file.extension.to_s) == 0 },
    :extension_inclusion, message, extensions
  )
end

#validate_height(height_range) ⇒ Object

Validates that the `height` metadata is in the given range.

validate_height 100..5000

128
129
130
131
132
# File 'lib/shrine/plugins/validation_helpers.rb', line 128

def validate_height(height_range)
  min_height, max_height = height_range.begin, height_range.end

  validate_min_height(min_height) && validate_max_height(max_height)
end

#validate_max_dimensions(max_width, max_height, message: nil) ⇒ Object

Validates that the dimensions are not larger than specified.

validate_max_dimensions [5000, 5000]

137
138
139
140
141
142
143
144
# File 'lib/shrine/plugins/validation_helpers.rb', line 137

def validate_max_dimensions((max_width, max_height), message: nil)
  fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]

  validate_result(
    file["width"] <= max_width && file["height"] <= max_height,
    :max_dimensions, message, [max_width, max_height]
  )
end

#validate_max_height(max, message: nil) ⇒ Object

Validates that the `height` metadata is not larger than `max`. Requires the `store_dimensions` plugin.

validate_max_height 5000

109
110
111
112
113
# File 'lib/shrine/plugins/validation_helpers.rb', line 109

def validate_max_height(max, message: nil)
  fail Error, "height metadata is missing" unless file["height"]

  validate_result(file["height"] <= max, :max_height, message, max)
end

#validate_max_size(max, message: nil) ⇒ Object

Validates that the `size` metadata is not larger than `max`.

validate_max_size 5*1024*1024

54
55
56
# File 'lib/shrine/plugins/validation_helpers.rb', line 54

def validate_max_size(max, message: nil)
  validate_result(file.size <= max, :max_size, message, max)
end

#validate_max_width(max, message: nil) ⇒ Object

Validates that the `width` metadata is not larger than `max`. Requires the `store_dimensions` plugin.

validate_max_width 5000

79
80
81
82
83
# File 'lib/shrine/plugins/validation_helpers.rb', line 79

def validate_max_width(max, message: nil)
  fail Error, "width metadata is missing" unless file["width"]

  validate_result(file["width"] <= max, :max_width, message, max)
end

#validate_mime_type_exclusion(types, message: nil) ⇒ Object

Validates that the `mime_type` metadata is not included in the given list.

validate_mime_type_exclusion %w[text/x-php]

184
185
186
187
188
189
# File 'lib/shrine/plugins/validation_helpers.rb', line 184

def validate_mime_type_exclusion(types, message: nil)
  validate_result(
    !types.include?(file.mime_type),
    :mime_type_exclusion, message, types
  )
end

#validate_mime_type_inclusion(types, message: nil) ⇒ Object Also known as: validate_mime_type

Validates that the `mime_type` metadata is included in the given list.

validate_mime_type_inclusion %w[audio/mp3 audio/flac]

172
173
174
175
176
177
# File 'lib/shrine/plugins/validation_helpers.rb', line 172

def validate_mime_type_inclusion(types, message: nil)
  validate_result(
    types.include?(file.mime_type),
    :mime_type_inclusion, message, types
  )
end

#validate_min_dimensions(min_width, min_height, message: nil) ⇒ Object

Validates that the dimensions are not smaller than specified.

validate_max_dimensions [100, 100]

149
150
151
152
153
154
155
156
# File 'lib/shrine/plugins/validation_helpers.rb', line 149

def validate_min_dimensions((min_width, min_height), message: nil)
  fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]

  validate_result(
    file["width"] >= min_width && file["height"] >= min_height,
    :min_dimensions, message, [min_width, min_height]
  )
end

#validate_min_height(min, message: nil) ⇒ Object

Validates that the `height` metadata is not smaller than `min`. Requires the `store_dimensions` plugin.

validate_min_height 100

119
120
121
122
123
# File 'lib/shrine/plugins/validation_helpers.rb', line 119

def validate_min_height(min, message: nil)
  fail Error, "height metadata is missing" unless file["height"]

  validate_result(file["height"] >= min, :min_height, message, min)
end

#validate_min_size(min, message: nil) ⇒ Object

Validates that the `size` metadata is not smaller than `min`.

validate_min_size 1024

61
62
63
# File 'lib/shrine/plugins/validation_helpers.rb', line 61

def validate_min_size(min, message: nil)
  validate_result(file.size >= min, :min_size, message, min)
end

#validate_min_width(min, message: nil) ⇒ Object

Validates that the `width` metadata is not smaller than `min`. Requires the `store_dimensions` plugin.

validate_min_width 100

89
90
91
92
93
# File 'lib/shrine/plugins/validation_helpers.rb', line 89

def validate_min_width(min, message: nil)
  fail Error, "width metadata is missing" unless file["width"]

  validate_result(file["width"] >= min, :min_width, message, min)
end

#validate_size(size_range) ⇒ Object

Validates that the `size` metadata is in the given range.

validate_size 1024..5*1024*1024

68
69
70
71
72
# File 'lib/shrine/plugins/validation_helpers.rb', line 68

def validate_size(size_range)
  min_size, max_size = size_range.begin, size_range.end

  validate_min_size(min_size) && validate_max_size(max_size)
end

#validate_width(width_range) ⇒ Object

Validates that the `width` metadata is in the given range.

validate_width 100..5000

98
99
100
101
102
# File 'lib/shrine/plugins/validation_helpers.rb', line 98

def validate_width(width_range)
  min_width, max_width = width_range.begin, width_range.end

  validate_min_width(min_width) && validate_max_width(max_width)
end