database_validations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +141 -0
  4. data/lib/database_validations.rb +15 -0
  5. metadata +172 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 290f698bb3acea997e8154fce04fc2bc193ad979ed13a26ba3b06cada78a0832
4
+ data.tar.gz: 202646e73ce8cac4fd33af5c52ef295d710cd18707ea8572903f6f4c50846fdf
5
+ SHA512:
6
+ metadata.gz: 38d5853ee7d94b95c72e60e548aeefa7d08b0f75598641ac64451a5197eb1a8d2a4111be1ede94e7cdbfac9a23096ac9f23ecd1a50801f7604c5692b8c95858a
7
+ data.tar.gz: ebd84880d6ac224f7a82e184ad9368b02f143ceb9d807e32d973922a80fa60e3575bee9b162faec335a21680d4ec17cde34de99ca7ac994da807aca7df53e388
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Toptal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # DatabaseValidations
2
+
3
+ ActiveRecord provides validations on app level but it won't guarantee the
4
+ consistent. In some cases, like `validates_uniqueness_of` it executes
5
+ additional SQL query to the database and that is not very efficient.
6
+
7
+ The main goal of the gem is to provide compatibility between database constraints
8
+ and ActiveRecord validations with better performance and consistency.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'database_validations'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install database_validations
25
+
26
+ ## validates_db_uniqueness_of
27
+
28
+ Supported databases: `postgresql`, `mysql` and `sqlite`.
29
+
30
+ ### Usage
31
+
32
+ ```ruby
33
+ class User < ActiveRecord::Base
34
+ validates_db_uniqueness_of :email
35
+ end
36
+
37
+ original = User.create(email: 'email@mail.com')
38
+ dupe = User.create(email: 'email@mail.com')
39
+ # => false
40
+ dupe.errors.messages
41
+ # => {:email=>["has already been taken"]}
42
+ User.create!(email: 'email@mail.com')
43
+ # => ActiveRecord::RecordInvalid Validation failed: email has already been taken
44
+ ```
45
+
46
+ **Note**: keep in mind, we don't check uniqueness validity through `valid?` method.
47
+ ```ruby
48
+ original = User.create(email: 'email@mail.com')
49
+ dupe = User.new(email: 'email@mail.com')
50
+ dupe.valid?
51
+ # => true
52
+ dupe.save
53
+ # => false
54
+ dupe.errors.messages
55
+ # => {:email=>["has already been taken"]}
56
+ ```
57
+
58
+ ### Configuration options
59
+
60
+ We want to provide full compatibility with existing `validates_uniqueness_of` validator.
61
+
62
+ This list of options are from `validates_uniqueness_of` validator:
63
+
64
+ - `scope`: One or more columns by which to limit the scope of the uniqueness constraint.
65
+ - `message`: Specifies a custom error message (default is: "has already been taken").
66
+ - `conditions`: Specify the conditions to be included as a <tt>WHERE</tt> SQL fragment to
67
+ limit the uniqueness constraint lookup (e.g. <tt>conditions: -> { where(status: 'active') }</tt>).
68
+ - `case_sensitive`: Looks for an exact match. Ignored by non-text columns (`true` by default).
69
+ - `allow_nil`: If set to `true`, skips this validation if the attribute is `nil` (default is `false`).
70
+ - `allow_blank`: If set to `true`, skips this validation if the attribute is blank (default is `false`).
71
+ - `if`: Specifies a method, proc or string to call to determine if the validation should occur
72
+ (e.g. <tt>if: :allow_validation</tt>, or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
73
+ proc or string should return or evaluate to a `true` or `false` value.
74
+ - `unless`: Specifies a method, proc or string to call to determine if the validation should not
75
+ occur (e.g. <tt>unless: :skip_validation</tt>, or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>).
76
+ The method, proc or string should return or evaluate to a `true` or `false` value.
77
+
78
+ **Note**: only few options are supported now: `scope`.
79
+
80
+ ### Benchmark ([code](https://github.com/toptal/database_validations/blob/master/benchmarks/uniqueness_validator_benchmark.rb))
81
+
82
+ #### Saving only duplicates items ([code](https://github.com/toptal/database_validations/blob/master/benchmarks/uniqueness_validator_benchmark.rb#L56))
83
+
84
+ ```
85
+ validates_db_uniqueness_of
86
+ 1.487k (±10.1%) i/s - 7.425k in 5.053608s
87
+ validates_uniqueness_of
88
+ 1.500k (±18.3%) i/s - 7.238k in 5.024355s
89
+ ```
90
+
91
+ #### Saving only unique items ([code](https://github.com/toptal/database_validations/blob/master/benchmarks/uniqueness_validator_benchmark.rb#L63))
92
+
93
+ ```
94
+ validates_db_uniqueness_of
95
+ 3.558k (± 3.5%) i/s - 18.105k in 5.094799s
96
+ validates_uniqueness_of
97
+ 2.031k (± 8.3%) i/s - 10.241k in 5.080059s
98
+ ```
99
+
100
+ #### Each hundredth item is duplicate ([code](https://github.com/toptal/database_validations/blob/master/benchmarks/uniqueness_validator_benchmark.rb#L70))
101
+
102
+ ```
103
+ validates_db_uniqueness_of
104
+ 3.499k (± 4.8%) i/s - 17.628k in 5.050887s
105
+ validates_uniqueness_of
106
+ 2.074k (± 8.6%) i/s - 10.388k in 5.063879s
107
+ ```
108
+
109
+ ## Development
110
+
111
+ You need to have installed and running `postgresql` and `mysql`.
112
+ And for each adapter manually create a database called `database_validations_test`.
113
+
114
+ After checking out the repo, run `bin/setup` to install dependencies.
115
+
116
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for
117
+ an interactive prompt that will allow you to experiment.
118
+
119
+ To install this gem onto your local machine, run `bundle exec rake install`.
120
+ To release a new version, update the version number in `version.rb`, and then
121
+ run `bundle exec rake release`, which will create a git tag for the version,
122
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ [Bug reports](https://github.com/toptal/database_validations/issues) and [pull requests](https://github.com/toptal/database_validations/pulls) are welcome on GitHub.
127
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected
128
+ to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
133
+
134
+ ## Code of Conduct
135
+
136
+ Everyone interacting in the DatabaseValidations project’s codebases, issue trackers, chat rooms and mailing
137
+ lists is expected to follow the [code of conduct](https://github.com/toptal/database_validations/blob/master/CODE_OF_CONDUCT.md).
138
+
139
+ ## Authors
140
+
141
+ - [Evgeniy Demin](https://github.com/djezzzl)
@@ -0,0 +1,15 @@
1
+ require 'active_record'
2
+
3
+ require 'database_validations/version'
4
+ require 'database_validations/uniqueness_validator'
5
+ require 'database_validations/errors'
6
+ require 'database_validations/helpers'
7
+ require 'database_validations/adapters'
8
+
9
+ module DatabaseValidations
10
+ extend ActiveSupport::Concern
11
+ end
12
+
13
+ if defined?(ActiveRecord::Base)
14
+ ActiveRecord::Base.include(DatabaseValidations)
15
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeniy Demin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: benchmark-ips
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pg
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.1'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: mysql2
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.5'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.5'
89
+ - !ruby/object:Gem::Dependency
90
+ name: bundler
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.16'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.16'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '10.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '10.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '3.0'
131
+ description: |-
132
+ ActiveRecord provides validations on app level but it won't guarantee the
133
+ consistent. In some cases, like `validates_uniqueness_of` it executes
134
+ additional SQL query to the database and that is not very efficient.
135
+
136
+ The main goal of the gem is to provide compatibility between database constraints
137
+ and ActiveRecord validations with better performance and consistency.
138
+ email:
139
+ - lawliet.djez@gmail.com
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files: []
143
+ files:
144
+ - LICENSE.txt
145
+ - README.md
146
+ - lib/database_validations.rb
147
+ homepage: https://github.com/toptal/database_validations
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.7.7
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Provide compatibility between database constraints and ActiveRecord validations
171
+ with better performance and consistency.
172
+ test_files: []