schema_validations 1.3.1 → 1.4.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: a23ef37b81324aaf2c339a28090b57a870f14583
4
- data.tar.gz: baee93aa0e3c28544a09f145ca178456ea1928c7
3
+ metadata.gz: 8484a054c29074d10d3fa8dbb06df1206e4f3d3f
4
+ data.tar.gz: a6d45b80b265de0de5c5d23a33ddfcfbafc4c2e4
5
5
  SHA512:
6
- metadata.gz: 49c6eb9dfacb6b4dda7027f6b37dd21533479d1157ddc10276d957fcbb56962078a35040af02d07815071121bbcff1e5c260c9c82c6cdb96b73e6ee099c3ef50
7
- data.tar.gz: 37200e9a56008fab775fb0c7b38f638f645ecc447f77184ed09ae96f9385f7f9e2b89e53e5f9a2d284906032133466c214621708770fa2e30f8464bd2518ac36
6
+ metadata.gz: d0e13def81c02bf86fe2a0df3f557cccfc9ec5f50a1501a43bedb5837f3b311a64e13867e8d8bacb682ddfddb068f5ff936d72514d6d3d99be54ebb5cea9412c
7
+ data.tar.gz: 302f4ea589910494881650a2aae57ed654cadc880622acfc0b645a5d66c89122bbbaff1f55ecc59ebb27efff05af58d0473d4285aa510b4d05907a02f2cea325
data/.travis.yml CHANGED
@@ -7,9 +7,9 @@ sudo: false
7
7
  rvm:
8
8
  - 2.1.5
9
9
  gemfile:
10
- - gemfiles/activerecord-4.2.1/Gemfile.mysql2
11
- - gemfiles/activerecord-4.2.1/Gemfile.postgresql
12
- - gemfiles/activerecord-4.2.1/Gemfile.sqlite3
10
+ - gemfiles/activerecord-4.2/Gemfile.mysql2
11
+ - gemfiles/activerecord-4.2/Gemfile.postgresql
12
+ - gemfiles/activerecord-4.2/Gemfile.sqlite3
13
13
  env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
14
14
  addons:
15
15
  postgresql: '9.4'
data/README.md CHANGED
@@ -48,7 +48,7 @@ As of version 1.2.0, SchemaValidations supports and is tested on:
48
48
 
49
49
  <!-- SCHEMA_DEV: MATRIX - begin -->
50
50
  <!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
51
- * ruby **2.1.5** with activerecord **4.2.1**, using **mysql2**, **postgresql** or **sqlite3**
51
+ * ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **postgresql** or **sqlite3**
52
52
 
53
53
  <!-- SCHEMA_DEV: MATRIX - end -->
54
54
 
@@ -102,6 +102,7 @@ Constraints:
102
102
  | :null => false | validates ... :presence => true |
103
103
  | :limit => 100 | validates ... :length => { :maximum => 100 } |
104
104
  | :unique => true | validates ... :uniqueness => true |
105
+ | :unique => true, :case_sensitive => false <br>(If [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) is also in use) | validates ... :uniqueness => { :case_sensitive => false } |
105
106
 
106
107
  Data types:
107
108
 
@@ -130,6 +131,10 @@ use case.
130
131
 
131
132
  ## Release Notes
132
133
 
