Giter Club home page Giter Club logo

validates_lengths_from_database's Introduction

<img src=“https://secure.travis-ci.org/rubiety/validates_lengths_from_database.png?branch=master” alt=“Build Status” /> <img src=“https://gemnasium.com/rubiety/validates_lengths_from_database.png” alt=“Dependency Status” /> <img src=“https://codeclimate.com/github/rubiety/validates_lengths_from_database.png” />

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

Note that unfortunately this cannot be done at a global level directly against ActiveRecord::Base, since the validates_length_from_database method requires the class to have a table name (with the ability to load the schema).

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

validates_lengths_from_database's People

Contributors

damiong28 avatar domcleal avatar rubiety avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.