hashid-rails 0.1.2 → 0.2.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/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/README.md +27 -4
- data/hashid-rails.gemspec +1 -0
- data/lib/hashid/rails.rb +37 -2
- data/lib/hashid/rails/version.rb +1 -1
- metadata +16 -3
- data/.travis.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71c60b5b414a413c97192b5c81cc6c0495a71a06
|
4
|
+
data.tar.gz: d4e88b4c3ee0ff8a1412eb19cf7bbae4ee29ed37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c27cbfdf204ae0227beccdc6efeeec7544bb4f283fbe939c67b9266e6fa818f5878f8a49f99edec0b4e409c7b1559e725923c4ea5166abd11395a2e6acaa4ed2
|
7
|
+
data.tar.gz: 39ef44045dd17fd962d869a8cad31ccd0593b9be32bc07cd1fd1f3a5c03ce83ee12a62740070c696a8605c616c1fcdb5cc5035ad80ef90e2067363c55fca0822
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.2.0 (2016-01-02)
|
4
|
+
|
5
|
+
- Customize the Hashid seed and length using a configuration initializer.
|
6
|
+
- Add test coverage
|
7
|
+
- Fix issue where calling `.reload` on model retries to `decode_id`.
|
8
|
+
|
3
9
|
## 0.1.2 (2015-04-12)
|
4
10
|
|
5
11
|
- Let `Model#find` work with integers passed in as strings.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# Hashid Rails
|
2
|
+
[ ](https://codeship.com/projects/124848)
|
3
|
+
[](https://codeclimate.com/github/jcypret/hashid-rails)
|
4
|
+
[](https://codeclimate.com/github/jcypret/hashid-rails/coverage)
|
2
5
|
|
3
|
-
This gem allows you to easily use [Hashids](http://hashids.org/ruby/) in your
|
6
|
+
This gem allows you to easily use [Hashids](http://hashids.org/ruby/) in your
|
7
|
+
Rails app. Instead of your models using sequential numbers like 1, 2, 3, they
|
8
|
+
will instead have unique short hashes like "yLA6m0oM", "5bAyD0LO", and
|
9
|
+
"wz3MZ49l". The database will still use integers under the hood, so this gem can
|
10
|
+
be added or removed at any time.
|
4
11
|
|
5
12
|
## Installation
|
6
13
|
|
@@ -23,14 +30,30 @@ Or install it yourself as:
|
|
23
30
|
Just use `Model#find` passing in the hashid instead of the model id:
|
24
31
|
|
25
32
|
```ruby
|
26
|
-
@person =
|
33
|
+
@person = Person.find(params[:hashid])
|
34
|
+
```
|
35
|
+
|
36
|
+
## Configuration
|
37
|
+
|
38
|
+
To customize the Hashids seed and ensure that another user of the gem cannot
|
39
|
+
easily reverse engineer your ids, create an initializer and:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Hashid::Rails.configure do |config|
|
43
|
+
config.secret = 'my secret'
|
44
|
+
config.length = 6
|
45
|
+
end
|
27
46
|
```
|
28
47
|
|
29
48
|
## Development
|
30
49
|
|
31
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
51
|
+
`bin/console` for an interactive prompt that will allow you to experiment.
|
32
52
|
|
33
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
54
|
+
release a new version, update the version number in `version.rb`, and then run
|
55
|
+
`bundle exec rake release` to create a git tag for the version, push git commits
|
56
|
+
and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
57
|
|
35
58
|
## Contributing
|
36
59
|
|
data/hashid-rails.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler', '~> 1.9'
|
29
29
|
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.4.0'
|
30
31
|
|
31
32
|
spec.add_runtime_dependency 'activerecord', '~> 4.0'
|
32
33
|
spec.add_runtime_dependency 'hashids', '~> 1.0'
|
data/lib/hashid/rails.rb
CHANGED
@@ -4,6 +4,22 @@ require 'active_record'
|
|
4
4
|
|
5
5
|
module Hashid
|
6
6
|
module Rails
|
7
|
+
|
8
|
+
# Get configuration or load defaults
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set configuration settings with a block
|
14
|
+
def self.configure
|
15
|
+
yield(configuration)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Reset gem configuration to defaults
|
19
|
+
def self.reset
|
20
|
+
@configuration = Configuration.new
|
21
|
+
end
|
22
|
+
|
7
23
|
def self.included(base)
|
8
24
|
base.extend ClassMethods
|
9
25
|
end
|
@@ -18,8 +34,11 @@ module Hashid
|
|
18
34
|
alias_method :hashid, :to_param
|
19
35
|
|
20
36
|
module ClassMethods
|
37
|
+
|
21
38
|
def hashids
|
22
|
-
|
39
|
+
secret = Hashid::Rails.configuration.secret
|
40
|
+
length = Hashid::Rails.configuration.length
|
41
|
+
Hashids.new("#{table_name}#{secret}", length)
|
23
42
|
end
|
24
43
|
|
25
44
|
def encode_id(id)
|
@@ -31,9 +50,25 @@ module Hashid
|
|
31
50
|
end
|
32
51
|
|
33
52
|
def find(hashid)
|
34
|
-
super decode_id(hashid) || hashid
|
53
|
+
model_reload? ? super(hashid) : super( decode_id(hashid) || hashid )
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def model_reload?
|
59
|
+
caller.any? {|s| s =~ /active_record\/persistence.*reload/}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Configuration
|
64
|
+
attr_accessor :secret, :length
|
65
|
+
|
66
|
+
def initialize
|
67
|
+
@secret = ''
|
68
|
+
@length = 6
|
35
69
|
end
|
36
70
|
end
|
71
|
+
|
37
72
|
end
|
38
73
|
end
|
39
74
|
|
data/lib/hashid/rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashid-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Cypret
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.4.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.4.0
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: activerecord
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,7 +94,6 @@ extra_rdoc_files: []
|
|
80
94
|
files:
|
81
95
|
- ".gitignore"
|
82
96
|
- ".rspec"
|
83
|
-
- ".travis.yml"
|
84
97
|
- CHANGELOG.md
|
85
98
|
- Gemfile
|
86
99
|
- LICENSE.txt
|
data/.travis.yml
DELETED