activerecord-databasevalidations 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +4 -0
- data/Gemfile.activerecord40 +4 -0
- data/Gemfile.activerecord41 +4 -0
- data/activerecord-databasevalidations.gemspec +2 -2
- data/lib/active_record/database_validations/version.rb +1 -1
- data/lib/active_record/validations/database_constraints.rb +9 -10
- data/test/database_constraints_validator_test.rb +5 -5
- data/test/test_helper.rb +2 -0
- metadata +22 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 759df661c99bd799d77eeeb2917d9d892d22de04
|
4
|
+
data.tar.gz: d2880441b78e568c1b7f6649158786dd84ceee1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65b73884f526c3bda96b3ea7e4a56ba9c520ad26d33cbefd70dd59e3244ad6963dac22ff82c86f7449f58f72b0cfcfde0b798ca34f3905be747bce56f74925ac
|
7
|
+
data.tar.gz: 6a54b91564ece55439ce503e46c963a22090e14c170e29528bd1dc5fce0802d66b8ba36e908e7be2b8cc96e54f5da0520a10f680ff5876a5038d6a20a0d4640c
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "activerecord", "~> 4"
|
21
|
+
spec.add_runtime_dependency "activerecord", "~> 4.0"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.5"
|
24
24
|
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_development_dependency "minitest"
|
26
26
|
spec.add_development_dependency "mysql2"
|
27
27
|
end
|
@@ -21,12 +21,11 @@ module ActiveRecord
|
|
21
21
|
longblob: { validator: ActiveModel::Validations::BytesizeValidator, default_maximum: 2 ** 32 - 1 },
|
22
22
|
}
|
23
23
|
|
24
|
-
attr_reader :
|
24
|
+
attr_reader :constraints
|
25
25
|
|
26
26
|
VALID_CONSTRAINTS = Set[:size, :basic_multilingual_plane, :not_null]
|
27
27
|
|
28
28
|
def initialize(options = {})
|
29
|
-
@klass = options[:class]
|
30
29
|
@constraints = Set.new(Array.wrap(options[:in]) + Array.wrap(options[:with]))
|
31
30
|
@constraint_validators = {}
|
32
31
|
super
|
@@ -39,14 +38,14 @@ module ActiveRecord
|
|
39
38
|
raise ArgumentError, "#{invalid_constraints.map(&:inspect).join(',')} is not a valid constraint." unless invalid_constraints.empty?
|
40
39
|
end
|
41
40
|
|
42
|
-
def not_null_validator(column)
|
41
|
+
def not_null_validator(klass, column)
|
43
42
|
return unless constraints.include?(:not_null)
|
44
43
|
return if column.null
|
45
44
|
|
46
45
|
ActiveModel::Validations::NotNullValidator.new(attributes: [column.name.to_sym], class: klass)
|
47
46
|
end
|
48
47
|
|
49
|
-
def size_validator(column)
|
48
|
+
def size_validator(klass, column)
|
50
49
|
return unless constraints.include?(:size)
|
51
50
|
return unless column.text? || column.binary?
|
52
51
|
|
@@ -61,26 +60,26 @@ module ActiveRecord
|
|
61
60
|
end
|
62
61
|
end
|
63
62
|
|
64
|
-
def basic_multilingual_plane_validator(column)
|
63
|
+
def basic_multilingual_plane_validator(klass, column)
|
65
64
|
return unless constraints.include?(:basic_multilingual_plane)
|
66
65
|
return unless column.text? && column.collation =~ /\Autf8(?:mb3)?_/
|
67
66
|
ActiveModel::Validations::BasicMultilingualPlaneValidator.new(attributes: [column.name.to_sym], class: klass)
|
68
67
|
end
|
69
68
|
|
70
|
-
def attribute_validators(attribute)
|
69
|
+
def attribute_validators(klass, attribute)
|
71
70
|
@constraint_validators[attribute] ||= begin
|
72
71
|
column = klass.columns_hash[attribute.to_s] or raise ArgumentError.new("Model #{self.class.name} does not have column #{column_name}!")
|
73
72
|
|
74
73
|
[
|
75
|
-
not_null_validator(column),
|
76
|
-
size_validator(column),
|
77
|
-
basic_multilingual_plane_validator(column),
|
74
|
+
not_null_validator(klass, column),
|
75
|
+
size_validator(klass, column),
|
76
|
+
basic_multilingual_plane_validator(klass, column),
|
78
77
|
].compact
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
82
81
|
def validate_each(record, attribute, value)
|
83
|
-
attribute_validators(attribute).each do |validator|
|
82
|
+
attribute_validators(record.class, attribute).each do |validator|
|
84
83
|
validator.validate_each(record, attribute, value)
|
85
84
|
end
|
86
85
|
end
|
@@ -49,14 +49,14 @@ class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
49
49
|
|
50
50
|
def test_not_null_field_defines_not_null_validator_if_requested
|
51
51
|
validator = Foo._validators[:checked].first
|
52
|
-
subvalidators = validator.attribute_validators(:checked)
|
52
|
+
subvalidators = validator.attribute_validators(Foo, :checked)
|
53
53
|
assert_equal 1, subvalidators.length
|
54
54
|
assert_kind_of ActiveModel::Validations::NotNullValidator, subvalidators.first
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_string_field_defines_length_validator_by_default
|
58
58
|
validator = Foo._validators[:string].first
|
59
|
-
subvalidators = validator.attribute_validators(:string)
|
59
|
+
subvalidators = validator.attribute_validators(Foo, :string)
|
60
60
|
assert_equal 1, subvalidators.length
|
61
61
|
assert_kind_of ActiveModel::Validations::LengthValidator, subvalidators.first
|
62
62
|
assert_equal 40, subvalidators.first.options[:maximum]
|
@@ -64,7 +64,7 @@ class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
64
64
|
|
65
65
|
def test_blob_field_defines_bytesize_validator
|
66
66
|
validator = Foo._validators[:blob].first
|
67
|
-
subvalidators = validator.attribute_validators(:blob)
|
67
|
+
subvalidators = validator.attribute_validators(Foo, :blob)
|
68
68
|
assert_equal 1, subvalidators.length
|
69
69
|
assert_kind_of ActiveModel::Validations::BytesizeValidator, subvalidators.first
|
70
70
|
assert_equal 65535, subvalidators.first.options[:maximum]
|
@@ -73,7 +73,7 @@ class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
73
73
|
|
74
74
|
def test_not_null_text_field_defines_requested_bytesize_validator_and_unicode_validator
|
75
75
|
validator = Foo._validators[:not_null_text].first
|
76
|
-
subvalidators = validator.attribute_validators(:not_null_text)
|
76
|
+
subvalidators = validator.attribute_validators(Foo, :not_null_text)
|
77
77
|
assert_equal 2, subvalidators.length
|
78
78
|
|
79
79
|
assert_kind_of ActiveModel::Validations::BytesizeValidator, subvalidators.first
|
@@ -90,7 +90,7 @@ class DatabaseConstraintsValidatorTest < Minitest::Test
|
|
90
90
|
|
91
91
|
def test_should_not_create_a_validor_for_a_utf8mb4_field
|
92
92
|
assert Bar.new(mb4_string: '💩').valid?
|
93
|
-
Bar._validators[:mb4_string].first.attribute_validators(:mb4_string).empty?
|
93
|
+
Bar._validators[:mb4_string].first.attribute_validators(Bar, :mb4_string).empty?
|
94
94
|
end
|
95
95
|
|
96
96
|
def test_error_messages
|
data/test/test_helper.rb
CHANGED
@@ -9,6 +9,8 @@ require "yaml"
|
|
9
9
|
|
10
10
|
require "active_record/database_validations"
|
11
11
|
|
12
|
+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(MiniTest::Test)
|
13
|
+
|
12
14
|
database_yml = YAML.load_file(File.expand_path('../database.yml', __FILE__))
|
13
15
|
ActiveRecord::Base.establish_connection(database_yml['test'])
|
14
16
|
I18n.enforce_available_locales = false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-databasevalidations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Willem van Bergen
|
@@ -14,70 +14,70 @@ dependencies:
|
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '4'
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '4'
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: mysql2
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description: Opt-in validations for your ActiveRecord models based on your MySQL database
|
@@ -88,9 +88,11 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
-
|
92
|
-
-
|
91
|
+
- .gitignore
|
92
|
+
- .travis.yml
|
93
93
|
- Gemfile
|
94
|
+
- Gemfile.activerecord40
|
95
|
+
- Gemfile.activerecord41
|
94
96
|
- LICENSE.txt
|
95
97
|
- README.md
|
96
98
|
- Rakefile
|
@@ -121,17 +123,17 @@ require_paths:
|
|
121
123
|
- lib
|
122
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
123
125
|
requirements:
|
124
|
-
- -
|
126
|
+
- - '>='
|
125
127
|
- !ruby/object:Gem::Version
|
126
128
|
version: '0'
|
127
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
130
|
requirements:
|
129
|
-
- -
|
131
|
+
- - '>='
|
130
132
|
- !ruby/object:Gem::Version
|
131
133
|
version: '0'
|
132
134
|
requirements: []
|
133
135
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.0.14
|
135
137
|
signing_key:
|
136
138
|
specification_version: 4
|
137
139
|
summary: Add validations to your ActiveRecord models based on MySQL database constraints.
|