|
| 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). |
0 commit comments