uniqable 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +4 -0
- data/README.md +2 -0
- data/lib/uniqable.rb +19 -6
- data/lib/uniqable/version.rb +1 -1
- data/uniqable.gemspec +2 -0
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 614ad5000574685a9eae9222e26c04898a03b22e
|
4
|
+
data.tar.gz: ff0f29efadbf39c75d7b2340af4a3cf628fc3e78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae466818b9f47c24b1a9a0d5359f37cbde6e189a6ac602c72204d585778323b75a32b02ffa97bbca00ec20f08f8ea7c1284936463043659801ec45dd832a38e0
|
7
|
+
data.tar.gz: c34d784116d1c532248f3aed6884a4ddc5cd8008f2fe2d08ce81f8ce67c3ec93c800fc963ff7b452d635c0ab34521878be3cf12b0e7c477304a1c2fa01d586ef
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -21,6 +21,7 @@ GEM
|
|
21
21
|
tzinfo (~> 1.1)
|
22
22
|
arel (5.0.1.20140414130214)
|
23
23
|
ast (2.4.0)
|
24
|
+
awesome_print (1.8.0)
|
24
25
|
builder (3.2.3)
|
25
26
|
concurrent-ruby (1.0.5)
|
26
27
|
diff-lcs (1.3)
|
@@ -57,6 +58,7 @@ GEM
|
|
57
58
|
ruby-progressbar (~> 1.7)
|
58
59
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
59
60
|
ruby-progressbar (1.9.0)
|
61
|
+
sqlite3 (1.3.13)
|
60
62
|
thread_safe (0.3.6)
|
61
63
|
tzinfo (1.2.5)
|
62
64
|
thread_safe (~> 0.1)
|
@@ -67,11 +69,13 @@ PLATFORMS
|
|
67
69
|
|
68
70
|
DEPENDENCIES
|
69
71
|
activerecord (~> 4.1.14.1)
|
72
|
+
awesome_print
|
70
73
|
bundler (~> 1.16)
|
71
74
|
rake (~> 10.0)
|
72
75
|
rspec (~> 3.0)
|
73
76
|
rspec_junit_formatter
|
74
77
|
rubocop (~> 0.49)
|
78
|
+
sqlite3
|
75
79
|
uniqable!
|
76
80
|
|
77
81
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -41,6 +41,8 @@ if you want you can use `:to_param` option to generate automatically `#to_param`
|
|
41
41
|
anyway you have one more method `.find_uniqable` which one you can use to find your model record
|
42
42
|
```ruby
|
43
43
|
MyModel.find_uniqable params[:uid]
|
44
|
+
# or
|
45
|
+
MyModel.find_uniqable! params[:uid] # this will raise exception ActiveRecord::RecordNotFound if record is absent
|
44
46
|
```
|
45
47
|
and another one is `.uniqable_fields`
|
46
48
|
```ruby
|
data/lib/uniqable.rb
CHANGED
@@ -8,10 +8,12 @@ module Uniqable
|
|
8
8
|
base.extend ClassMethods
|
9
9
|
end
|
10
10
|
|
11
|
+
# Methods going to be AR model's methods
|
11
12
|
module ClassMethods
|
12
13
|
# Uniqable fields and options declaration
|
13
14
|
# @example:
|
14
15
|
# uniqable :uid, :slug, to_param: :uid
|
16
|
+
# rubocop:disable Metrics/MethodLength
|
15
17
|
def uniqable(*fields, to_param: nil)
|
16
18
|
fields = [:uid] if fields.blank?
|
17
19
|
fields.each do |name|
|
@@ -21,12 +23,18 @@ module Uniqable
|
|
21
23
|
fields
|
22
24
|
end
|
23
25
|
|
24
|
-
if to_param # :to_param option
|
25
|
-
|
26
|
-
|
27
|
-
end
|
26
|
+
return if to_param.blank? # :to_param option
|
27
|
+
define_method :to_param do
|
28
|
+
public_send(to_param)
|
28
29
|
end
|
29
30
|
end
|
31
|
+
# rubocop:enable Metrics/MethodLength
|
32
|
+
|
33
|
+
# @return [self]
|
34
|
+
def where_uniqable(uid)
|
35
|
+
where_sql = uniqable_fields.map { |r| "#{table_name}.#{r} = :uid" }.join(' OR ')
|
36
|
+
where(where_sql, uid: uid)
|
37
|
+
end
|
30
38
|
|
31
39
|
# Find record by one of the uniq field
|
32
40
|
# usage @example:
|
@@ -35,8 +43,13 @@ module Uniqable
|
|
35
43
|
# MyModel.find_uniqable params[:uid] # can be uid or slug column
|
36
44
|
# @return [self]
|
37
45
|
def find_uniqable(uid)
|
38
|
-
|
39
|
-
|
46
|
+
where_uniqable(uid).take
|
47
|
+
end
|
48
|
+
|
49
|
+
# Same as method above just raise exception if nothing is there
|
50
|
+
# @return [self]
|
51
|
+
def find_uniqable!(uid)
|
52
|
+
where_uniqable(uid).take!
|
40
53
|
end
|
41
54
|
end
|
42
55
|
|
data/lib/uniqable/version.rb
CHANGED
data/uniqable.gemspec
CHANGED
@@ -23,8 +23,10 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
25
|
spec.add_development_dependency 'activerecord', '~> 4.1.14.1'
|
26
|
+
spec.add_development_dependency 'awesome_print'
|
26
27
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
28
|
spec.add_development_dependency 'rake', '~> 10.0'
|
28
29
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
30
|
spec.add_development_dependency 'rubocop', '~> 0.49'
|
31
|
+
spec.add_development_dependency 'sqlite3'
|
30
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uniqable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Renat "MpaK" Ibragimov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.1.14.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: awesome_print
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,20 @@ dependencies:
|
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0.49'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: Generate a unique, random token for ActiveRecord models.
|
84
112
|
email:
|
85
113
|
- mrak69@gmail.com
|