schema_validations 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06905897e2968a424dd329db0dd07bfdc32625aa
4
- data.tar.gz: 351f0900d4f03138c76a3f19a4e83c4a02cf522d
3
+ metadata.gz: 8fe25cdf83ff0041992948fe1a400d40cd4e4ebc
4
+ data.tar.gz: 286fec9f821594ffe76bffdf1fd0b08c87320f18
5
5
  SHA512:
6
- metadata.gz: 84c4bae6e9d3931f74a817c05c272a82be472c41a147a579c3ea5bd80b53b76f7287afe2f8a7eff909d1d59c667c3fd38fb147c3031ec8c74c1ab9f460dcfdd5
7
- data.tar.gz: 3d39c05c232dd621b8ea04c80c018c108844cd5817725088b9ee300682863444ed7eb8b1c1fb7231116c62b6bbcaf32c487a537e14fec877c8bacec5b21ae0ab
6
+ metadata.gz: 7065a87c0ae2eec4d971423b6fa6d9f63c250e4e4c65f7fc3ee775d896408cc9e64fd704936b367d66206869fbc7874d4e326020171dbef15f365aa68d3bad86
7
+ data.tar.gz: 00c9fd2a04f6ba7f539595e717abbfb68db2c62fc59fa6a140266c069f09f57e31e1b167f1becef7d839cd1bc29bad66a4ae8977296c1c25525168e9acd0ef04
data/.gitignore CHANGED
@@ -17,6 +17,7 @@ tmtags
17
17
  coverage
18
18
  rdoc
19
19
  pkg
20
+ .byebug_history
20
21
 
21
22
  ## PROJECT::SPECIFIC
22
23
  .rvmrc
data/README.md CHANGED
@@ -185,6 +185,10 @@ Earlier versions of SchemaValidations supported:
185
185
 
186
186
  ## Release Notes
187
187
 
188
+ ### 2.0.2
189
+
190
+ * Use schema_monkey rather than Railties
191
+
188
192
  ### 2.0.1
189
193
 
190
194
  * Bug fix: Don't crash when optimistic locking is in use (#8)
@@ -1,3 +1,3 @@
1
1
  eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
2
 
3
- gem "activerecord", "~> 4.2.0"
3
+ gem "activerecord", "~> 4.2.6"
@@ -2,9 +2,9 @@ require "pathname"
2
2
  eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)
3
3
 
4
4
  platform :ruby do
5
- gem "mysql2"
5
+ gem "mysql2", '>= 0.3.18', '< 0.5'
6
6
  end
7
7
 
8
8
  platform :jruby do
9
9
  gem 'activerecord-jdbcmysql-adapter'
10
- end
10
+ end
@@ -1,216 +1,216 @@
1
1
  module SchemaValidations
2
2
  module ActiveRecord
3
- module Validations
3
+ module Base
4
4
 
5
- def self.extended(base) # :nodoc:
6
- base.class_eval do
7
- define_method :load_schema_validations do
8
- self.class.send :load_schema_validations
5
+ def load_schema_validations
6
+ self.class.send :load_schema_validations
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def self.extended(base)
12
+ base.class_eval do
13
+ class_attribute :schema_validations_loaded
9
14
  end
10
- class_attribute :schema_validations_loaded
11
15
  end
12
- class << base
13
- alias_method_chain :validators, :schema_validations
14
- alias_method_chain :validators_on, :schema_validations
15
- end if base.respond_to? :validators
16
- end
17
16
 
18
- def inherited(klass) # :nodoc:
19
- super
20
- before_validation :load_schema_validations unless schema_validations_loaded?
21
- end
17
+ def inherited(subclass) # :nodoc:
18
+ super
19
+ before_validation :load_schema_validations unless schema_validations_loaded?
20
+ end
22
21
 
23
- def validators_with_schema_validations
24
- load_schema_validations unless schema_validations_loaded?
25
- validators_without_schema_validations
26
- end
22
+ def validators
23
+ load_schema_validations unless schema_validations_loaded?
24
+ super
25
+ end
27
26
 
28
- def validators_on_with_schema_validations(*args)
29
- load_schema_validations unless schema_validations_loaded?
30
- validators_on_without_schema_validations(*args)
31
- end
27
+ def validators_on(*args)
28
+ load_schema_validations unless schema_validations_loaded?
29
+ super
30
+ end
32
31
 
33
- # Per-model override of Config options. Use via, e.g.
34
- # class MyModel < ActiveRecord::Base
35
- # schema_validations :auto_create => false
36
- # end
37
- #
38
- # If <tt>:auto_create</tt> is not specified, it is implicitly
39
- # specified as true. This allows the "non-invasive" style of using
40
- # SchemaValidations in which you set the global Config to
41
- # <tt>auto_create = false</tt>, then in any model that you want auto
42
- # validations you simply do:
43
- #
44
- # class MyModel < ActiveRecord::Base
45
- # schema_validations
46
- # end
47
- #
48
- # Of course other options can be passed, such as
49
- #
50
- # class MyModel < ActiveRecord::Base
51
- # schema_validations :except_type => :validates_presence_of
52
- # end
53
- #
54
- #
55
- def schema_validations(opts={})
56
- @schema_validations_config = SchemaValidations.config.merge({:auto_create => true}.merge(opts))
57
- end
32
+ # Per-model override of Config options. Use via, e.g.
33
+ # class MyModel < ActiveRecord::Base
34
+ # schema_validations :auto_create => false
35
+ # end
36
+ #
37
+ # If <tt>:auto_create</tt> is not specified, it is implicitly
38
+ # specified as true. This allows the "non-invasive" style of using
39
+ # SchemaValidations in which you set the global Config to
40
+ # <tt>auto_create = false</tt>, then in any model that you want auto
41
+ # validations you simply do:
42
+ #
43
+ # class MyModel < ActiveRecord::Base
44
+ # schema_validations
45
+ # end
46
+ #
47
+ # Of course other options can be passed, such as
48
+ #
49
+ # class MyModel < ActiveRecord::Base
50
+ # schema_validations :except_type => :validates_presence_of
51
+ # end
52
+ #
53
+ #
54
+ def schema_validations(opts={})
55
+ @schema_validations_config = SchemaValidations.config.merge({:auto_create => true}.merge(opts))
56
+ end
58
57
 
59
- def schema_validations_config # :nodoc:
60
- @schema_validations_config ||= SchemaValidations.config.dup
61
- end
58
+ def schema_validations_config # :nodoc:
59
+ @schema_validations_config ||= SchemaValidations.config.dup
60
+ end
62
61
 
63
- private
64
- # Adds schema-based validations to model.
65
- # Attributes as well as associations are validated.
66
- # For instance if there is column
67
- #
68
- # <code>email NOT NULL</code>
69
- #
70
- # defined at database-level it will be translated to
71
- #
72
- # <code>validates_presence_of :email</code>
73
- #
74
- # If there is an association named <tt>user</tt>
75
- # based on <tt>user_id NOT NULL</tt> it will be translated to
76
- #
77
- # <code>validates_presence_of :user</code>
78
- #
79
- # Note it uses the name of association (user) not the column name (user_id).
80
- # Only <tt>belongs_to</tt> associations are validated.
81
- #
82
- # This accepts following options:
83
- # * :only - auto-validate only given attributes
84
- # * :except - auto-validate all but given attributes
85
- #
86
- def load_schema_validations #:nodoc:
87
- # Don't bother if: it's already been loaded; the class is abstract; not a base class; or the table doesn't exist
88
- return unless create_schema_validations?
89
- load_column_validations
90
- load_association_validations
91
- self.schema_validations_loaded = true
92
- end
62
+ private
63
+ # Adds schema-based validations to model.
64
+ # Attributes as well as associations are validated.
65
+ # For instance if there is column
66
+ #
67
+ # <code>email NOT NULL</code>
68
+ #
69
+ # defined at database-level it will be translated to
70
+ #
71
+ # <code>validates_presence_of :email</code>
72
+ #
73
+ # If there is an association named <tt>user</tt>
74
+ # based on <tt>user_id NOT NULL</tt> it will be translated to
75
+ #
76
+ # <code>validates_presence_of :user</code>
77
+ #
78
+ # Note it uses the name of association (user) not the column name (user_id).
79
+ # Only <tt>belongs_to</tt> associations are validated.
80
+ #
81
+ # This accepts following options:
82
+ # * :only - auto-validate only given attributes
83
+ # * :except - auto-validate all but given attributes
84
+ #
85
+ def load_schema_validations #:nodoc:
86
+ # Don't bother if: it's already been loaded; the class is abstract; not a base class; or the table doesn't exist
87
+ return unless create_schema_validations?
88
+ load_column_validations
89
+ load_association_validations
90
+ self.schema_validations_loaded = true
91
+ end
93
92
 
94
- def load_column_validations #:nodoc:
95
- content_columns.each do |column|
96
- name = column.name.to_sym
93
+ def load_column_validations #:nodoc:
94
+ content_columns.each do |column|
95
+ name = column.name.to_sym
96
+
97
+ # Data-type validation
98
+ datatype = case
99
+ when respond_to?(:defined_enums) && defined_enums.has_key?(column.name) then :enum
100
+ when column.type == :integer then :integer
101
+ when column.number? then :numeric
102
+ when column.text? then :text
103
+ when column.type == :boolean then :boolean
104
+ end
105
+
106
+ case datatype
107
+ when :integer
108
+ load_integer_column_validations(name, column)
109
+ when :numeric
110
+ validate_logged :validates_numericality_of, name, :allow_nil => true
111
+ when :text
112
+ validate_logged :validates_length_of, name, :allow_nil => true, :maximum => column.limit if column.limit
113
+ end
114
+
115
+ # NOT NULL constraints
116
+ if column.required_on
117
+ if datatype == :boolean
118
+ validate_logged :validates_inclusion_of, name, :in => [true, false], :message => :blank
119
+ else
120
+ validate_logged :validates_presence_of, name
121
+ end
122
+ end
97
123
 
98
- # Data-type validation
99
- datatype = case
100
- when respond_to?(:defined_enums) && defined_enums.has_key?(column.name) then :enum
101
- when column.type == :integer then :integer
102
- when column.number? then :numeric
103
- when column.text? then :text
104
- when column.type == :boolean then :boolean
105
- end
106
-
107
- case datatype
108
- when :integer
109
- load_integer_column_validations(name, column)
110
- when :numeric
111
- validate_logged :validates_numericality_of, name, :allow_nil => true
112
- when :text
113
- validate_logged :validates_length_of, name, :allow_nil => true, :maximum => column.limit if column.limit
124
+ # UNIQUE constraints
125
+ add_uniqueness_validation(column) if column.unique?
114
126
  end
127
+ end
128
+
129
+ def load_integer_column_validations(name, column) # :nodoc:
130
+ options = { :allow_nil => true, :only_integer => true }
115
131
 
116
- # NOT NULL constraints
117
- if column.required_on
118
- if datatype == :boolean
119
- validate_logged :validates_inclusion_of, name, :in => [true, false], :message => :blank
132
+ if range = column.cast_type.try(:range)
133
+ options[:greater_than_or_equal_to] = range.begin
134
+ if range.exclude_end?
135
+ options[:less_than] = range.end
120
136
  else
121
- validate_logged :validates_presence_of, name
137
+ options[:less_than_or_equal_to] = range.end
122
138
  end
123
139
  end
124
140
 
125
- # UNIQUE constraints
126
- add_uniqueness_validation(column) if column.unique?
141
+ validate_logged :validates_numericality_of, name, options
127
142
  end
128
- end
129
143
 
130
- def load_integer_column_validations(name, column) # :nodoc:
131
- options = { :allow_nil => true, :only_integer => true }
144
+ def load_association_validations #:nodoc:
145
+ reflect_on_all_associations(:belongs_to).each do |association|
146
+ # :primary_key_name was deprecated (noisily) in rails 3.1
147
+ foreign_key_method = (association.respond_to? :foreign_key) ? :foreign_key : :primary_key_name
148
+ column = columns_hash[association.send(foreign_key_method).to_s]
149
+ next unless column
150
+
151
+ # NOT NULL constraints
152
+ validate_logged :validates_presence_of, association.name if column.required_on
132
153
 
133
- if range = column.cast_type.try(:range)
134
- options[:greater_than_or_equal_to] = range.begin
135
- if range.exclude_end?
136
- options[:less_than] = range.end
137
- else
138
- options[:less_than_or_equal_to] = range.end
154
+ # UNIQUE constraints
155
+ add_uniqueness_validation(column) if column.unique?
139
156
  end
140
157
  end
141
158
 
142
- validate_logged :validates_numericality_of, name, options
143
- end
144
-
145
- def load_association_validations #:nodoc:
146
- reflect_on_all_associations(:belongs_to).each do |association|
147
- # :primary_key_name was deprecated (noisily) in rails 3.1
148
- foreign_key_method = (association.respond_to? :foreign_key) ? :foreign_key : :primary_key_name
149
- column = columns_hash[association.send(foreign_key_method).to_s]
150
- next unless column
159
+ def add_uniqueness_validation(column) #:nodoc:
160
+ scope = column.unique_scope.map(&:to_sym)
161
+ name = column.name.to_sym
151
162
 
152
- # NOT NULL constraints
153
- validate_logged :validates_presence_of, association.name if column.required_on
163
+ options = {}
164
+ options[:scope] = scope if scope.any?
165
+ options[:allow_nil] = true
166
+ options[:case_sensitive] = false if has_case_insensitive_index?(column, scope)
167
+ options[:if] = (proc do |record|
168
+ if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
169
+ record.public_send(:"#{column.name}_changed?")
170
+ else
171
+ false
172
+ end
173
+ end)
154
174
 
155
- # UNIQUE constraints
156
- add_uniqueness_validation(column) if column.unique?
175
+ validate_logged :validates_uniqueness_of, name, options
157
176
  end
158
- end
159
177
 
160
- def add_uniqueness_validation(column) #:nodoc:
161
- scope = column.unique_scope.map(&:to_sym)
162
- name = column.name.to_sym
163
-
164
- options = {}
165
- options[:scope] = scope if scope.any?
166
- options[:allow_nil] = true
167
- options[:case_sensitive] = false if has_case_insensitive_index?(column, scope)
168
- options[:if] = (proc do |record|
169
- if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
170
- record.public_send(:"#{column.name}_changed?")
171
- else
172
- false
173
- end
174
- end)
175
-
176
- validate_logged :validates_uniqueness_of, name, options
177
- end
178
+ def has_case_insensitive_index?(column, scope)
179
+ indexed_columns = (scope + [column.name]).map(&:to_sym).sort
180
+ index = column.indexes.select { |i| i.unique && i.columns.map(&:to_sym).sort == indexed_columns }.first
178
181
 
179
- def has_case_insensitive_index?(column, scope)
180
- indexed_columns = (scope + [column.name]).map(&:to_sym).sort
181
- index = column.indexes.select { |i| i.unique && i.columns.map(&:to_sym).sort == indexed_columns }.first
182
-
183
- index && index.respond_to?(:case_sensitive?) && !index.case_sensitive?
184
- end
182
+ index && index.respond_to?(:case_sensitive?) && !index.case_sensitive?
183
+ end
185
184
 
186
- def create_schema_validations? #:nodoc:
187
- schema_validations_config.auto_create? && !(schema_validations_loaded || abstract_class? || name.blank? || !table_exists?)
188
- end
185
+ def create_schema_validations? #:nodoc:
186
+ schema_validations_config.auto_create? && !(schema_validations_loaded || abstract_class? || name.blank? || !table_exists?)
187
+ end
189
188
 
