Introspects your database string field maximum lengths and automatically defines length validations.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles
lib
rails
spec
.gitignore
.rspec
.travis.yml
Appraisals
CHANGELOG.rdoc
Gemfile
Gemfile.lock
LICENSE
README.rdoc
Rakefile
init.rb
validates_lengths_from_database.gemspec

README.rdoc

Build Status Dependency Status

Validates Lengths from Database

Few people add length validations to fields in their database, and when saving such fields that have exhausted their length, an SQL error occurs. This gem introspects your table schema for maximum lengths on string and text fields and automatically adds length validations to the model.

Requirements

As of v0.5.0, validates_lengths_from_database lazily loads column information and requires Rails 3+. If you are using Rails 2, please use the v0.4.0 release.

Due to issues maintaining backwards compatibility, as of v0.4.0 validates_lengths_from_database requires ruby 1.9.3 or higher. If you are still using 1.8.7 or 1.9.2, please use the v0.3.0 release.

Installation

Include the gem using bundler in your Gemfile:

gem "validates_lengths_from_database"

Usage

In your model you can activate validations:

class Post < ActiveRecord::Base
  validates_lengths_from_database
end

It also supports filter-style :only and :except options:

class Post < ActiveRecord::Base
  validates_lengths_from_database :only => [:title, :contents]
end

class Post < ActiveRecord::Base
  validates_lengths_from_database :except => [:other_field]
end

If you'd rather specify the limits explicitly instead of pulling them from the database, you can set it:

class Post < ActiveRecord::Base
  validates_lengths_from_database :limit => 255
end

Or to set the limit differently for string (VARCHAR) and (TEXT) columns:

class Post < ActiveRecord::Base
  validates_lengths_from_database :limit => {:string => 255, :text => 4092}
end

Since v0.5.2, it's possible to use the `validate_lengths_from_database` on an abstract parent (without a table), such as ApplicationRecord or ActiveRecord::Base.

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  validates_lengths_from_database :limit => 255
end

# This will use the validations with default options (from parent)
class Post < ApplicationRecord
end

# This will use the validations, with overriding the default options (the limit will not be applied)
class User < ApplicationRecord
  validates_lengths_from_database :only => [:name]
end

# This will use the validations, using the default options + adding some class specific customizations
class Comment < ApplicationRecord
  validate_lengths_from_database_options[:only] << :name
end

Running Tests

This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested.

# Just the gems locked in Gemfile.lock
$ rake test

# All of the Appraisals:
$ appraisal install
$ appraisal rake test