134
+ ### 1.4.0
135
+
136
+ * Add support for case-insensitive uniqueness. Thanks to [allenwq](https://github.com/allenwq)
137
+
133
138
  ### 1.3.1
134
139
 
135
140
  * Change log level from 'info' to 'debug', since there's no need to clutter production logs with this sort of development info. Thanks to [@obduk](https://github.com/obduk)
@@ -1,3 +1,3 @@
1
1
  eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
2
 
3
- gem "activerecord", "4.2.1"
3
+ gem "activerecord", "~> 4.2.0"
@@ -159,13 +159,27 @@ module SchemaValidations
159
159
  def add_uniqueness_validation(column) #:nodoc:
160
160
  scope = column.unique_scope.map(&:to_sym)
161
161
  name = column.name.to_sym
162
- validate_logged :validates_uniqueness_of, name, :scope => scope, :allow_nil => true, :if => (proc do |record|
162
+
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|
163
168
  if scope.all? { |scope_sym| record.public_send(:"#{scope_sym}?") }
164
169
  record.public_send(:"#{column.name}_changed?")
165
170
  else
166
171
  false
167
172
  end
168
173
  end)
174
+
175
+ validate_logged :validates_uniqueness_of, name, options
176
+ end
177
+
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
181
+
182
+ index && index.respond_to?(:case_sensitive?) && !index.case_sensitive?
169
183
  end
170
184
 
171
185
  def create_schema_validations? #:nodoc:
@@ -1,3 +1,3 @@
1
1
  module SchemaValidations
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
data/schema_dev.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  ruby:
2
2
  - 2.1.5
3
3
  activerecord:
4
- - 4.2.1
4
+ - 4.2
5
5
  db:
6
6
  - mysql2
7
7
  - postgresql
data/spec/spec_helper.rb CHANGED
@@ -15,8 +15,12 @@ DatabaseCleaner.strategy = :truncation
15
15
  SchemaDev::Rspec.setup
16
16
 
17
17
  RSpec.configure do |config|
18
- config.before(:each) do
18
+ config.around(:each) do |example|
19
19
  DatabaseCleaner.clean
20
+ remove_all_models
21
+ ActiveRecord::Migration.suppress_messages do
22
+ example.run
23
+ end
20
24
  end
21
25
  end
22
26
 
@@ -2,12 +2,34 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Validations" do
4
4
 
5
- before(:all) do
6
- define_schema
7
- end
5
+ before(:each) do
6
+ ActiveRecord::Schema.define do
7
+
8
+ create_table :articles, :force => true do |t|
9
+ t.string :title, :limit => 50
10
+ t.text :content, :null => false
11
+ t.integer :state
12
+ t.integer :votes
13
+ t.float :average_mark, :null => false
14
+ t.boolean :active, :null => false
15
+ end
16
+ add_index :articles, :title, :unique => true
17
+ add_index :articles, [:state, :active], :unique => true
18
+
19
+ create_table :reviews, :force => true do |t|
20
+ t.integer :article_id, :null => false
21
+ t.string :author, :null => false
22
+ t.string :content, :limit => 200
23
+ t.string :type
24
+ end
25
+ add_index :reviews, :article_id, :unique => true
8
26
 
9
- after(:each) do
10
- remove_all_models
27
+ create_table :article_reviews, :force => true do |t|
28
+ t.integer :article_id
29
+ t.integer :review_id
30
+ end
31
+ add_index :article_reviews, [:article_id, :review_id], :unique => true
32
+ end
11
33
  end
12
34
 
13
35
  context "auto-created" do
@@ -298,6 +320,64 @@ describe "Validations" do
298
320
  end
299
321
  end if ActiveRecord::Base.respond_to? :enum
300
322
 
323
+ context 'with case sensitive options' do
324
+ before do
325
+ allow_any_instance_of(ActiveRecord::ConnectionAdapters::IndexDefinition).to receive(:case_sensitive?).and_return(false)
326
+ end
327
+
328
+ context 'without scope' do
329
+ before do
330
+ ActiveRecord::Schema.define do
331
+ create_table :books, :force => true do |t|
332
+ t.string :title
333
+ end
334
+
335
+ add_index :books, :title, :unique => true
336
+ end
337
+
338
+ with_auto_validations do
339
+ class Book < ActiveRecord::Base; end
340
+ end
341
+ end
342
+
343
+ it "should validate the uniqueness in a case insensitive manner" do
344
+ mixed_case_title = 'Schema Validations'
345
+ Book.create(title: mixed_case_title)
346
+
347
+ expect(Book.new(title: mixed_case_title)).not_to be_valid
348
+ expect(Book.new(title: mixed_case_title.downcase)).not_to be_valid
349
+ end
350
+ end
351
+
352
+ context 'within a scope' do
353
+ before do
354
+ ActiveRecord::Schema.define do
355
+ create_table :folders, :force => true do |t|
356
+ t.integer :parent_id
357
+ t.string :name
358
+ end
359
+
360
+ add_index :folders, [:parent_id, :name], :unique => true
361
+ end
362
+
363
+ with_auto_validations do
364
+ class Folder < ActiveRecord::Base
365
+ belongs_to :parent, class_name: Folder.name
366
+ end
367
+ end
368
+ end
369
+
370
+ it "should validate the uniqueness in a case insensitive manner" do
371
+ mixed_case_name = 'Schema Validations'
372
+ parent_folder = Folder.create
373
+ Folder.create(:parent => parent_folder, :name => mixed_case_name)
374
+
375
+ expect(Folder.new(:parent => parent_folder, :name => mixed_case_name)).not_to be_valid
376
+ expect(Folder.new(:parent => parent_folder, :name => mixed_case_name.downcase)).not_to be_valid
377
+ end
378
+ end
379
+ end
380
+
301
381
  protected
302
382
  def with_auto_validations(value = true)
303
383
  old_value = SchemaValidations.config.auto_create
@@ -311,38 +391,6 @@ describe "Validations" do
311
391
  end
312
392
  end
313
393
 
314
- def define_schema
315
- ActiveRecord::Migration.suppress_messages do
316
- ActiveRecord::Schema.define do
317
- connection.tables.each do |table| drop_table table end
318
-
319
- create_table :articles, :force => true do |t|
320
- t.string :title, :limit => 50
321
- t.text :content, :null => false
322
- t.integer :state
323
- t.integer :votes
324
- t.float :average_mark, :null => false
325
- t.boolean :active, :null => false
326
- end
327
- add_index :articles, :title, :unique => true
328
- add_index :articles, [:state, :active], :unique => true
329
-
330
- create_table :reviews, :force => true do |t|
331
- t.integer :article_id, :null => false
332
- t.string :author, :null => false
333
- t.string :content, :limit => 200
334
- t.string :type
335
- end
336
- add_index :reviews, :article_id, :unique => true
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
343
- end
344
- end
345
- end
346
394
 
347
395
  def valid_article_attributes
348
396
  {
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.3.1
4
+ version: 1.4.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-08-28 00:00:00.000000000 Z
12
+ date: 2015-10-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_columns
@@ -175,10 +175,10 @@ files:
175
175
  - README.md
176
176
  - Rakefile
177
177
  - gemfiles/Gemfile.base
178
- - gemfiles/activerecord-4.2.1/Gemfile.base
179
- - gemfiles/activerecord-4.2.1/Gemfile.mysql2
180
- - gemfiles/activerecord-4.2.1/Gemfile.postgresql
181
- - gemfiles/activerecord-4.2.1/Gemfile.sqlite3
178
+ - gemfiles/activerecord-4.2/Gemfile.base
179
+ - gemfiles/activerecord-4.2/Gemfile.mysql2
180
+ - gemfiles/activerecord-4.2/Gemfile.postgresql
181
+ - gemfiles/activerecord-4.2/Gemfile.sqlite3
182
182
  - init.rb
183
183
  - lib/schema_validations.rb
184
184
  - lib/schema_validations/active_record/validations.rb