validates_by_schema 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Validates By Schema (validates_by_schema)
2
+ [![Build Status](https://secure.travis-ci.org/joshwlewis/validates_by_schema.png)](http://travis-ci.org/joshwlewis/validates_by_schema)
3
+
4
+ Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
5
+
6
+ ## Usage
7
+
8
+ 1. Add it to your Gemfile:
9
+ ```ruby
10
+ gem "validates_by_schema", :git => git://github.com/joshwlewis/validates_by_schema.git
11
+ ```
12
+
13
+ 2. Then `bundle`
14
+
15
+ 3. Add it to your ActiveRecord model:
16
+ ```ruby
17
+ class Widget < ActiveRecord::Base
18
+ validates_from_schema
19
+ end
20
+ ```
21
+
22
+ ## Example
23
+
24
+ Say you had a table setup like this:
25
+ ```ruby
26
+ create_table "widgets", :force => true do |t|
27
+ t.integer "wheel_count", :null => false
28
+ t.decimal "thickness", :precision => 4, :scale => 4
29
+ t.string "color"
30
+ end
31
+ ```
32
+
33
+ Then these validations run when your model `validates_from_schema`:
34
+ ```ruby
35
+ validates :wheel_count, presence: true, numericality {allow_nil: false, less_than: 2147483647}
36
+ validates :thickness, numericality: {allow_nil: true, less_than: 0.999, greater_than: -0.999}
37
+ validates :color, length: {allow_nil: true, maximum: 255}
38
+ ```
39
+
40
+ This should support any database supported by Rails. I have only tested MySQL up to this point.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ValidatesBySchema'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validates_by_schema do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,53 @@
1
+ module ValidatesBySchema
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def validates_by_schema options={}
6
+ return unless table_exists?
7
+ # Don't auto validated primary keys or timestamps
8
+ columns = self.columns.reject(&:primary)
9
+ columns.reject!{|c| ['updated_at', 'created_at'].include? c.name}
10
+
11
+ # Allow user to specify :only or :except options
12
+ except = options.delete(:except).try(:map, &:to_s)
13
+ only = options.delete(:only).try(:map, &:to_s)
14
+ if only.present?
15
+ columns.select!{|c| only.include? c.name}
16
+ elsif except.present?
17
+ columns.reject!{|c| except.include? c.name}
18
+ end
19
+
20
+ columns.each do |c|
21
+ case c.type
22
+ when :integer
23
+ val_hash = {presence: !c.null, numericality: {
24
+ only_integer: true, allow_nil: c.null}}
25
+ if c.limit
26
+ max = (2 ** (8 * c.limit)) / 2
27
+ val_hash[:numericality][:less_than] = max
28
+ val_hash[:numericality][:greater_than] = -max
29
+ end
30
+ validates c.name, val_hash
31
+ when :decimal
32
+ maximum = 10.0**(c.precision-c.scale) - 10.0**(-c.scale)
33
+ validates c.name, presence: !c.null, numericality: {
34
+ allow_nil: c.null, greater_than_or_equal_to: -maximum,
35
+ less_than_or_equal_to: maximum }
36
+ when :float
37
+ validates c.name, presence: !c.null,
38
+ numericality: { allow_nil: c.null }
39
+ when :string, :text
40
+ validates c.name, presence: !c.null, length: { maximum: c.limit.to_i,
41
+ allow_nil: c.null}
42
+ when :boolean
43
+ validates c.name, inclusion: { in: [true, false],
44
+ allow_nil: c.null }
45
+ when :date, :datetime, :time
46
+ validates c.name, presence: !c.null
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ ActiveRecord::Base.send(:include, ValidatesBySchema)
@@ -0,0 +1,3 @@
1
+ module ValidatesBySchema
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_by_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda-matchers
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Validate based on database column attributes! Automagically validate
79
+ presence, length, numericality, inclusion, and timeliness of ActiveRecord columns.
80
+ email:
81
+ - josh.w.lewis@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - lib/tasks/validates_by_schema_tasks.rake
87
+ - lib/validates_by_schema/version.rb
88
+ - lib/validates_by_schema.rb
89
+ - MIT-LICENSE
90
+ - Rakefile
91
+ - README.md
92
+ homepage: http://www.emergentcoils.com
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.23
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Default validation for ActiveRecord based on database column attributes
116
+ test_files: []
117
+ has_rdoc: