schema_validations 1.2.0 → 1.3.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: 6ea33d2335e1af126b8d168661412173f65bd12c
4
- data.tar.gz: 91b7ba79752b866df0c1184356f5aa9f43ec311f
3
+ metadata.gz: 6e6588d0c8d748fe24ce334bf2c57678ce35d345
4
+ data.tar.gz: 2e49d0ee57215bc100a652834d35196a5a47e33d
5
5
  SHA512:
6
- metadata.gz: f5d11da519e0fab53b9d494e0e51b203b7a593c22c35e1d68f92a2c442185dd703ea96461566bbb7723b285fe4194db2863f5de3a5d4f2550dbdf3c5831d480b
7
- data.tar.gz: 27d023e4d31824c6f85444cdefc34d15002da1efd1f252960c5cb961a1f04abd5cf870d556e9e087baa0361bc993a947eb35fcd18090a6168161e6c23171142f
6
+ metadata.gz: 9a7899832e98bdaca1f90fa1bb1619d12dbe55fdcce6ccccfea24285d6b20a9ad1f9c63cb904f65a537cebe5196da38c80e53b843f70b6a265c12f6f91e58ebe
7
+ data.tar.gz: f64bceebdcf7ee916576abd86be39f7d329c9bd020f6c51b1f33e5faaa32b381902b460a6464717cf4c064815d528eea0a21bd6d577428b96153d18ae23a3cf9
data/README.md CHANGED
@@ -6,8 +6,7 @@ on the database schema.
6
6
 
