robworley-schema-validations 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +13 -0
- data/lib/schema_validations.rb +89 -0
- data/schema_validations.gemspec +18 -0
- metadata +71 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Rob Worley
|
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.rdoc
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
= Schema Validations
|
2
|
+
|
3
|
+
DRY up your ActiveRecord validations based on schema information.
|
4
|
+
|
5
|
+
ActiveRecord::Base.send(:include, SchemaValidations)
|
6
|
+
|
7
|
+
Use skip_schema_validations when you need to circumvent validation of specific attributes. This is particularly useful when a field that does not permit nulls will be populated after validation.
|
8
|
+
|
9
|
+
class User < ActiveRecord::Base
|
10
|
+
skip_schema_validations :crypted_password
|
11
|
+
end
|
12
|
+
|
13
|
+
Much of this code was plagiarized from Simon Harris' Redhill on Rails work. I simply fixed it up for Rails 2.3.x, added test coverage and removed some of the more problematic validations for uniqueness, precision, scale etc. It's not quite as comprehensive but it's a useful foundation to build upon.
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module SchemaValidations
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def self.extended(base)
|
8
|
+
base.send(:class_inheritable_array, :schema_validations_to_skip)
|
9
|
+
base.send(:schema_validations_to_skip=, [])
|
10
|
+
|
11
|
+
class << base
|
12
|
+
alias_method_chain :allocate, :schema_validations
|
13
|
+
alias_method_chain :new, :schema_validations
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def skip_schema_validations(*syms)
|
18
|
+
self.schema_validations_to_skip = syms
|
19
|
+
end
|
20
|
+
|
21
|
+
def allocate_with_schema_validations
|
22
|
+
load_schema_validations
|
23
|
+
allocate_without_schema_validations
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_with_schema_validations(*args)
|
27
|
+
load_schema_validations
|
28
|
+
new_without_schema_validations(*args) { |*block_args| yield(*block_args) if block_given? }
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def load_schema_validations
|
34
|
+
# Don't bother if: it's already been loaded; the table doesn't exist; the class is abstract
|
35
|
+
return if @schema_validations_loaded || !table_exists? || abstract_class?
|
36
|
+
@schema_validations_loaded = true
|
37
|
+
load_column_validations
|
38
|
+
load_association_validations
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def load_column_validations
|
44
|
+
content_columns.each do |column|
|
45
|
+
next unless validates?(column)
|
46
|
+
|
47
|
+
name = column.name.to_sym
|
48
|
+
|
49
|
+
# Data-type validation
|
50
|
+
if column.type == :integer
|
51
|
+
validates_numericality_of name, :allow_nil => true, :only_integer => true
|
52
|
+
elsif column.number?
|
53
|
+
validates_numericality_of name, :allow_nil => true
|
54
|
+
elsif column.text? && column.limit
|
55
|
+
validates_length_of name, :allow_nil => true, :maximum => column.limit
|
56
|
+
end
|
57
|
+
|
58
|
+
# NOT NULL constraints
|
59
|
+
unless column.null
|
60
|
+
# Work-around for a "feature" of the way validates_presence_of handles boolean fields
|
61
|
+
# See http://dev.rubyonrails.org/ticket/5090 and http://dev.rubyonrails.org/ticket/3334
|
62
|
+
if column.type == :boolean
|
63
|
+
validates_inclusion_of name, :in => [true, false], :message => I18n.translate('activerecord.errors.messages.blank')
|
64
|
+
else
|
65
|
+
validates_presence_of name
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def load_association_validations
|
72
|
+
columns = columns_hash
|
73
|
+
reflect_on_all_associations(:belongs_to).each do |association|
|
74
|
+
column = columns[association.primary_key_name]
|
75
|
+
next unless validates?(column)
|
76
|
+
|
77
|
+
# NOT NULL constraints
|
78
|
+
module_eval(
|
79
|
+
"validates_presence_of :#{column.name}, :if => lambda { |record| record.#{association.name}.nil? }"
|
80
|
+
) unless column.null
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def validates?(column)
|
85
|
+
column && column.name !~ /^(((created|updated)_(at|on))|position)$/ &&
|
86
|
+
!self.schema_validations_to_skip.include?(column.name.to_sym)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "robworley-schema-validations"
|
3
|
+
s.version = "1.0.1"
|
4
|
+
s.date = "2011-02-15"
|
5
|
+
s.summary = "DRY up your ActiveRecord validations based on schema information."
|
6
|
+
s.email = "robert.worley@gmail.com"
|
7
|
+
s.homepage = "http://github.com/robworley/schema-validations"
|
8
|
+
s.description = "Validate ActiveRecord models based your database schema."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Rob Worley"]
|
11
|
+
s.files = ["MIT-LICENSE",
|
12
|
+
"README.rdoc",
|
13
|
+
"schema_validations.gemspec",
|
14
|
+
"lib/schema_validations.rb"
|
15
|
+
]
|
16
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
17
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: robworley-schema-validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Worley
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-15 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Validate ActiveRecord models based your database schema.
|
23
|
+
email: robert.worley@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- schema_validations.gemspec
|
34
|
+
- lib/schema_validations.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/robworley/schema-validations
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --main
|
42
|
+
- README.rdoc
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.5.2
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: DRY up your ActiveRecord validations based on schema information.
|
70
|
+
test_files: []
|
71
|
+
|