validates_lengths_from_database 0.6.0 → 0.7.0

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: 8a2017e0b6fb3a3f8a69d7756dcbd581421d4520
4
- data.tar.gz: cf44cca0ce1ffcf4b86afd8e188c44ede1db10ad
3
+ metadata.gz: ee05414f5d7aef3fb39a4e692b6616259d20db11
4
+ data.tar.gz: '096b57b2a49d98450f1127f27dfd135c34556d86'
5
5
  SHA512:
6
- metadata.gz: 3aeba2873a46956abf4097a30eb0fb05a8934db8c13338aa69f3670d97a02d3c68689c2d5c8dc0f78ca8a34d026f0616f1d6e3914fec53b77db1fd77f8731a9c
7
- data.tar.gz: 3e1ab91da9556d596daba58909c660ef1f924948c85c6eef9ff57d72c41837cb0606c153c78d9f19ed26de1376343015aa46dd9c241cea57ff230b79348e1406
6
+ metadata.gz: dd35abc81106fe376476a747e78a89aeedaf2828a9aeff2e1f30181a486efc7da88474e5914b82a5df3706bb6f2dd8c0995ea5c05d8a9986ebc234ea40f3cb2c
7
+ data.tar.gz: d637ae88ae7aa3e2a738e095981270d0228817ab660f8e98214bc0f0bfc23b09e463bb8ab62be5221a6191a9c9504e089d3ceb62d96b23f5d95f9530437a6714
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == v0.7.0 (2017-10-09)
2
+
3
+ * Drop hard reliance on rails (can use with just activerecord) - thanks @grosser
4
+ * Drop requiring of rubygems (unnecessary) - thanks @grosser
5
+ * Update dependencies to modern, build-able versions
6
+
1
7
  == v0.6.0 (2017-05-02)
2
8
 
3
9
  * Drop support for integer length validations (too many issues)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_lengths_from_database (0.5.0)
4
+ validates_lengths_from_database (0.6.0)
5
5
  activerecord (>= 3)
6
6
 
7
7
  GEM
@@ -31,7 +31,7 @@ GEM
31
31
  diff-lcs (1.1.2)
32
32
  i18n (0.7.0)
33
33
  iconv (1.0.4)
34
- json (1.8.3)
34
+ json (1.8.6)
35
35
  minitest (4.7.5)
36
36
  multi_json (1.10.1)
37
37
  pg (0.17.1)
@@ -67,4 +67,4 @@ DEPENDENCIES
67
67
  validates_lengths_from_database!
68
68
 
69
69
  BUNDLED WITH
70
- 1.11.2
70
+ 1.15.4
@@ -1,75 +1,3 @@
1
- require "rubygems"
2
- require "active_record"
3
-
4
- module ValidatesLengthsFromDatabase
5
- def self.included(base)
6
- base.send(:extend, ClassMethods)
7
- base.send(:include, InstanceMethods)
8
- end
9
-
10
- module ClassMethods
11
- def validates_lengths_from_database(options = {})
12
- options.symbolize_keys!
13
-
14
- options[:only] = Array[options[:only]] if options[:only] && !options[:only].is_a?(Array)
15
- options[:except] = Array[options[:except]] if options[:except] && !options[:except].is_a?(Array)
16
- options[:limit] ||= {}
17
-
18
- if options[:limit] and !options[:limit].is_a?(Hash)
19
- options[:limit] = {:string => options[:limit], :text => options[:limit], :decimal => options[:limit], :integer => options[:limit], :float => options[:limit]}
20
- end
21
- @validate_lengths_from_database_options = options
22
-
23
- validate :validate_lengths_from_database
24
-
25
- nil
26
- end
27
-
28
- def validate_lengths_from_database_options
29
- if defined? @validate_lengths_from_database_options
30
- @validate_lengths_from_database_options
31
- else
32
- # in case we inherited the validations, copy the options so that we can update it in child
33
- # without affecting the parent
34
- @validate_lengths_from_database_options = superclass.validate_lengths_from_database_options.inject({}) do |hash, (key, value)|
35
- value = value.dup if value.respond_to?(:dup)
36
- hash.update(key => value)
37
- end
38
- end
39
- end
40
- end
41
-
42
- module InstanceMethods
43
- def validate_lengths_from_database
44
- options = self.class.validate_lengths_from_database_options
45
-
46
- if options[:only]
47
- columns_to_validate = options[:only].map(&:to_s)
48
- else
49
- columns_to_validate = self.class.column_names.map(&:to_s)
50
- columns_to_validate -= options[:except].map(&:to_s) if options[:except]
51
- end
52
-
53
- columns_to_validate.each do |column|
54
- column_schema = self.class.columns.find {|c| c.name == column }
55
- next if column_schema.nil?
56
- next if column_schema.respond_to?(:array) && column_schema.array
57
- next unless [:string, :text, :decimal].include?(column_schema.type)
58
- case column_schema.type
59
- when :string, :text
60
- column_limit = options[:limit][column_schema.type] || column_schema.limit
61
- if column_limit
62
- ActiveModel::Validations::LengthValidator.new(:maximum => column_limit, :allow_blank => true, :attributes => [column]).validate(self)
63
- end
64
- when :decimal
65
- if column_schema.precision && column_schema.scale
66
- max_val = (10 ** column_schema.precision)/(10 ** column_schema.scale)
67
- ActiveModel::Validations::NumericalityValidator.new(:less_than => max_val, :allow_blank => true, :attributes => [column]).validate(self)
68
- end
69
- end
70
- end
71
- end
72
- end
73
- end
74
1
 