7
7
  [![Gem Version](https://badge.fury.io/rb/schema_validations.svg)](http://badge.fury.io/rb/schema_validations)
8
8
  [![Build Status](https://secure.travis-ci.org/SchemaPlus/schema_validations.svg)](http://travis-ci.org/SchemaPlus/schema_validations)
9
- [![Coverage Status](https://img.shields.io/coveralls/SchemaPlus/schema_validations.svg)](https://coveralls.io/r/SchemaPlus/schema_validations)
10
- [![Dependency Status](https://gemnasium.com/lomba/schema_validations.svg)](https://gemnasium.com/lomba/schema_validations)
9
+ [![Coverage Status](https://coveralls.io/repos/SchemaPlus/schema_validations/badge.svg?branch=master&service=github)](https://coveralls.io/github/SchemaPlus/schema_validations)
11
10
 
12
11
 
13
12
  ## Overview
@@ -106,11 +105,11 @@ Constraints:
106
105
 
107
106
  Data types:
108
107
 
109
- | Type | Validation |
110
- |--------------------|-----------------------------------------------------------|
111
- | :boolean | :validates ... :inclusion => { :in => [true, false] } |
112
- | :float | :validates ... :numericality => true |
113
- | :integer | :validates ... :numericality => { :only_integer => true } |
108
+ | Type | Validation |
109
+ |--------------------|----------------------------------------------------------------------------------------------------------------|
110
+ | :boolean | :validates ... :inclusion => { :in => [true, false] } |
111
+ | :float | :validates ... :numericality => true |
112
+ | :integer | :validates ... :numericality => { :only_integer => true, :greater_than_or_equal_to => ..., :less_than => ... } |
114
113
 
115
114
  ## How do I know what it did?
116
115
  If you're curious (or dubious) about what validations SchemaValidations
@@ -131,6 +130,10 @@ use case.
131
130
 
132
131
  ## Release Notes
133
132
 
133
+ ### 1.3.0
134
+
135
+ * Add range checks to integer validations. Thanks to [@lowjoel](https://github.com/lowjoel)
136
+
134
137
  ### 1.2.0
135
138
 
136
139
  * No longer pull in schema_plus's auto-foreign key behavior. Limited to AR >= 4.2.1
@@ -106,7 +106,7 @@ module SchemaValidations
106
106
 
107
107
  case datatype
108
108
  when :integer
109
- validate_logged :validates_numericality_of, name, :allow_nil => true, :only_integer => true
109
+ load_integer_column_validations(name, column)
110
110
  when :numeric
111
111
  validate_logged :validates_numericality_of, name, :allow_nil => true
112
112
  when :text
@@ -127,6 +127,20 @@ module SchemaValidations
127
127
  end
128
128
  end
129
129
 
130
+ def load_integer_column_validations(name, column) # :nodoc:
131
+ options = { :allow_nil => true, :only_integer => true }
132
+
133
+ range = column.cast_type.send(: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
139
+ end
140
+
141
+ validate_logged :validates_numericality_of, name, options
142
+ end
143
+
130
144
  def load_association_validations #:nodoc:
131
145
  reflect_on_all_associations(:belongs_to).each do |association|
132
146
  # :primary_key_name was deprecated (noisily) in rails 3.1
@@ -144,9 +158,14 @@ module SchemaValidations
144
158
 
145
159
  def add_uniqueness_validation(column) #:nodoc:
146
160
  scope = column.unique_scope.map(&:to_sym)
147
- condition = :"#{column.name}_changed?"
148
161
  name = column.name.to_sym
149
- validate_logged :validates_uniqueness_of, name, :scope => scope, :allow_nil => true, :if => condition
162
+ validate_logged :validates_uniqueness_of, name, :scope => scope, :allow_nil => true, :if => (proc do |record|
163
+ if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
164
+ record.public_send(:"#{column.name}_changed?")
165
+ else
166
+ false
167
+ end
168
+ end)
150
169
  end
151
170
 
152
171
  def create_schema_validations? #:nodoc:
@@ -1,3 +1,3 @@
1
1
  module SchemaValidations
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -43,7 +43,7 @@ module SchemaValidations
43
43
  has_value :except, :default => [:created_at, :updated_at, :created_on, :updated_on]
44
44
 
45
45
  ##
46
- # :attr_accessor: only_type
46
+ # :attr_accessor: except_type
47
47
  #
48
48
  # List of validation types to exclude from automatic validation.
49
49
  # Value is a single type, and array of types, or +nil+. Default is +nil+.
@@ -20,6 +20,11 @@ describe "Validations" do
20
20
  belongs_to :news_article, :class_name => 'Article', :foreign_key => :article_id
21
21
  schema_validations :except => :content
22
22
  end
23
+
24
+ class ArticleReview < ActiveRecord::Base
25
+ belongs_to :article
26
+ belongs_to :review
27
+ end
23
28
  end
24
29
  end
25
30
 
@@ -61,6 +66,11 @@ describe "Validations" do
61
66
  expect(Article.new(:state => 1.23).error_on(:state).size).to eq(1)
62
67
  end
63
68
 
69
+ it "should validate the range of votes" do
70
+ expect(Article.new(votes: 2147483648).error_on(:votes).size).to eq(1)
71
+ expect(Article.new(votes: -2147483649).error_on(:votes).size).to eq(1)
72
+ end
73
+
64
74
  it "should validate average_mark numericality" do
65
75
  expect(Article.new(:average_mark => "high").error_on(:average_mark).size).to eq(1)
66
76
  end
@@ -103,6 +113,19 @@ describe "Validations" do
103
113
  expect(Review.new.error_on(:news_article).size).to eq(1)
104
114
  end
105
115
 
116
+ it "should not validate uniqueness when scope is absent" do
117
+ article_review_1 = ArticleReview.create(:article_id => 1, :review_id => nil)
118
+ expect(article_review_1).to be_valid
119
+
120
+ article_review_2 = ArticleReview.create(:article_id => 1, :review_id => nil)
121
+ expect(article_review_2).to be_valid
122
+
123
+ article_review_3 = ArticleReview.create(:article_id => nil, :review_id => 1)
124
+ expect(article_review_3).to be_valid
125
+
126
+ article_review_4 = ArticleReview.create(:article_id => nil, :review_id => 1)
127
+ expect(article_review_4).to be_valid
128
+ end
106
129
  end
107
130
 
108
131
  context "auto-created but changed" do
@@ -297,6 +320,7 @@ describe "Validations" do
297
320
  t.string :title, :limit => 50
298
321
  t.text :content, :null => false
299
322
  t.integer :state
323
+ t.integer :votes
300
324
  t.float :average_mark, :null => false
301
325
  t.boolean :active, :null => false
302
326
  end
@@ -311,6 +335,11 @@ describe "Validations" do
311
335
  end
312
336
  add_index :reviews, :article_id, :unique => true
313
337
 
338
+ create_table :article_reviews, :force => true do |t|
339
+ t.integer :article_id
340
+ t.integer :review_id
341
+ end
342
+ add_index :article_reviews, [:article_id, :review_id], :unique => true
314
343
  end
315
344
  end
316
345
  end
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: 1.2.0
4
+ version: 1.3.0
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: 2015-05-12 00:00:00.000000000 Z
12
+ date: 2015-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_columns