uniqable 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -3
- data/lib/uniqable.rb +24 -1
- data/lib/uniqable/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: 9c62d651fd862f7f16f28da78984646b6f058a3a
|
4
|
+
data.tar.gz: 22ed697e5d197cc2ec2cd51ace98d8cd222b6d59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02aea5d916ad5f4a834847f1daf4ce3dcbd678f7e117695465df991c2f33c05df5881c75e4969156d9dadfbb88a4e172bc478c68da6186fc05d4c6ca66af9fb1
|
7
|
+
data.tar.gz: aac1283ae40d4095ce7408edcf7de102e8bad63dda1ea096ee8819ee17a24b782da18a91ea38a386c0f3dda928eb4afc68b2f3d5520e90bd1221a4defcc9be8f
|
data/README.md
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
[![CircleCI](https://circleci.com/gh/mpakus/uniqable.svg?style=svg)](https://circleci.com/gh/mpakus/uniqable)
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
TODO: Delete this and the text above, and describe your gem
|
5
|
+
Ruby on Rails gem for generating a unique, random token in an ActiveRecord model.
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
@@ -35,6 +33,16 @@ First include Uniqable concern to your model and then describe columns which you
|
|
35
33
|
|
36
34
|
It generates unique and random token before each Model instance is created.
|
37
35
|
|
36
|
+
if you want you can use `:to_param` option to generate automatically `#to_param` method
|
37
|
+
```ruby
|
38
|
+
uniqable :uid, :slug, to_param: :uid
|
39
|
+
```
|
40
|
+
|
41
|
+
anyway you have one more method `.find_uniqable` which one you can use to find your model record
|
42
|
+
```ruby
|
43
|
+
MyModel.find_uniqable params[:uid]
|
44
|
+
```
|
45
|
+
|
38
46
|
You can also create your own token callback method and set the field:
|
39
47
|
|
40
48
|
```ruby
|
data/lib/uniqable.rb
CHANGED
@@ -9,14 +9,37 @@ module Uniqable
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
|
12
|
+
# Uniqable fields and options declaration
|
13
|
+
# @example:
|
14
|
+
# uniqable :uid, :slug, to_param: :uid
|
15
|
+
def uniqable(*fields, to_param: nil)
|
13
16
|
fields = [:uid] if fields.blank?
|
17
|
+
@_uniqable_fields = fields
|
14
18
|
fields.each do |name|
|
15
19
|
before_create { |record| record.uniqable_uid(name) }
|
16
20
|
end
|
21
|
+
# :to_param option
|
22
|
+
if to_param
|
23
|
+
define_method :to_param do
|
24
|
+
public_send(to_param)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Find record by one of the uniq field
|
30
|
+
# usage @example:
|
31
|
+
# uniqable :uid, :slug
|
32
|
+
# ...
|
33
|
+
# MyModel.find_uniqable params[:uid] # can be uid or slug column
|
34
|
+
# @return [self]
|
35
|
+
def find_uniqable(uid)
|
36
|
+
where_sql = @_uniqable_fields.map{ |r| "#{table_name}.#{r} = :uid"}.join(' OR ')
|
37
|
+
self.where(where_sql, uid: uid).take(1)
|
17
38
|
end
|
18
39
|
end
|
19
40
|
|
41
|
+
# Generate and set random and uniq field
|
42
|
+
# @TODO: split into 2 actions generate and set
|
20
43
|
def uniqable_uid(field)
|
21
44
|
loop do
|
22
45
|
send("#{field}=", SecureRandom.hex(8))
|
data/lib/uniqable/version.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.1
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|