rails-schema-validations 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ add validations to your models automatically based on the schema with:
2
+
3
+ validations_from_schema
4
+
5
+ | schema type | rails validation |
6
+ |-------------|---------------------------|
7
+ | non-null | validates_presence_of |
8
+ | limit | validates_length_of |
9
+ | integer | validates_numericality_of |
10
+ | float | validates_numericality_of |
11
+
12
+
13
+ Tested on MySQL
@@ -0,0 +1,20 @@
1
+ # this will put it in the scope of a model
2
+ module ActiveRecord::Validations::ClassMethods
3
+ def validations_from_schema
4
+ self.content_columns.map do |col|
5
+ case col.type
6
+ when :integer
7
+ validates_numericality_of col.name, :only_integer => true, :allow_nil => col.null
8
+ when :float
9
+ validates_numericality_of col.name, :allow_nil => col.null
10
+ #when :time, :datetime :string
11
+ end
12
+ end.compact + content_columns.map do |col|
13
+ validates_presence_of col.name unless col.null
14
+ end.compact + content_columns.map do |col|
15
+ if col.limit
16
+ validates_length_of col.name, :maximum => col.limit, :allow_nil => col.null
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ #require 'rubygems'
4
+ require 'active_record'
5
+ require 'riot'
6
+ require 'riot/rails'
7
+
8
+ def __DIR__
9
+ File.dirname(__FILE__)
10
+ end
11
+ require File.join(__DIR__, '..', 'lib', 'rails-schema-validations')
@@ -0,0 +1,68 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ def load_schema
4
+ # sqlite doesn't support limits in the schema!
5
+ ActiveRecord::Base.establish_connection(
6
+ :adapter => "mysql",
7
+ :database => "validations_from_schema"
8
+ )
9
+
10
+ ActiveRecord::Schema.define do
11
+ create_table "models", :force => true do |t|
12
+ %w(string integer float datetime date).each do |kind|
13
+ t.send(kind, kind)
14
+ t.send(kind, "#{kind}_notnull", :null => false)
15
+ end
16
+ %w(string integer).each do |kind|
17
+ t.send(kind, "#{kind}_limit", :limit => 10)
18
+ t.send(kind, "#{kind}_limit_notnull", :limit => 10, :null => false)
19
+ end
20
+ t.timestamps
21
+ end
22
+ end
23
+ end
24
+
25
+ load_schema
26
+
27
+ class Model < ActiveRecord::Base
28
+ validations_from_schema
29
+ end
30
+
31
+ context Model do
32
+ setup { Model.new }
33
+
34
+ def asserts_error_on field, value
35
+ asserts("#{field} has an error with #{value}") do
36
+ topic.send("#{field}=", value)
37
+ topic.valid?
38
+ topic.errors[field.to_sym].present?
39
+ end
40
+ end
41
+
42
+ def asserts_no_error_on field, value
43
+ asserts("#{field} has no error with #{value}") do
44
+ topic.send("#{field}=", value)
45
+ topic.valid?
46
+ topic.errors[field.to_sym].blank?
47
+ end
48
+ end
49
+
50
+ # null
51
+ %w(string integer float date datetime).each do |name|
52
+ asserts_no_error_on name, nil
53
+ asserts_error_on "#{name}_notnull", nil
54
+ end
55
+ %w(string integer).each do |name|
56
+ asserts_no_error_on "#{name}_limit", nil
57
+ asserts_error_on "#{name}_limit_notnull", nil
58
+ end
59
+
60
+ # only_integer
61
+ asserts_error_on :integer_limit, 1.1
62
+
63
+ asserts_error_on :string_limit, '12345678910'
64
+ asserts_error_on :integer_limit, 12345678910
65
+
66
+ asserts_no_error_on :string_limit, '12'
67
+ asserts_no_error_on :integer_limit, 1
68
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-schema-validations
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ version: "0.4"
10
+ platform: ruby
11
+ authors:
12
+ - Greg Weber
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-24 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Automatically generate validations from the schema
22
+ email: greg@gregweber.info
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - README
31
+ - lib/rails-schema-validations.rb
32
+ - test/validations_from_schema_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/gregwebs/rails-schema-validations
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Automatically generate validations from the schema
68
+ test_files:
69
+ - test/validations_from_schema_test.rb
70
+ - test/test_helper.rb