README.md in file_validators-1.0.0 vs README.md in file_validators-1.1.0
- old
+ new
@@ -45,31 +45,46 @@
## API
### File Size Validator:
-* `in`: A range of bytes
+* `in`: A range of bytes or a proc that returns a range
```ruby
validates :avatar, file_size: { in: 100.kilobytes..1.megabyte }
```
-* `less_than_or_equal_to`: Less than or equal to a number in bytes
+* `less_than`: Less than a number in bytes or a proc that returns a number
```ruby
+validates :avatar, file_size: { less_than: 2.gigabytes }
+```
+* `less_than_or_equal_to`: Less than or equal to a number in bytes or a proc that returns a number
+```ruby
validates :avatar, file_size: { less_than_or_equal_to: 50.bytes }
```
-* `greater_than_or_equal_to`: Greater than or equal to a number in bytes
+* `greater_than`: greater than a number in bytes or a proc that returns a number
```ruby
+validates :avatar, file_size: { greater_than: 1.byte }
+```
+* `greater_than_or_equal_to`: Greater than or equal to a number in bytes or a proc that returns a number
+```ruby
validates :avatar, file_size: { greater_than_or_equal_to: 50.bytes }
```
-* `less_than`: Less than a number in bytes
+* `message`: Error message to display. With all the options above except `:in`, you will get `count` as a replacement.
+With `:in` you will get `min` and `max` as replacements.
+`count`, `min` and `max` each will have its value and unit together.
+You can write error messages without using any replacement.
```ruby
-validates :avatar, file_size: { less_than: 2.gigabytes }
+validates :avatar, file_size: { less_than: 100.kilobytes,
+ message: 'avatar file size should be less than %{count}' }
```
-* `greater_than`: greater than a number in bytes
```ruby
-validates :avatar, file_size: { greater_than: 1.byte }
+validates :document, file_size: { in: 1.kilobyte..1.megabyte,
+ message: 'document should be within %{min} and %{max}' }
```
-You can also combine these options.
+* `if`: A lambda or name of an instance method. Validation will only be run if this lambda or method returns true.
+* `unless`: Same as `if` but validates if lambda or method returns false.
+
+You can combine different options.
```ruby
validates :avatar, file_size: { less_than: 1.megabyte,
greater_than_or_equal_to: 20.kilobytes }
```
The following two examples are equivalent:
@@ -78,29 +93,19 @@
less_than_or_equal_to: 3.megabytes }
```
```ruby
validates :avatar, file_size: { in: 500.kilobytes..3.megabytes }
```
-If you use `:in`, then the other options will be neglected.
-* `message`: Error message to display. With all the options above except `:in`, you will get `count` as a replacement.
-With `:in` you will get `min` and `max` as replacements.
-`count`, `min` and `max` each will have its value and unit together.
-You can write error messages without using any replacement.
+Options can also take `Proc`/`lambda`:
+
```ruby
-validates :avatar, file_size: { less_than: 100.kilobytes,
- message: 'avatar file size should be less than %{count}' }
+validates :avatar, file_size: { less_than: lambda { |record| record.size_in_bytes } }
```
-```ruby
-validates :document, file_size: { in: 1.kilobyte..1.megabyte,
- message: 'document should be within %{min} and %{max}' }
-```
-* `if`: A lambda or name of an instance method. Validation will only be run if this lambda or method returns true.
-* `unless`: Same as `if` but validates if lambda or method returns false.
### File Content Type Validator
-* `allow`: Allowed content types. Can be a single content type or an array. Each type can be a String or a Regexp. Allows all by default.
+* `allow`: Allowed content types. Can be a single content type or an array. Each type can be a String or a Regexp. It also accepts proc. Allows all by default.
```ruby
# string
validates :avatar, file_content_type: { allow: 'image/jpeg' }
```
```ruby
@@ -117,36 +122,15 @@
```
```ruby
# array of regexps and strings
validates :attachment, file_content_type: { allow: [/^image\/.*/, 'video/mp4'] }
```
-* `exclude`: Forbidden content types. Can be a single content type or an array. Each type can be a String or a Regexp.
```ruby
-# string
-validates :avatar, file_content_type: { exclude: 'image/jpeg' }
+# proc/lambda example
+validates :video, file_content_type: { allow: lambda { |record| record.content_types } }
```
-```ruby
-# array of strings
-validates :attachment, file_content_type: { exclude: ['image/jpeg', 'text/plain'] }
-```
-```ruby
-# regexp
-validates :avatar, file_content_type: { exclude: /^image\/.*/ }
-```
-```ruby
-# array of regexps
-validates :attachment, file_content_type: { exclude: [/^image\/.*/, /^text\/.*/] }
-```
-```ruby
-# array of regexps and strings
-validates :attachment, file_content_type: { exclude: [/^text\/.*/, 'image/gif'] }
-```
-You can also combine `:allow` and `:exclude`:
-```ruby
-# this will allow all the image types except gif
-validates :avatar, file_content_type: { allow: /^image\/.*/, exclude: 'image/gif' }
-```
+* `exclude`: Forbidden content types. Can be a single content type or an array. Each type can be a String or a Regexp. It also accepts `proc`. See `:allow` options examples.
* `message`: The message to display when the uploaded file has an invalid content type.
You will get `types` as a replacement. You can write error messages without using any replacement.
```ruby
validates :avatar, file_content_type: { allow: ['image/jpeg', 'image/gif'],
message: 'should have content type %{types}' }
@@ -156,19 +140,38 @@
message: 'Avatar only allows jpeg and gif image files' }
```
* `if`: A lambda or name of an instance method. Validation will only be run is this lambda or method returns true.
* `unless`: Same as `if` but validates if lambda or method returns false.
+You can combine `:allow` and `:exclude`:
+```ruby
+# this will allow all the image types except png and gif
+validates :avatar, file_content_type: { allow: /^image\/.*/, exclude: ['image/png', 'image/gif'] }
+```
+
## i18n Translations
-By default, `FileSizeValidator` will use the error messages of `:less_than`, `:greater_than_or_equal_to` etc from `errors.messages` of your locale. `errors.messages` translation is available under ActiveModel's locale.
+File Size Errors
+* `file_size_is_in`: takes `min` and `max` as replacements
+* `file_size_is_less_than`: takes `count` as replacement
+* `file_size_is_less_than_or_equal_to`: takes `count` as replacement
+* `file_size_is_greater_than`: takes `count` as replacement
+* `file_size_is_greater_than_or_equal_to`: takes `count` as replacement
-For `:in`, `:allow` and `:exclude` you will have to write your own error messages under `errors.messages`.
+Content Type Errors
+* `allowed_file_content_types`: generated when you have specified allowed types but the content type
+of the file doesn't match. takes `types` as replacement.
+* `excluded_file_content_types`: generated when you have specified excluded types and the content type
+of the file matches anyone of them. takes `types` as replacement.
+This gem provides `en` translations for this errors under `errors.messages` namespace.
+If you want to override and/or create other locales, you can
+check [this](https://github.com/musaffa/file_validators/blob/master/lib/file_validators/locale/en.yml) out to see how translations are done.
+
You can override all of them with the `:message` option.
For unit format, it will use `number.human.storage_units.format` from your locale.
-For unit translation, it will use `number.human.storage_units`.
+For unit translation, `number.human.storage_units` is used.
Rails applications already have these translations either in ActiveSupport's locale (Rails 4) or in ActionView's locale (Rails 3).
In case your setup doesn't have the translations, here's an example for `en`:
```yml
en: