database_validations 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0272e7610a4c92445421092f94faff13711f4b235d74340ee924b946dfe4d487
4
- data.tar.gz: 0646b4e6bab48d3e48c4860d715e600a7f02211be18676d0c5258e33b490e1e8
3
+ metadata.gz: 2236241fbd9c25b9cbe9ffa63f5634659c6ad0ded206324cc7021765049ae116
4
+ data.tar.gz: ec3e6c0ec0ea21a00455a2035452030977994e99bdb4b4ff45b8405d57e5f0d4
5
5
  SHA512:
6
- metadata.gz: 0a475cdbd890674df1b3ed1f7f493c0426638016e5c4a20e8a5f333cb7f4d3101581a40478d45c2dedde0fea77fbdd1589a7947d8ab6c7d4f26008c829b066db
7
- data.tar.gz: b7e63a9b639fcb81a3f7921336563d9c4cb5d68e27c27c80863ad0ed56d40977ffb9f93ba6732ef469983052f174374e30f38e42cf732833b51898ceee81f135
6
+ metadata.gz: f5691f17f29a775e910150be7415c77a02eb0436e428bf14efa60365f0840574066307b02d0b71de00431dff71f24215e366a68d44f1cf34528efb3570a584ec
7
+ data.tar.gz: 613e741dc4f48e245f3d6abc9af136fad6ae28d974583e2713ce9af4955d68c97d73fb371444e8fc519bfa428758221680dffc77b48e91a1d9b62103a2291d5a
data/README.md CHANGED
@@ -87,11 +87,11 @@ We want to provide full compatibility with existing `validates_uniqueness_of` va
87
87
  | unless | + | + | + |
88
88
  | index_name | + | + | - |
89
89
  | where | + | - | - |
90
- | case_sensitive | - | - | - |
90
+ | case_sensitive | + | - | - |
91
91
  | allow_nil | - | - | - |
92
92
  | allow_blank | - | - | - |
93
93
 
94
- **Keep in mind**: Both `if` and `unless` options are used only for `valid?` method and provided only for performance reason.
94
+ **Keep in mind**: `if`, `unless` and `case_sensitive` options are used only for `valid?` method.
95
95
 
96
96
  ```ruby
97
97
  class User < ActiveRecord::Base
@@ -104,7 +104,7 @@ user.field = 'another'
104
104
  user.valid? # Will not query the database
105
105
  ```
106
106
 
107
- **Backward compatibility**: Even when we don't support `case_sensitive`, `allow_nil` and `allow_blank` options now, the following:
107
+ **Backward compatibility**: Even when we don't natively support `case_sensitive`, `allow_nil` and `allow_blank` options now, the following:
108
108
 
109
109
  ```ruby
110
110
  validates_db_uniqueness_of :email
@@ -116,6 +116,18 @@ Is the same by default as the following
116
116
  validates_uniqueness_of :email, allow_nil: true, allow_blank: false, case_sensitive: true
117
117
  ```
118
118
 
119
+ Complete `case_sensitive` replacement example (for `PostgreSQL` only):
120
+
121
+ ```ruby
122
+ validates :slug, uniqueness: { case_sensitive: false, scope: :field }
123
+ ```
124
+
125
+ Should be replaced by:
126
+
127
+ ```ruby
128
+ validates_db_uniqueness_of :slug, index_name: :unique_index_with_field_lower_on_slug, case_sensitive: false
129
+ ```
130
+
119
131
  Options descriptions:
120
132
  - `scope`: One or more columns by which to limit the scope of the uniqueness constraint.
121
133
  - `message`: Specifies a custom error message (default is: "has already been taken").
@@ -7,6 +7,7 @@
7
7
  # * `scoped_to(scope)` -- specifies a scope for the validator;
8
8
  # * `with_where(where)` -- specifies a where condition for the validator;
9
9
  # * `with_index(index_name)` -- specifies an index name for the validator;
10
+ # * `case_insensitive` -- specifies case insensitivity for the validator;
10
11
  #
