validates_schema 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 markbates
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.
data/README ADDED
@@ -0,0 +1,53 @@
1
+ =Validates Schema
2
+
3
+ Add a few validations for ActiveRecord objects (ActiveRecord 3.0+) based off of the schema for each table.
4
+
5
+ ==Installation
6
+
7
+ Add the following to your <code>Gemfile</code>:
8
+
9
+ gem 'validates_schema'
10
+
11
+ Then install the gem:
12
+
13
+ $ bundle install
14
+
15
+ 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.
16
+
17
+ ==Examples
18
+
19
+ The following schema:
20
+
21
+ ActiveRecord::Schema.define do
22
+
23
+ create_table :users, :force => true do |t|
24
+ t.string :name, :limit => 200
25
+ t.string :email
26
+ t.string :password, :null => false
27
+ t.string :url, :limit => 128, :null => false
28
+ t.integer :age
29
+ t.integer :pin, :limit => 8, :null => false
30
+ t.float :salt
31
+ t.text :bio
32
+ t.text :summary, :null => false
33
+ t.boolean :alive, :null => false
34
+ t.decimal :salary, :null => false, :precision => 2, :scale => 8
35
+ t.timestamps
36
+ end
37
+
38
+ end
39
+
40
+ will generate the following validations:
41
+
42
+ class User < ActiveRecord::Base
43
+ validates :name, :length => {:maximum => 200}
44
+ validates :email, :length => {:maximum => 255}
45
+ validates :password, :length => {:maximum => 255}, :presence => true
46
+ validates :url, :length => {:maximum => 128}, :presence => true
47
+ validates :age, :numericality => {:only_integer => true}
48
+ validates :pin, :numericality => {:only_integer => true}, :presence=>true
49
+ validates :salt, :numericality => true
50
+ validates :summary, :presence => true
51
+ validates :alive, :presence => true
52
+ validates :salary, :numericality => true, :presence => true
53
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+
3
+ Dir.glob(File.join(File.dirname(__FILE__), 'validates_schema', '**/*.rb')).each do |f|
4
+ require File.expand_path(f)
5
+ end
@@ -0,0 +1,33 @@
1
+ class ActiveRecord::Base
2
+
3
+ class << self
4
+
5
+ def inherited(klass)
6
+ super(klass)
7
+ # puts klass
8
+ klass.arel_table.columns.each do |c|
9
+ detail = c.column
10
+ next if detail.name == 'id'
11
+ options = {}
12
+ case detail.type
13
+ when :string, :text, :binary
14
+ options[:length] = { :maximum => detail.limit } if detail.limit.present?
15
+ when :integer, :float, :decimal
16
+ options[:numericality] = true
17
+ options[:numericality] = {:only_integer => true} if detail.type == :integer
18
+ end
19
+ options[:presence] = true unless detail.null
20
+
21
+ unless options.empty?
22
+ # puts "validates :#{detail.name.to_sym}, #{options.inspect}"
23
+ klass.class_eval do
24
+ validates detail.name.to_sym, options || {}
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_schema
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - markbates
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-04 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ version: 3.0.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ description: "validates_schema was developed by: markbates"
36
+ email: mark@markbates.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - LICENSE
44
+ files:
45
+ - lib/validates_schema/validates_schema.rb
46
+ - lib/validates_schema.rb
47
+ - README
48
+ - LICENSE
49
+ has_rdoc: true
50
+ homepage: http://www.metabates.com
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 1478591478662090228
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project: validates_schema
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: validates_schema
82
+ test_files: []
83
+