database_validation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module DatabaseValidation
2
+
3
+ def self.version
4
+ VERSION.join('.')
5
+ end
6
+
7
+ private
8
+
9
+ VERSION = [0, 0, 1]
10
+
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_record'
2
+
3
+ module DatabaseValidation
4
+
5
+ def self.included(base)
6
+ @base = base
7
+ Detector.detect_validations(@base)
8
+ end
9
+
10
+ # tested with 'mysql' and 'mysql2' adapters
11
+ module Detector
12
+
13
+ def self.detect_validations(base)
14
+ base.columns.each do |field|
15
+ next if field.name == base.primary_key # don't add validations here
16
+ allow_nil = field.null
17
+ base.send(:validates_length_of, field.name.to_sym, :maximum => field.limit, :allow_nil => allow_nil)
18
+ base.send(:validates_presence_of, field.name.to_sym, :allow_nil => allow_nil) unless allow_nil
19
+ end
20
+ ActiveRecord::Base.connection.indexes(base.table_name).each do |key|
21
+ col_syms = key.columns.map(&:to_sym)
22
+ base.send(:validates_uniqueness_of, col_syms.first.to_sym, :scope => col_syms.slice(1, -1)) if key.unique
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/../lib/database_validation'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'johnsql')
4
+
5
+ def execute(sql)
6
+ ActiveRecord::Base.connection.execute(sql)
7
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_validation
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - John Crepezzi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-15 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: Automatically pull validations out of your database and apply them to your models
34
+ email: john.crepezzi@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/database_validation/version.rb
43
+ - lib/database_validation.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://seejohnrun.github.com/database_validation/
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: database_validation
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Automatic Rails validations from your database
77
+ test_files:
78
+ - spec/spec_helper.rb