limitable 1.1.0 → 2.0.1

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: c335fa86982f21956881f0c1c3be6e5daee3dc7a6a84ca2de981ccf63b6aba06
4
+ data.tar.gz: 309c1771137400899b3e24e4d71d8e3c0da8f0020fa597581c9b40704118260e
5
5
  SHA512:
6
- metadata.gz: cf6db368c846bd59f870c569d31a49f7bcb6a2d9e5cb508256b9acdbd9cf8b28582db914d0320af77354f8ea2517bcbbbcbb106861934fcbba96a52d25a89513
7
- data.tar.gz: 205f0c6ced808e91f8c3475c0377093f52688e2b7e2c7b26e06e7071969f1c55a6692d1d271470b6f035eed45151910e604e04e23b6c66c28bfa3cd3c66013c4
6
+ metadata.gz: f2a2d4150c455dda3f4034797400549fc993ce0cefca77ba3eb4a256b441f08f4e4be0c217b4b97fdc77f7050b61346dd4ff3b3439f80f6d0f0ab85db2268b1f
7
+ data.tar.gz: 2623f3c517f71e764be0010c82da08de385ba5e3e4b496c0a472cbb6cb81a39a29c597bcbb8c0ccb54ae288a38ccc61cd82302326f7b4a68bf3dcc59edb52b6b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## [2.0.1](https://github.com/benmelz/limitable/compare/v2.0.0...v2.0.1) (2025-04-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add support for rails 8.0 ([#112](https://github.com/benmelz/limitable/issues/112)) ([db56929](https://github.com/benmelz/limitable/commit/db56929c74f51d43a87484d9562261b96910315c))
7
+
8
+ # [2.0.0](https://github.com/benmelz/limitable/compare/v1.1.0...v2.0.0) (2023-07-30)
9
+
10
+
11
+ * 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)
12
+
13
+
14
+ ### BREAKING CHANGES
15
+
16
+ * reword default validation error messages
17
+
1
18
  # [1.1.0](https://github.com/benmelz/limitable/compare/v1.0.1...v1.1.0) (2023-07-17)
2
19
 
3
20
 
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.1"
5
5
  end
data/lib/limitable.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_record'
4
- require 'i18n'
5
- require_relative 'limitable/base'
6
- require_relative 'limitable/version'
3
+ require "active_record"
4
+ require "i18n"
5
+ require_relative "limitable/base"
6
+ require_relative "limitable/locale"
7
+ require_relative "limitable/version"
7
8
 
8
9
  # == Limitable
9
10
  #
@@ -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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Melz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-17 00:00:00.000000000 Z
11
+ date: 2025-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '6'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '8'
22
+ version: '8.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '6'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '8'
32
+ version: '8.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: i18n
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -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,9 +72,9 @@ 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.1/CHANGELOG.md
74
76
  rubygems_mfa_required: 'true'
75
- post_install_message:
77
+ post_install_message:
76
78
  rdoc_options: []
77
79
  require_paths:
78
80
  - lib
@@ -87,8 +89,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
89
  - !ruby/object:Gem::Version
88
90
  version: '0'
89
91
  requirements: []
90
- rubygems_version: 3.4.10
91
- signing_key:
92
+ rubygems_version: 3.5.22
93
+ signing_key:
92
94
  specification_version: 4
93
95
  summary: Inferred database limit validations for ActiveRecord.
94
96
  test_files: []