11
12
  # Example:
12
13
  #
@@ -36,6 +37,10 @@ RSpec::Matchers.define :validate_db_uniqueness_of do |field|
36
37
  @index_name = index_name
37
38
  end
38
39
 
40
+ chain(:case_insensitive) do
41
+ @case_sensitive = false
42
+ end
43
+
39
44
  match do |object|
40
45
  @validators = []
41
46
 
@@ -43,15 +48,23 @@ RSpec::Matchers.define :validate_db_uniqueness_of do |field|
43
48
 
44
49
  DatabaseValidations::Helpers.each_validator(model) do |validator|
45
50
  @validators << {
46
- field: validator.field,
47
- scope: validator.scope,
48
- where: validator.where_clause,
49
- message: validator.message,
50
- index_name: validator.index_name
51
+ field: validator.field,
52
+ scope: validator.scope,
53
+ where: validator.where_clause,
54
+ message: validator.message,
55
+ index_name: validator.index_name,
56
+ case_sensitive: validator.case_sensitive
51
57
  }
52
58
  end
53
59
 
54
- @validators.include?(field: field, scope: Array.wrap(@scope), where: @where, message: @message, index_name: @index_name)
60
+ @validators.include?(
61
+ field: field,
62
+ scope: Array.wrap(@scope),
63
+ where: @where,
64
+ message: @message,
65
+ index_name: @index_name,
66
+ case_sensitive: @case_sensitive
67
+ )
55
68
  end
56
69
 
57
70
  description do
@@ -60,7 +73,8 @@ RSpec::Matchers.define :validate_db_uniqueness_of do |field|
60
73
  desc += "message: '#{@message}'; " if @message
61
74
  desc += "scope: #{@scope}; " if @scope
62
75
  desc += "where: '#{@where}'; " if @where
63
- desc += "index_name: '#{index_name}'." if @index_name
76
+ desc += "index_name: '#{@index_name}'; " if @index_name
77
+ desc += "be case insensitive." if @case_sensitive === false
64
78
  desc
65
79
  end
66
80
 
@@ -1,7 +1,7 @@
1
1
  module DatabaseValidations
2
2
  module Adapters
3
3
  class PostgresqlAdapter < BaseAdapter
4
- SUPPORTED_OPTIONS = %i[scope message where if unless index_name].freeze
4
+ SUPPORTED_OPTIONS = %i[scope message where if unless index_name case_sensitive].freeze
5
5
  ADAPTER = :postgresql
6
6
 
7
7
  def index_name(error_message)
@@ -1,6 +1,7 @@
1
1
  module DatabaseValidations
2
2
  class UniquenessOptions
3
3
  CUSTOM_OPTIONS = %i[where index_name].freeze
4
+ DEFAULT_OPTIONS = {allow_nil: true, case_sensitive: true, allow_blank: false}.freeze
4
5
 
5
6
  attr_reader :field
6
7
 
@@ -24,8 +25,9 @@ module DatabaseValidations
24
25
  def validates_uniqueness_options
25
26
  where_clause_str = where_clause
26
27
 
27
- options.except(*CUSTOM_OPTIONS)
28
- .merge(allow_nil: true, case_sensitive: true, allow_blank: false)
28
+ DEFAULT_OPTIONS
29
+ .merge(options)
30
+ .except(*CUSTOM_OPTIONS)
29
31
  .tap { |opts| opts[:conditions] = -> { where(where_clause_str) } if where_clause }
30
32
  end
31
33
 
@@ -65,6 +67,11 @@ module DatabaseValidations
65
67
  @index_name ||= options[:index_name]
66
68
  end
67
69
 
70
+ # @return [Boolean|nil]
71
+ def case_sensitive
72
+ @case_sensitive ||= options[:case_sensitive]
73
+ end
74
+
68
75
  private
69
76
 
70
77
  attr_reader :adapter, :options
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.7.2'
2
+ VERSION = '0.7.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-17 00:00:00.000000000 Z
11
+ date: 2018-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord