enforce_schema_rules 0.0.17 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +61 -0
- data/lib/enforce_schema_rules.rb +11 -8
- metadata +16 -15
- data/README +0 -81
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c173ca802197b5ae0e7d9eebbcf1490dc4c9b07
|
4
|
+
data.tar.gz: 973c060193c583aaa837e63883d893293bc2fbaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da1b3f98f2b17b9572286ff3a4317e1566023d0dcbea359375633a731e8d3f02d8566846c8c3c3f61f264f785aec8b485646593c337f9d3592dcdda174fd45c6
|
7
|
+
data.tar.gz: ed6229f742815bd36c603b9c685b59dc2de59533a84fef54a599adf854b8a944d40b4f8ece0fc4587a690b209bc5309be4140ba233567de717742e881eefdefe
|
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= Enforce Schema Rules
|
2
|
+
|
3
|
+
This plugin provides a way to validate your model against database rules
|
4
|
+
you've already created in your schema, including column lengths for
|
5
|
+
strings, "not null" designations, unique index constraints, and
|
6
|
+
ensuring integer input for integer columns.
|
7
|
+
|
8
|
+
The four methods currently supported are:
|
9
|
+
|
10
|
+
enforce_column_limits
|
11
|
+
enforce_integer_columns
|
12
|
+
enforce_not_null
|
13
|
+
enforce_unique_indexes
|
14
|
+
|
15
|
+
You can also just call <tt>enforce_schema_rules</tt> to enforce all the
|
16
|
+
above.
|
17
|
+
|
18
|
+
Accepts the relevant validates_length_of options (i.e. :on and :message)
|
19
|
+
and these assume the usual defaults.
|
20
|
+
|
21
|
+
In addition to the regular options, you can also have an
|
22
|
+
<tt>:except</tt> list enumerating the column that you don't want to
|
23
|
+
validate.
|
24
|
+
|
25
|
+
By default, magic columns (_at, _on, _id, id, position, etc) are
|
26
|
+
skipped. If you'd like to override that behavior, you can define your
|
27
|
+
own <tt>:exclusion_regexp</tt>
|
28
|
+
|
29
|
+
Examples:
|
30
|
+
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
enforce_schema_rules :except => :dhh
|
33
|
+
end
|
34
|
+
|
35
|
+
class Book < ActiveRecord::Base
|
36
|
+
enforce_column_limits :message => "exceeds the %d character limit", :on => :update
|
37
|
+
enforce_unique_indexes
|
38
|
+
enforce_not_null :exclusion_regexp => /$fk_/
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
== Download
|
43
|
+
|
44
|
+
Development is done at:
|
45
|
+
|
46
|
+
http://github.com/twinge/enforce_schema_rules
|
47
|
+
|
48
|
+
You can install as a gem with:
|
49
|
+
|
50
|
+
gem install enforce_schema_rules
|
51
|
+
|
52
|
+
== Bugs & feedback
|
53
|
+
|
54
|
+
Please send bug reports, patches and feedback to the GitHub project.
|
55
|
+
|
56
|
+
== Credit
|
57
|
+
|
58
|
+
The plugin was written by Josh Starcher <josh.starcher@gmail.com>. It
|
59
|
+
was updated to Rails 3 by Eric Anderson <eric@pixelwareinc.com>. This
|
60
|
+
plugin is basically an extension of David Easley's enforce-limits
|
61
|
+
plugin. Michael Schuerig provided contributed a patch and a style lesson.
|
data/lib/enforce_schema_rules.rb
CHANGED
@@ -18,7 +18,7 @@ module Jls
|
|
18
18
|
enforce_not_null(options.dup)
|
19
19
|
enforce_unique_indexes(options.dup)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
# Enforce string column limits
|
23
23
|
def enforce_column_limits(options = {})
|
24
24
|
args = build_validation_args(options, :string, :too_long)
|
@@ -27,12 +27,15 @@ module Jls
|
|
27
27
|
validates_each(*args) do |record, attr, value|
|
28
28
|
limit = record.class.columns_hash[attr.to_s].limit
|
29
29
|
if limit
|
30
|
-
message =
|
30
|
+
message = case
|
31
|
+
when options[:message].is_a?(String) then options[:message] % {count: limit}
|
32
|
+
when limit == 1 then options[:message][:one]
|
33
|
+
else options[:message][:other] % {:count => limit} end
|
31
34
|
record.errors.add(attr, message) unless value.nil? || value.size <= limit
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
35
|
-
|
38
|
+
|
36
39
|
# Enforce numericality of integer columns
|
37
40
|
def enforce_integer_columns(options = {})
|
38
41
|
# first get the non-integers
|
@@ -44,14 +47,14 @@ module Jls
|
|
44
47
|
args = build_validation_args(options, :integer, :not_a_number)
|
45
48
|
validates_numericality_of(*args) unless args.first.is_a? Hash
|
46
49
|
end
|
47
|
-
|
50
|
+
|
48
51
|
# Enfore "not null" columns settings
|
49
52
|
def enforce_not_null(options = {})
|
50
53
|
args = build_validation_args(options, :not_null, :blank)
|
51
54
|
return if args.first.is_a?(Hash)
|
52
55
|
validates_presence_of(*args)
|
53
56
|
end
|
54
|
-
|
57
|
+
|
55
58
|
# Enfore unique indexes
|
56
59
|
def enforce_unique_indexes(options = {})
|
57
60
|
attrs = build_validation_args(options, false, :taken)
|
@@ -61,7 +64,7 @@ module Jls
|
|
61
64
|
validates_uniqueness_of(index.columns.first, options)
|
62
65
|
end
|
63
66
|
end
|
64
|
-
|
67
|
+
|
65
68
|
def build_validation_args(options, col_type, validation_option = :invalid)
|
66
69
|
options[validation_option] = I18n.translate('errors.messages')[validation_option]
|
67
70
|
options[:message] ||= options[validation_option]
|
@@ -71,10 +74,10 @@ module Jls
|
|
71
74
|
when :numeric
|
72
75
|
lambda { |col| col.name !~ exclusion_regexp && col.number? && col.type != :integer }
|
73
76
|
when :not_null
|
74
|
-
# I have to exclude boolean types because of a "feature" of the way validates_presence_of
|
77
|
+
# I have to exclude boolean types because of a "feature" of the way validates_presence_of
|
75
78
|
# handles boolean fields
|
76
79
|
# See http://dev.rubyonrails.org/ticket/5090 and http://dev.rubyonrails.org/ticket/3334
|
77
|
-
lambda { |col|
|
80
|
+
lambda { |col| col.name !~ exclusion_regexp && !col.null && col.type != :boolean }
|
78
81
|
else
|
79
82
|
lambda { |col| col.name !~ exclusion_regexp && col_type == col.type }
|
80
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enforce_schema_rules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Starcher
|
@@ -11,48 +11,48 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: sqlite3
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
requirements:
|
34
|
-
- -
|
34
|
+
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
36
|
version: '0'
|
37
37
|
type: :development
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '0'
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
45
|
name: test-unit
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
|
-
- -
|
48
|
+
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
type: :development
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
|
-
- -
|
55
|
+
- - ">="
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
description: |2
|
@@ -68,33 +68,34 @@ email:
|
|
68
68
|
executables: []
|
69
69
|
extensions: []
|
70
70
|
extra_rdoc_files:
|
71
|
-
- README
|
71
|
+
- README.rdoc
|
72
72
|
files:
|
73
|
+
- README.rdoc
|
73
74
|
- lib/enforce_schema_rules.rb
|
74
|
-
- README
|
75
75
|
homepage:
|
76
76
|
licenses: []
|
77
77
|
metadata: {}
|
78
78
|
post_install_message:
|
79
79
|
rdoc_options:
|
80
|
-
- --main
|
81
|
-
- README
|
80
|
+
- "--main"
|
81
|
+
- README.rdoc
|
82
82
|
require_paths:
|
83
83
|
- lib
|
84
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- -
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
|
-
- -
|
91
|
+
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: An ActiveRecord plugin to automatically enforce database contraints
|
100
100
|
test_files: []
|
101
|
+
has_rdoc:
|
data/README
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
Enforce Schema Rules
|
2
|
-
=====================
|
3
|
-
|
4
|
-
This plugin provides a way to validate your model against database rules
|
5
|
-
you've already created in your schema, including column lengths for
|
6
|
-
strings, "not null" designations, unique index constraints, and
|
7
|
-
ensuring integer input for integer columns.
|
8
|
-
|
9
|
-
The four methods currently supported are:
|
10
|
-
|
11
|
-
enforce_column_limits
|
12
|
-
enforce_integer_columns
|
13
|
-
enforce_not_null
|
14
|
-
enforce_unique_indexes
|
15
|
-
|
16
|
-
You can also just call <tt>enforce_schema_rules</tt> to enforce all the
|
17
|
-
above.
|
18
|
-
|
19
|
-
Accepts the relevant validates_length_of options (i.e. :on and :message)
|
20
|
-
and these assume the usual defaults.
|
21
|
-
|
22
|
-
In addition to the regular options, you can also have an
|
23
|
-
<tt>:except</tt> list enumerating the column that you don't want to
|
24
|
-
validate.
|
25
|
-
|
26
|
-
By default, magic columns (_at, _on, _id, id, position, etc) are
|
27
|
-
skipped. If you'd like to override that behavior, you can define your
|
28
|
-
own <tt>:exclusion_regexp</tt>
|
29
|
-
|
30
|
-
Examples:
|
31
|
-
|
32
|
-
class Person < ActiveRecord::Base
|
33
|
-
enforce_schema_rules :except => :dhh
|
34
|
-
end
|
35
|
-
|
36
|
-
class Book < ActiveRecord::Base
|
37
|
-
enforce_column_limits :message => "exceeds the %d character limit", :on => :update
|
38
|
-
enforce_unique_indexes
|
39
|
-
enforce_not_null :exclusion_regexp => /$fk_/
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
|
-
Download
|
44
|
-
--------
|
45
|
-
|
46
|
-
Development is done at:
|
47
|
-
|
48
|
-
http://github.com/twinge/enforce_schema_rules
|
49
|
-
|
50
|
-
You can install as a gem with:
|
51
|
-
|
52
|
-
gem install enforce_schema_rules
|
53
|
-
|
54
|
-
Bugs & feedback
|
55
|
-
---------------
|
56
|
-
Please send bug reports, patches and feedback to the GitHub project.
|
57
|
-
|
58
|
-
Todo
|
59
|
-
----
|
60
|
-
The main goal was recent updates was to get it to work in Rails 3. Some
|
61
|
-
cleanup was just a side-effect. If anybody is interested, additional
|
62
|
-
cleanup items include:
|
63
|
-
|
64
|
-
* Make sure we don't need better test coverage. The upgrades to
|
65
|
-
Rails 3 caused some test fixes but I am not sure we have coverage so
|
66
|
-
a good eye towards that would be good.
|
67
|
-
* The smallest number of changes were done to get things working under
|
68
|
-
Rails 3. This plugin was written a while back. It might could be
|
69
|
-
refactored using some new goodness that comes with a more recent
|
70
|
-
ActiveRecord/Rails implementation.
|
71
|
-
* I most likely dropped support for Rails 2.x. The old version works
|
72
|
-
fine for those still on Rails 2.x so I say we just drop Rails 2.x
|
73
|
-
support (there are some warnings but they can be fixed fairly easily
|
74
|
-
and don't need to be fixed).
|
75
|
-
|
76
|
-
Credit
|
77
|
-
------
|
78
|
-
The plugin was written by Josh Starcher <josh.starcher@gmail.com>. It
|
79
|
-
was updated to Rails 3 by Eric Anderson <eric@pixelwareinc.com>. This
|
80
|
-
plugin is basically an extension of David Easley's enforce-limits
|
81
|
-
plugin. Michael Schuerig provided contributed a patch and a syle lesson.
|