2
+ require "validates_lengths_from_database/core"
75
3
  require "validates_lengths_from_database/railtie"
@@ -0,0 +1,70 @@
1
+ module ValidatesLengthsFromDatabase
2
+ def self.included(base)
3
+ base.send(:extend, ClassMethods)
4
+ base.send(:include, InstanceMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def validates_lengths_from_database(options = {})
9
+ options.symbolize_keys!
10
+
11
+ options[:only] = Array[options[:only]] if options[:only] && !options[:only].is_a?(Array)
12
+ options[:except] = Array[options[:except]] if options[:except] && !options[:except].is_a?(Array)
13
+ options[:limit] ||= {}
14
+
15
+ if options[:limit] and !options[:limit].is_a?(Hash)
16
+ options[:limit] = {:string => options[:limit], :text => options[:limit], :decimal => options[:limit], :integer => options[:limit], :float => options[:limit]}
17
+ end
18
+ @validate_lengths_from_database_options = options
19
+
20
+ validate :validate_lengths_from_database
21
+
22
+ nil
23
+ end
24
+
25
+ def validate_lengths_from_database_options
26
+ if defined? @validate_lengths_from_database_options
27
+ @validate_lengths_from_database_options
28
+ else
29
+ # in case we inherited the validations, copy the options so that we can update it in child
30
+ # without affecting the parent
31
+ @validate_lengths_from_database_options = superclass.validate_lengths_from_database_options.inject({}) do |hash, (key, value)|
32
+ value = value.dup if value.respond_to?(:dup)
33
+ hash.update(key => value)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ module InstanceMethods
40
+ def validate_lengths_from_database
41
+ options = self.class.validate_lengths_from_database_options
42
+
43
+ if options[:only]
44
+ columns_to_validate = options[:only].map(&:to_s)
45
+ else
46
+ columns_to_validate = self.class.column_names.map(&:to_s)
47
+ columns_to_validate -= options[:except].map(&:to_s) if options[:except]
48
+ end
49
+
50
+ columns_to_validate.each do |column|
51
+ column_schema = self.class.columns.find {|c| c.name == column }
52
+ next if column_schema.nil?
53
+ next if column_schema.respond_to?(:array) && column_schema.array
54
+ next unless [:string, :text, :decimal].include?(column_schema.type)
55
+ case column_schema.type
56
+ when :string, :text
57
+ column_limit = options[:limit][column_schema.type] || column_schema.limit
58
+ if column_limit
59
+ ActiveModel::Validations::LengthValidator.new(:maximum => column_limit, :allow_blank => true, :attributes => [column]).validate(self)
60
+ end
61
+ when :decimal
62
+ if column_schema.precision && column_schema.scale
63
+ max_val = (10 ** column_schema.precision)/(10 ** column_schema.scale)
64
+ ActiveModel::Validations::NumericalityValidator.new(:less_than => max_val, :allow_blank => true, :attributes => [column]).validate(self)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,7 +1,7 @@
1
1
  module ValidatesLengthsFromDatabase
2
2
  if defined?(Rails::Railtie)
3
3
  require "rails"
4
-
4
+
5
5
  class Railtie < Rails::Railtie
6
6
  initializer "validates_lengths_from_database.extend_active_record" do
7
7
  ActiveSupport.on_load(:active_record) do
@@ -10,7 +10,7 @@ module ValidatesLengthsFromDatabase
10
10
  end
11
11
  end
12
12
  end
13
-
13
+
14
14
  class Railtie
15
15
  def self.insert
16
16
  ActiveRecord::Base.send(:include, ValidatesLengthsFromDatabase)
@@ -1,3 +1,3 @@
1
1
  module ValidatesLengthsFromDatabase
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
data/spec/db/test.sqlite3 CHANGED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_lengths_from_database
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-03 00:00:00.000000000 Z
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -166,6 +166,7 @@ files:
166
166
  - Rakefile
167
167
  - init.rb
168
168
  - lib/validates_lengths_from_database.rb
169
+ - lib/validates_lengths_from_database/core.rb
169
170
  - lib/validates_lengths_from_database/railtie.rb
170
171
  - lib/validates_lengths_from_database/version.rb
171
172
  - rails/init.rb