hashids_uri 0.1.1 → 0.1.2
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/README.md +5 -1
- data/lib/hashids_uri/version.rb +1 -1
- data/lib/hashids_uri.rb +27 -18
- 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: 53d72563690a2848294f1bdc7a42bdf820efa41e
|
4
|
+
data.tar.gz: b99671e3614e0ecd8fa3590fc92a0ad131f43a5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef0248aaf4c2c85809b81137f6b60be9628d6249e846b67502822e6a7a1e4d30a292f8f95621e3b21fc75b50875ad690f9967d956accb3d27b6a62b59a89db68
|
7
|
+
data.tar.gz: 7ef6798511f4da6d9e044b0e0756424d4df337e3bc25e3faf6b124aac03bbcbcaf9efbd0347a778f9965179b0ea9acc871d832b48115152b75a4aa6207cf27b0
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
[](https://travis-ci.org/danieldraper/hashids_uri)
|
2
|
+
[](https://gemnasium.com/danieldraper/hashids_uri)
|
3
|
+
[](https://codeclimate.com/github/danieldraper/hashids_uri)
|
4
|
+
|
1
5
|
# HashidsUri
|
2
6
|
|
3
|
-
HashidsUri allows you to easily use (
|
7
|
+
HashidsUri allows you to easily use [Hashids](http://hashids.org/) in your Ruby on Rails URLs via the [Hashids Gem](https://github.com/peterhellberg/hashids.rb). You no longer need to expose sequential IDs to your users and you can do all this without changing your primary key to a UUID or some custom database level function.
|
4
8
|
|
5
9
|
An example URL using HashidsUri
|
6
10
|
```http
|
data/lib/hashids_uri/version.rb
CHANGED
data/lib/hashids_uri.rb
CHANGED
@@ -3,29 +3,26 @@ require 'active_record'
|
|
3
3
|
|
4
4
|
module ActiveRecord
|
5
5
|
module HashidsUri
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attr_accessor :salt, :min_length
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.send :include, InstanceMethods
|
10
9
|
end
|
11
10
|
|
12
11
|
module ClassMethods
|
13
12
|
def has_hashids_uri(options = {})
|
14
|
-
|
15
|
-
ActiveRecord::HashidsUri.min_length = options.fetch(:min_length, 6)
|
13
|
+
class_attribute :salt, :min_length
|
16
14
|
|
17
|
-
|
15
|
+
self.salt = options.fetch(:salt, '')
|
16
|
+
self.min_length = options.fetch(:min_length, 6)
|
18
17
|
end
|
19
|
-
end
|
20
18
|
|
21
|
-
module ClassMethods
|
22
19
|
def find_by_hash(hash)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
)
|
20
|
+
decoded_id = ::Hashids.new(
|
21
|
+
self.salt,
|
22
|
+
self.min_length
|
23
|
+
).decode(hash).first
|
24
|
+
|
25
|
+
find(decoded_id)
|
29
26
|
end
|
30
27
|
|
31
28
|
def find(*args)
|
@@ -37,12 +34,24 @@ module ActiveRecord
|
|
37
34
|
end
|
38
35
|
|
39
36
|
module InstanceMethods
|
40
|
-
def
|
37
|
+
def salt
|
38
|
+
self.class.salt
|
39
|
+
end
|
40
|
+
|
41
|
+
def min_length
|
42
|
+
self.class.min_length
|
43
|
+
end
|
44
|
+
|
45
|
+
def hashid
|
41
46
|
::Hashids.new(
|
42
|
-
|
43
|
-
|
47
|
+
salt,
|
48
|
+
min_length
|
44
49
|
).encode(id)
|
45
50
|
end
|
51
|
+
|
52
|
+
def to_param
|
53
|
+
hashid
|
54
|
+
end
|
46
55
|
end
|
47
56
|
end
|
48
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashids_uri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Draper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|