Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/markbates/validates_schema
Adds ActiveRecord validations based on the DB schema
https://github.com/markbates/validates_schema
Last synced: 2 months ago
JSON representation
Adds ActiveRecord validations based on the DB schema
- Host: GitHub
- URL: https://github.com/markbates/validates_schema
- Owner: markbates
- License: mit
- Created: 2011-01-03T19:55:59.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2011-01-04T17:34:29.000Z (almost 14 years ago)
- Last Synced: 2024-05-02T06:06:50.431Z (8 months ago)
- Language: Ruby
- Homepage: http://www.metabates.com
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
- License: LICENSE
Awesome Lists containing this project
README
=Validates Schema
Add a few validations for ActiveRecord objects (ActiveRecord 3.0+) based off of the schema for each table.
==Installation
Add the following to your
Gemfile
:gem 'validates_schema'
Then install the gem:
$ bundle install
That's it! There is nothing else you need to do to get validations on your ActiveRecord models. When each of your models inherits ActiveRecord::Base it will automatically look up the schema for that model and generate the appropriate validations based on the schema.
==Examples
The following schema:
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.string :name, :limit => 200
t.string :email
t.string :password, :null => false
t.string :url, :limit => 128, :null => false
t.integer :age
t.integer :pin, :limit => 8, :null => false
t.float :salt
t.text :bio
t.text :summary, :null => false
t.boolean :alive, :null => false
t.decimal :salary, :null => false, :precision => 2, :scale => 8
t.timestamps
endend
will generate the following validations:
class User < ActiveRecord::Base
validates :name, :length => {:maximum => 200}
validates :email, :length => {:maximum => 255}
validates :password, :length => {:maximum => 255}, :presence => true
validates :url, :length => {:maximum => 128}, :presence => true
validates :age, :numericality => {:only_integer => true}
validates :pin, :numericality => {:only_integer => true}, :presence=>true
validates :salt, :numericality => true
validates :summary, :presence => true
validates :alive, :presence => true
validates :salary, :numericality => true, :presence => true
end