Skip to content

Commit 0040aeb

Browse files
authored
feat: limitable (#1)
- limit validations for integer columns - limit validations for string columns - limit validations for text columns
1 parent 58bed0f commit 0040aeb

24 files changed

+754
-0
lines changed

.github/dependabot.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: bundler
4+
directory: /
5+
schedule:
6+
interval: daily
7+
commit-message:
8+
prefix: chore(deps)

.github/workflows/cd.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: cd
2+
on:
3+
push:
4+
branches: [main]
5+
jobs:
6+
semantic-release:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: ruby/setup-ruby@v1
11+
with:
12+
ruby-version: 3.0
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 18
16+
- run: npm install --global semantic-release@21 @semantic-release/changelog@6 @semantic-release/git@10 semantic-release-rubygem@1
17+
- run: npx semantic-release@21
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }}

.github/workflows/ci.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: ci
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
jobs:
8+
rspec:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
ruby-version:
13+
- 3.0
14+
- 3.1
15+
- 3.2
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: ruby/setup-ruby@v1
19+
with:
20+
ruby-version: ${{ matrix.ruby-version }}
21+
- run: bin/setup
22+
- run: bin/rake appraisal rspec
23+
rubocop:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v3
27+
- uses: ruby/setup-ruby@v1
28+
with:
29+
ruby-version: 3.0
30+
- run: bin/setup
31+
- run: bin/rake rubocop

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/coverage/
2+
/gemfiles/
3+
/pkg/
4+
/.rspec_status
5+
/limitable-*.gem

.releaserc.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"branches": [
3+
"main"
4+
],
5+
"plugins": [
6+
"@semantic-release/commit-analyzer",
7+
"@semantic-release/release-notes-generator",
8+
"@semantic-release/changelog",
9+
[
10+
"semantic-release-rubygem",
11+
{
12+
"updateGemfileLock": true,
13+
"gemFileDir": "."
14+
}
15+
],
16+
[
17+
"@semantic-release/git",
18+
{
19+
"assets": [
20+
"lib/limitable/version.rb",
21+
"Gemfile.lock",
22+
"CHANGELOG.md"
23+
],
24+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
25+
}
26+
],
27+
[
28+
"@semantic-release/github",
29+
{
30+
"assets": [
31+
{
32+
"path": "*.gem"
33+
}
34+
]
35+
}
36+
]
37+
]
38+
}

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require:
2+
- rubocop-performance
3+
- rubocop-rake
4+
- rubocop-rspec
5+
6+
AllCops:
7+
Exclude:
8+
- bin/rake
9+
- gemfiles/**/*
10+
NewCops: enable
11+
TargetRubyVersion: 3.0

Appraisals

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
appraise 'rails-6.0' do
4+
gem 'activerecord', '~> 6.0.0'
5+
end
6+
7+
appraise 'rails-6.1' do
8+
gem 'activerecord', '~> 6.1.0'
9+
end
10+
11+
appraise 'rails-7.0' do
12+
gem 'activerecord', '~> 7.0.0'
13+
end

Gemfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gemspec
6+
7+
gem 'appraisal'
8+
gem 'rake'
9+
gem 'rspec'
10+
gem 'rubocop'
11+
gem 'rubocop-performance'
12+
gem 'rubocop-rake'
13+
gem 'rubocop-rspec'
14+
gem 'simplecov'
15+
gem 'sqlite3'