190
- def validate_logged(method, arg, opts={}) #:nodoc:
191
- if _filter_validation(method, arg)
192
- msg = "[schema_validations] #{self.name}.#{method} #{arg.inspect}"
193
- msg += ", #{opts.inspect[1...-1]}" if opts.any?
194
- logger.debug msg
195
- send method, arg, opts
189
+ def validate_logged(method, arg, opts={}) #:nodoc:
190
+ if _filter_validation(method, arg)
191
+ msg = "[schema_validations] #{self.name}.#{method} #{arg.inspect}"
192
+ msg += ", #{opts.inspect[1...-1]}" if opts.any?
193
+ logger.debug msg
194
+ send method, arg, opts
195
+ end
196
196
  end
197
- end
198
197
 
199
- def _filter_validation(macro, name) #:nodoc:
200
- config = schema_validations_config
201
- types = [macro]
202
- if match = macro.to_s.match(/^validates_(.*)_of$/)
203
- types << match[1].to_sym
198
+ def _filter_validation(macro, name) #:nodoc:
199
+ config = schema_validations_config
200
+ types = [macro]
201
+ if match = macro.to_s.match(/^validates_(.*)_of$/)
202
+ types << match[1].to_sym
203
+ end
204
+ return false if config.only and not Array.wrap(config.only).include?(name)
205
+ return false if config.except and Array.wrap(config.except).include?(name)
206
+ return false if config.whitelist and Array.wrap(config.whitelist).include?(name)
207
+ return false if config.only_type and not (Array.wrap(config.only_type) & types).any?
208
+ return false if config.except_type and (Array.wrap(config.except_type) & types).any?
209
+ return false if config.whitelist_type and (Array.wrap(config.whitelist_type) & types).any?
210
+ return true
204
211
  end
205
- return false if config.only and not Array.wrap(config.only).include?(name)
206
- return false if config.except and Array.wrap(config.except).include?(name)
207
- return false if config.whitelist and Array.wrap(config.whitelist).include?(name)
208
- return false if config.only_type and not (Array.wrap(config.only_type) & types).any?
209
- return false if config.except_type and (Array.wrap(config.except_type) & types).any?
210
- return false if config.whitelist_type and (Array.wrap(config.whitelist_type) & types).any?
211
- return true
212
- end
213
212
 
213
+ end
214
214
  end
215
215
 
216
216
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaValidations
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
@@ -4,7 +4,6 @@ require 'schema_plus_columns'
4
4
  require 'schema_validations/version'
5
5
  require 'schema_validations/active_record/validations'
6
6
  require 'schema_validations/active_record/type'
7
- require 'schema_validations/railtie' if defined?(Rails::Railtie)
8
7
 
9
8
  module SchemaValidations
10
9
 
@@ -109,13 +108,6 @@ module SchemaValidations
109
108
  yield config
110
109
  end
111
110
 
112
- def self.insert #:nodoc:
113
- return if @inserted
114
- @inserted = true
115
- ::ActiveRecord::Base.extend SchemaValidations::ActiveRecord::Validations
116
- ::ActiveRecord::Type::Integer.prepend SchemaValidations::ActiveRecord::Type::Integer
117
- end
118
-
119
111
  end
120
112
 
121
- SchemaValidations.insert unless defined? Rails::Railtie
113
+ SchemaMonkey.register SchemaValidations
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_dependency("activerecord", "~> 4.2", ">= 4.2.1")
25
25
  s.add_dependency("valuable")
26
26
 
27
- s.add_development_dependency("schema_dev", "~> 3.5")
27
+ s.add_development_dependency("schema_dev", "~> 3.6")
28
28
  s.add_development_dependency("rake")
29
29
  s.add_development_dependency("rdoc")
30
30
  s.add_development_dependency("rspec")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-01-19 00:00:00.000000000 Z
12
+ date: 2016-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_columns
@@ -65,14 +65,14 @@ dependencies:
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.5'
68
+ version: '3.6'
69
69
  type: :development
70
70
  prerelease: false
71
71
  version_requirements: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.5'
75
+ version: '3.6'
76
76
  - !ruby/object:Gem::Dependency
77
77
  name: rake
78
78
  requirement: !ruby/object:Gem::Requirement