limitable 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40858400b0402bc9997ba697359789cf32255fc366d746a79d9fefb01c725ef1
4
- data.tar.gz: d8719386f9528e0f9c508ece436f02259cc620a9692f84466eef205c2d4eab09
3
+ metadata.gz: fa345bff02b1a03c0ef3b2515de75383f9e1f55d2cf397fcd2660fe0a66b0746
4
+ data.tar.gz: 3f467cd6b86b8b97b1d7b9d749d0a507b943d9395c4691503cd051360d6ce557
5
5
  SHA512:
6
- metadata.gz: cf6db368c846bd59f870c569d31a49f7bcb6a2d9e5cb508256b9acdbd9cf8b28582db914d0320af77354f8ea2517bcbbbcbb106861934fcbba96a52d25a89513
7
- data.tar.gz: 205f0c6ced808e91f8c3475c0377093f52688e2b7e2c7b26e06e7071969f1c55a6692d1d271470b6f035eed45151910e604e04e23b6c66c28bfa3cd3c66013c4
6
+ metadata.gz: 6f3d2b2a5ba1904d1a6537bf635cc5ba1c8c374d381ae92fab53b44895e0df3c961ecfbd5bb6dd9440622bb256c5c994cf29a73f416fbea96d0974ea16fa5d46
7
+ data.tar.gz: e14012f9cb14604324304088be9cb9cd6dc620e40b2e5233f0d2964386ebccbd4fe755e12859cd95be2a73d3d60bb5c6c3c78bb5d747aca65aa8ff26120d35ea
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # [2.0.0](https://github.com/benmelz/limitable/compare/v1.1.0...v2.0.0) (2023-07-30)
2
+
3
+
4
+ * feat!: custom translations (#15) ([bacc785](https://github.com/benmelz/limitable/commit/bacc7858d7d506bba60bad2f1d39c32732136aa3)), closes [#15](https://github.com/benmelz/limitable/issues/15) [#12](https://github.com/benmelz/limitable/issues/12)
5
+
6
+
7
+ ### BREAKING CHANGES
8
+
9
+ * reword default validation error messages
10
+
1
11
  # [1.1.0](https://github.com/benmelz/limitable/compare/v1.0.1...v1.1.0) (2023-07-17)
2
12
 
3
13
 
data/README.md CHANGED
@@ -59,6 +59,22 @@ class MyModel < ApplicationRecord
59
59
  end
60
60
  ```
61
61
 
62
+ ### Translations
63
+
64
+ `Limitable` ships with i18n support for its validation error messages. Each column type has its own translation key,
65
+ outlined alongside their default values in `lib/limitable/locale/en.yml`.
66
+
67
+ Each validator will pass a `limit` parameter (min/max integer for integer columns, bytes for string/text/binary),
68
+ which can be used to make the messages less ambiguous if desired.
69
+
70
+ e.g.
71
+
72
+ ```yaml
73
+ en:
74
+ limitable:
75
+ string_limit_exceeded: "may not exceed %{limit} characters"
76
+ ```
77
+
62
78
  ### SQL Adapters
63
79
 
64
80
  `Limitable` is designed to be SQL adapter agnostic, however different adapters have different default behaviors that
@@ -0,0 +1,8 @@
1
+ en:
2
+ limitable:
3
+ integer_limit_exceeded:
4
+ upper: "is too large"
5
+ lower: "is too small"
6
+ string_limit_exceeded: "is too long"
7
+ text_limit_exceeded: "is too long"
8
+ binary_limit_exceeded: "is too large"
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+
5
+ module Limitable
6
+ # == Limitable::Locale
7
+ #
8
+ # Loads the default limitable custom translations into i18n.
9
+ #
10
+ module Locale
11
+ I18n.load_path << File.expand_path('locale/en.yml', __dir__)
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limitable
4
- VERSION = '1.1.0'
4
+ VERSION = '2.0.0'
5
5
  end
data/lib/limitable.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'active_record'
4
4
  require 'i18n'
5
5
  require_relative 'limitable/base'
6
+ require_relative 'limitable/locale'
6
7
  require_relative 'limitable/version'
7
8
 
8
9
  # == Limitable
@@ -32,10 +33,8 @@ module Limitable
32
33
  return if limit.blank?
33
34
 
34
35
  case column.type
35
- when :integer
36
- klass.validate(&build_integer_limit_validator(column_name, limit))
37
- when :binary, :string, :text
38
- klass.validate(&build_string_limit_validator(column_name, limit))
36
+ when :integer, :string, :text, :binary
37
+ klass.validate(&send(:"build_#{column.type}_limit_validator", column_name, limit))
39
38
  end
40
39
  end
41
40
 
@@ -49,18 +48,35 @@ module Limitable
49
48
  end
50
49
  next unless value.is_a? Integer
51
50
 
52
- errors.add column_name, I18n.t('errors.messages.greater_than_or_equal_to', count: min) if value < min
53
- errors.add column_name, I18n.t('errors.messages.less_than_or_equal_to', count: max) if value > max
51
+ errors.add column_name, I18n.t('limitable.integer_limit_exceeded.lower', limit: min) if value < min
52
+ errors.add column_name, I18n.t('limitable.integer_limit_exceeded.upper', limit: max) if value > max
54
53
  end
55
54
  end
56
55
 
57
56
  def build_string_limit_validator(column_name, limit)
58
57
  lambda do
59
58
  value = self.class.type_for_attribute(column_name).serialize self[column_name]
60
- value = value.to_s if value.is_a? ActiveModel::Type::Binary::Data
61
59
  next unless value.is_a?(String) && value.bytesize > limit
62
60
 
63
- errors.add column_name, I18n.t('errors.messages.too_long.other', count: limit)
61
+ errors.add column_name, I18n.t('limitable.string_limit_exceeded', limit: limit)
62
+ end
63
+ end
64
+
65
+ def build_text_limit_validator(column_name, limit)
66
+ lambda do
67
+ value = self.class.type_for_attribute(column_name).serialize self[column_name]
68
+ next unless value.is_a?(String) && value.bytesize > limit
69
+
70
+ errors.add column_name, I18n.t('limitable.text_limit_exceeded', limit: limit)
71
+ end
72
+ end
73
+
74
+ def build_binary_limit_validator(column_name, limit)
75
+ lambda do
76
+ value = self.class.type_for_attribute(column_name).serialize self[column_name]
77
+ next unless value.is_a?(ActiveModel::Type::Binary::Data) && value.to_s.bytesize > limit
78
+
79
+ errors.add column_name, I18n.t('limitable.binary_limit_exceeded', limit: limit)
64
80
  end
65
81
  end
66
82
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: limitable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Melz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-17 00:00:00.000000000 Z
11
+ date: 2023-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -63,6 +63,8 @@ files:
63
63
  - README.md
64
64
  - lib/limitable.rb
65
65
  - lib/limitable/base.rb
66
+ - lib/limitable/locale.rb
67
+ - lib/limitable/locale/en.yml
66
68
  - lib/limitable/version.rb
67
69
  homepage: https://github.com/benmelz/limitable
68
70
  licenses:
@@ -70,7 +72,7 @@ licenses:
70
72
  metadata:
71
73
  homepage_uri: https://github.com/benmelz/limitable
72
74
  source_code_uri: https://github.com/benmelz/limitable
73
- changelog_uri: https://github.com/benmelz/limitable/blob/v1.1.0/CHANGELOG.md
75
+ changelog_uri: https://github.com/benmelz/limitable/blob/v2.0.0/CHANGELOG.md
74
76
  rubygems_mfa_required: 'true'
75
77
  post_install_message:
76
78
  rdoc_options: []