uniqueness 0.5.0 → 0.6.0
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 +4 -4
- data/.travis.yml +8 -2
- data/README.md +33 -2
- data/lib/uniqueness/engine.rb +1 -1
- data/lib/uniqueness/model.rb +53 -17
- data/lib/uniqueness/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9103e34b3744f24b43bc7d6b5c8bbeff052e7bd
|
4
|
+
data.tar.gz: fbbb46f768b37161ee05abc657d9ff35cbb8a09c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10fe4f821e8aaa584e7bb6a79c771e72c4949e1f10373f86ce016d473198d281e963e9248cb11d6e1b74e1a9fd98fcc17c7dc26ab2f55bf2c845a656de00ddba
|
7
|
+
data.tar.gz: 538b5d94051fc97a83ec06415a8ae11528c8b6d06f3d3e72ed7cb8d70d43cc135cbaae0c5bce67754bc35fea772f9d021a8626ef7161929c00db339a0b041134
|
data/.travis.yml
CHANGED
@@ -4,8 +4,6 @@ rvm:
|
|
4
4
|
- 2.2.4
|
5
5
|
- 2.1.8
|
6
6
|
- 2.0.0
|
7
|
-
- 1.9.3
|
8
|
-
- jruby-19mode
|
9
7
|
- rbx-2
|
10
8
|
before_install:
|
11
9
|
- gem install bundler -v '~> 1.11'
|
@@ -14,5 +12,13 @@ gemfile:
|
|
14
12
|
- gemfiles/41.gemfile
|
15
13
|
- gemfiles/42.gemfile
|
16
14
|
- gemfiles/50.gemfile
|
15
|
+
matrix:
|
16
|
+
exclude:
|
17
|
+
- rvm: 2.0.0
|
18
|
+
gemfile: gemfiles/50.gemfile
|
19
|
+
- rvm: 2.1.8
|
20
|
+
gemfile: gemfiles/50.gemfile
|
21
|
+
- rvm: rbx-2
|
22
|
+
gemfile: gemfiles/50.gemfile
|
17
23
|
cache:
|
18
24
|
- bundler
|
data/README.md
CHANGED
@@ -3,11 +3,20 @@
|
|
3
3
|
[](https://codeclimate.com/github/owahab/uniqueness)
|
4
4
|
[](https://travis-ci.org/owahab/uniqueness)
|
5
5
|
[](https://coveralls.io/github/owahab/uniqueness?branch=master)
|
6
|
+
[](http://inch-ci.org/github/owahab/uniqueness)
|
7
|
+
[](https://hakiri.io/github/owahab/uniqueness/master)
|
6
8
|
|
7
9
|
Rails recently introduced `has_secure_token` but it's very primitive.
|
8
10
|
Meet the competition.
|
9
11
|
|
10
|
-
[Documentation](http://www.rubydoc.info/github/owahab/uniqueness)
|
12
|
+
[Code Documentation](http://www.rubydoc.info/github/owahab/uniqueness)
|
13
|
+
|
14
|
+
## Requirements
|
15
|
+
|
16
|
+
Minimum requirements are:
|
17
|
+
|
18
|
+
1. Rails __4.0.0+__
|
19
|
+
2. Ruby __2.0.0+__
|
11
20
|
|
12
21
|
## Installation
|
13
22
|
|
@@ -27,7 +36,26 @@ Or install it yourself as:
|
|
27
36
|
|
28
37
|
## Usage
|
29
38
|
|
39
|
+
Adds random field support to Rails models.
|
40
|
+
|
41
|
+
To auto-generate a new random string for field `foo`:
|
42
|
+
|
43
|
+
class Example < ActiveRecord::Base
|
44
|
+
has_unique_field :foo
|
45
|
+
end
|
46
|
+
|
47
|
+
You can customize the generated string by
|
48
|
+
passing an options hash. The following keys are supported:
|
30
49
|
|
50
|
+
`:length` number of characters, defaults to __32__
|
51
|
+
|
52
|
+
`:type` type of string, can be one of: `:human`, `:auto`, defaults to __:auto__
|
53
|
+
|
54
|
+
Human type generates strings easier to read by excluding ambiguous characters like `1, 5, 8, B, o, O, I, l, s, u`.
|
55
|
+
|
56
|
+
`:blacklist` characters to exclude when generating the random string, defaults to __[]__
|
57
|
+
|
58
|
+
`:scope` scopes, defines the `ActiveRecord` `scope` applied before calculating the `position` field value. Defaults to __[]__
|
31
59
|
|
32
60
|
## Development
|
33
61
|
|
@@ -40,4 +68,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
40
68
|
Please see CONTRIBUTING.md for details.
|
41
69
|
|
42
70
|
## Credits
|
43
|
-
|
71
|
+
|
72
|
+
[](http://eventtus.com)
|
73
|
+
|
74
|
+
Project is sponsored by [Eventtus](http://eventtus.com).
|
data/lib/uniqueness/engine.rb
CHANGED
data/lib/uniqueness/model.rb
CHANGED
@@ -2,6 +2,8 @@ module Uniqueness
|
|
2
2
|
module Model # :nodoc:
|
3
3
|
def self.included(base)
|
4
4
|
base.extend ClassMethods
|
5
|
+
# Track all uniqueness arguments
|
6
|
+
base.class_attribute :uniqueness_options
|
5
7
|
end
|
6
8
|
|
7
9
|
module ClassMethods
|
@@ -18,28 +20,45 @@ module Uniqueness
|
|
18
20
|
#
|
19
21
|
# +:case_sensitive+ defaults to <tt>true</tt>
|
20
22
|
#
|
21
|
-
# +:type+ type of string, defaults to <tt>:
|
22
|
-
# can be one of: <tt>:human</tt>, <tt>:
|
23
|
+
# +:type+ type of string, defaults to <tt>:auto</tt>
|
24
|
+
# can be one of: <tt>:human</tt>, <tt>:auto</tt>
|
23
25
|
#
|
24
|
-
# +:blacklist+ characters to exclude when generating the random
|
25
|
-
#
|
26
|
+
# +:blacklist+ characters to exclude when generating the random
|
27
|
+
# string, defaults to <tt>[]</tt>
|
28
|
+
#
|
29
|
+
# +:scope+ defines the `ActiveRecord` `scope` applied before
|
30
|
+
# calculating the `position` field value.
|
31
|
+
# defaults to <tt>[]</tt>
|
26
32
|
def has_unique_field(name, options = {})
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
self.uniqueness_options ||= {}
|
34
|
+
self.uniqueness_options[name] = self.uniqueness_default_options.merge(options)
|
35
|
+
before_validation :uniqueness_generate
|
36
|
+
validate :uniqueness_validation
|
37
|
+
end
|
38
|
+
|
39
|
+
# Default sorting options
|
40
|
+
def uniqueness_default_options
|
41
|
+
{
|
42
|
+
length: 32,
|
43
|
+
type: :auto,
|
44
|
+
blacklist: [],
|
45
|
+
scope: []
|
46
|
+
}
|
32
47
|
end
|
33
48
|
end
|
34
49
|
|
35
50
|
# Generates a new code based on given options
|
36
|
-
def uniqueness_generate
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
51
|
+
def uniqueness_generate
|
52
|
+
self.uniqueness_options.each do |field, options|
|
53
|
+
value = send(field)
|
54
|
+
unless value.present?
|
55
|
+
dict = uniqueness_dictionary - options[:blacklist]
|
56
|
+
dict -= [*(:A..:Z)].map(&:to_s) unless options[:case_sensitive]
|
57
|
+
dict -= uniqueness_ambigious_dictionary if options[:type].to_sym == :human
|
58
|
+
value = Array.new(options[:length]).map { dict[rand(dict.length)] }.join
|
59
|
+
self.send("#{field}=", value)
|
60
|
+
end
|
61
|
+
end
|
43
62
|
end
|
44
63
|
|
45
64
|
# Dictionary used for uniqueness generation
|
@@ -48,7 +67,24 @@ module Uniqueness
|
|
48
67
|
end
|
49
68
|
|
50
69
|
def uniqueness_ambigious_dictionary
|
51
|
-
[
|
70
|
+
[:b, :B, :o, :O, :q, :I, :l, :L, :s, :S, :u, :U, :z, :Z].map(&:to_s)
|
71
|
+
end
|
72
|
+
|
73
|
+
def uniqueness_validation
|
74
|
+
self.class.uniqueness_options.each do |field, options|
|
75
|
+
value = send(field)
|
76
|
+
if value.nil?
|
77
|
+
errors.add(field, 'should not be empty')
|
78
|
+
else
|
79
|
+
conditions = {}
|
80
|
+
options[:scope].each do |s|
|
81
|
+
conditions[s] = send(s)
|
82
|
+
end
|
83
|
+
conditions[field] = value
|
84
|
+
query = self.class.where(conditions)
|
85
|
+
errors.add(field, 'should be unique') if query.any?
|
86
|
+
end
|
87
|
+
end
|
52
88
|
end
|
53
89
|
end
|
54
90
|
end
|
data/lib/uniqueness/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uniqueness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Abdel-Wahab
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|