Gemfile.lock

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
PATH
2+
remote: .
3+
specs:
4+
limitable (0.0.0.pre.development)
5+
activerecord (>= 6, < 8)
6+
i18n (>= 1.6, < 2)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
activemodel (7.0.5)
12+
activesupport (= 7.0.5)
13+
activerecord (7.0.5)
14+
activemodel (= 7.0.5)
15+
activesupport (= 7.0.5)
16+
activesupport (7.0.5)
17+
concurrent-ruby (~> 1.0, >= 1.0.2)
18+
i18n (>= 1.6, < 2)
19+
minitest (>= 5.1)
20+
tzinfo (~> 2.0)
21+
appraisal (2.4.1)
22+
bundler
23+
rake
24+
thor (>= 0.14.0)
25+
ast (2.4.2)
26+
concurrent-ruby (1.2.2)
27+
diff-lcs (1.5.0)
28+
docile (1.4.0)
29+
i18n (1.13.0)
30+
concurrent-ruby (~> 1.0)
31+
json (2.6.3)
32+
minitest (5.18.0)
33+
parallel (1.23.0)
34+
parser (3.2.2.1)
35+
ast (~> 2.4.1)
36+
rainbow (3.1.1)
37+
rake (13.0.6)
38+
regexp_parser (2.8.0)
39+
rexml (3.2.5)
40+
rspec (3.12.0)
41+
rspec-core (~> 3.12.0)
42+
rspec-expectations (~> 3.12.0)
43+
rspec-mocks (~> 3.12.0)
44+
rspec-core (3.12.2)
45+
rspec-support (~> 3.12.0)
46+
rspec-expectations (3.12.3)
47+
diff-lcs (>= 1.2.0, < 2.0)
48+
rspec-support (~> 3.12.0)
49+
rspec-mocks (3.12.5)
50+
diff-lcs (>= 1.2.0, < 2.0)
51+
rspec-support (~> 3.12.0)
52+
rspec-support (3.12.0)
53+
rubocop (1.51.0)
54+
json (~> 2.3)
55+
parallel (~> 1.10)
56+
parser (>= 3.2.0.0)
57+
rainbow (>= 2.2.2, < 4.0)
58+
regexp_parser (>= 1.8, < 3.0)
59+
rexml (>= 3.2.5, < 4.0)
60+
rubocop-ast (>= 1.28.0, < 2.0)
61+
ruby-progressbar (~> 1.7)
62+
unicode-display_width (>= 2.4.0, < 3.0)
63+
rubocop-ast (1.28.1)
64+
parser (>= 3.2.1.0)
65+
rubocop-capybara (2.18.0)
66+
rubocop (~> 1.41)
67+
rubocop-factory_bot (2.23.1)
68+
rubocop (~> 1.33)
69+
rubocop-performance (1.18.0)
70+
rubocop (>= 1.7.0, < 2.0)
71+
rubocop-ast (>= 0.4.0)
72+
rubocop-rake (0.6.0)
73+
rubocop (~> 1.0)
74+
rubocop-rspec (2.22.0)
75+
rubocop (~> 1.33)
76+
rubocop-capybara (~> 2.17)
77+
rubocop-factory_bot (~> 2.22)
78+
ruby-progressbar (1.13.0)
79+
simplecov (0.22.0)
80+
docile (~> 1.1)
81+
simplecov-html (~> 0.11)
82+
simplecov_json_formatter (~> 0.1)
83+
simplecov-html (0.12.3)
84+
simplecov_json_formatter (0.1.4)
85+
sqlite3 (1.6.3-arm64-darwin)
86+
sqlite3 (1.6.3-x86_64-linux)
87+
thor (1.2.2)
88+
tzinfo (2.0.6)
89+
concurrent-ruby (~> 1.0)
90+
unicode-display_width (2.4.2)
91+
92+
PLATFORMS
93+
arm64-darwin-22
94+
x86_64-linux
95+
96+
DEPENDENCIES
97+
appraisal
98+
limitable!
99+
rake
100+
rspec
101+
rubocop
102+
rubocop-performance
103+
rubocop-rake
104+
rubocop-rspec
105+
simplecov
106+
sqlite3
107+
108+
BUNDLED WITH
109+
2.3.26

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Benjamin Melz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Limitable
2+
3+
Limitable scans your ActiveRecord database schema for column size limits and defines corresponding model validations
4+
so that you don't have to.
5+
6+
It aims to make your database schema the "one source of truth" about the maximum data sizes your columns can handle.
7+
More practically, it removes the redundant need for explicit guards such as:
8+
9+
```ruby
10+
validates :my_string_column, length: { less_than: 256 }
11+
12+
validates :my_integer_column, numericality: { less_than: 2_147_483_647 }
13+
14+
begin
15+
# ...
16+
rescue ActiveRecord::ValueTooLong
17+
# ...
18+
end
19+
```
20+
21+
## Installation
22+
23+
Install the gem and add to the application's Gemfile by executing:
24+
25+
```shell
26+
bundle add limitable
27+
```
28+
29+
If bundler is not being used to manage dependencies, install the gem by executing:
30+
31+
```shell
32+
gem install limitable
33+
```
34+
35+
## Usage
36+
37+
Once included in a model, `Limitable` will scan `integer`, `string` and `text` columns for size limits
38+
and define _byte size_ validations accordingly. Limits are configurable through `ActiveRecord` migrations.
39+
40+
### Quick Start
41+
42+
To enable these database limit validations globally:
43+
44+
```ruby
45+
class ApplicationRecord < ActiveRecord::Base
46+
include Limitable::Base
47+
48+
# ...
49+
end
50+
```
51+
52+
To enable database limit validations on a per-model basis:
53+
54+
```ruby
55+
class MyModel < ApplicationRecord
56+
include Limitable
57+
58+
# ...
59+
end
60+
```
61+
62+
### SQL Adapters
63+
64+
`Limitable` is designed to be SQL adapter agnostic, however although some adapters have different default
65+
default behaviors than others.
66+
67+
#### `mysql2`
68+
69+
MySQL/mariadb has and reports hard limits on all supported column types. As such, you won't need to specify explicit
70+
limits in your database migrations/schema unless you want to change them from their default values.
71+
72+
#### `pg`
73+
74+
PostgreSQL has and reports hard limits on its integer columns, however it supports and defaults to unlimited
75+
string/text columns. If you wish for limits to be set, they must be explicitly set in your database migrations/schema.
76+
77+
#### `sqlite3`
78+
79+
SQLite has hard limits on most of its column types, but it does not report them to active record. If you wish for limits
80+
to be picked up, they must be explicitly set in your database migrations/schema.
81+
82+
## Development
83+
84+
- Run `bin/setup` to install dependencies.
85+
- Run `bin/rake appraisal rspec` to run the tests.
86+
- Run `bin/rake rubocop` to run the linter.
87+
- Run `bin/console` for an interactive prompt that will allow you to experiment.
88+
89+
## Contributing
90+
91+
Bug reports and pull requests are welcome on GitHub at https://github.com/benmelz/limitable.
92+
93+
## License
94+
95+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'appraisal'
4+
require 'bundler/gem_tasks'
5+
require 'rspec/core/rake_task'
6+
require 'rubocop/rake_task'
7+
8+
RSpec::Core::RakeTask.new(:rspec)
9+
RuboCop::RakeTask.new

0 commit comments

Comments
 (0)