chimeon-hashid-rails 1.3.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 +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.rubocop.yml +15 -0
- data/CHANGELOG.md +73 -0
- data/Gemfile +11 -0
- data/Guardfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +152 -0
- data/Rakefile +9 -0
- data/bin/console +15 -0
- data/bin/setup +7 -0
- data/chimeon-hashid-rails.gemspec +41 -0
- data/lib/hashid/rails.rb +137 -0
- data/lib/hashid/rails/configuration.rb +31 -0
- data/lib/hashid/rails/version.rb +7 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0e9cb2826518cd4dea75a04162f0101a8cf1a0c58d5f182db462168a55323b93
|
4
|
+
data.tar.gz: fb201f5cc45fd4c5fbd2cd1cc385ad23dd6f779b8f121bc858ae50bd671e5f48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9034d83046a9255b8073821bcc0cb5496012672c4a5660e5d376b21f1b5ee3c124adcdf398397c58c2e2e85f8e085afd2b3630e5c3495f392763e0b32ab7abf1
|
7
|
+
data.tar.gz: 1c5c6e24548d8f1b1c2666746db5e6d3012d9def2a670d7794b3f11751858081ca92f4e687bc4b987facee3bb5c1d7d82a490fd07d3c89733bdd0105b07098a3
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format documentation --color --require spec_helper --tag focus
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Layout/IndentFirstArrayElement:
|
4
|
+
EnforcedStyle: consistent
|
5
|
+
Metrics/BlockLength:
|
6
|
+
Exclude:
|
7
|
+
- "*.gemspec"
|
8
|
+
- "spec/**/*.rb"
|
9
|
+
Naming/PredicateName:
|
10
|
+
NameWhitelist:
|
11
|
+
- has_many
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
Style/StringLiterals:
|
15
|
+
EnforcedStyle: double_quotes
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.2.2 (2018-07-29)
|
4
|
+
### Fixed
|
5
|
+
- Handle exception raised when using a letter-only alphabet and attempting to
|
6
|
+
decode an integer ID from [@Drakula2k](https://github.com/Drakula2k) ([#54](https://github.com/jcypret/hashid-rails/pull/54)).
|
7
|
+
|
8
|
+
## 1.2.1 (2018-01-13)
|
9
|
+
- Found issue where unsigned hashids with `find` did not fall back to passed in ID ([#46](https://github.com/jcypret/hashid-rails/pull/46)).
|
10
|
+
- Move finder specs to a shared example run against both the signed and unsigned hashids.
|
11
|
+
|
12
|
+
## 1.2.0 (2017-11-17)
|
13
|
+
- Fix regression where `find_by_hashid` and `find_by_hashid!` attempt to decode
|
14
|
+
values that are not hashids. ([#41](https://github.com/jcypret/hashid-rails/pull/41))
|
15
|
+
|
16
|
+
## 1.1.1 (2017-11-03)
|
17
|
+
- Fix eager loading and finding records through a parent. ([#39](https://github.com/jcypret/hashid-rails/pull/39))
|
18
|
+
|
19
|
+
## 1.1.0 (2017-10-04)
|
20
|
+
- Add option to disable hashid signing. This adds backwards compatibility with
|
21
|
+
pre-1.0 releases. Thanks [@olliebennett](https://github.com/olliebennett)! ([#37](https://github.com/jcypret/hashid-rails/pull/37))
|
22
|
+
- Add note to README about upgrading from pre-1.0 releases.
|
23
|
+
|
24
|
+
## 1.0.0 (2017-04-29)
|
25
|
+
- Sign hashids to prevent accidentally decoded regular ids
|
26
|
+
- Require explicitly including Hashid::Rails in models
|
27
|
+
- Improve support for model associations
|
28
|
+
- Rename config variables to better match hashids project
|
29
|
+
- Improve overall test coverage
|
30
|
+
|
31
|
+
## 0.7.0 (2017-02-15)
|
32
|
+
- Add configuration option to disable overriding default `find` (#22).
|
33
|
+
|
34
|
+
## 0.6.0 (2017-01-07)
|
35
|
+
- Add Rubocop and adjust styles to be consistent.
|
36
|
+
- Fix issue where finding multiple non-hashids returns an array of nils.
|
37
|
+
- Switch over testing to use SQLite for more accurate db interactions.
|
38
|
+
|
39
|
+
## 0.5.0 (2016-10-15)
|
40
|
+
- Update specs to support Rails 5.x series.
|
41
|
+
|
42
|
+
## 0.4.1 (2016-08-21)
|
43
|
+
- Limit installations to Rails 4.x; gem is not yet Rails 5 compatible.
|
44
|
+
|
45
|
+
## 0.4.0 (2016-08-21)
|
46
|
+
- Add `find_by_hashid` method to always try and decode, as opposed to `find` which tries to find it as an integer first.
|
47
|
+
|
48
|
+
## 0.3.2 (2016-03-30)
|
49
|
+
- Multiple ids can be passed to `find` method.
|
50
|
+
|
51
|
+
## 0.3.1 (2016-03-10)
|
52
|
+
- Update Rails dependency to work with Rails 4.0 and up.
|
53
|
+
|
54
|
+
## 0.3.0 (2016-03-10)
|
55
|
+
- Customize the alphabet used for Hashids.
|
56
|
+
|
57
|
+
## 0.2.0 (2016-01-02)
|
58
|
+
|
59
|
+
- Customize the Hashid seed and length using a configuration initializer.
|
60
|
+
- Add test coverage
|
61
|
+
- Fix issue where calling `.reload` on model retries to `decode_id`.
|
62
|
+
|
63
|
+
## 0.1.2 (2015-04-12)
|
64
|
+
|
65
|
+
- Let `Model#find` work with integers passed in as strings.
|
66
|
+
|
67
|
+
## 0.1.1 (2015-04-12)
|
68
|
+
|
69
|
+
- Let `Model#find` work with integer model ids as well as hashids.
|
70
|
+
|
71
|
+
## 0.1.0 (2015-04-12)
|
72
|
+
|
73
|
+
- Initial Release
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
guard_options = {
|
2
|
+
all_after_pass: false,
|
3
|
+
all_on_start: false,
|
4
|
+
cmd: "bundle exec rspec"
|
5
|
+
}
|
6
|
+
|
7
|
+
guard :rspec, guard_options do
|
8
|
+
watch(%r{^spec/.+_spec\.rb$})
|
9
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
10
|
+
watch("spec/spec_helper.rb") { "spec" }
|
11
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Justin Cypret
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
# Hashid Rails
|
2
|
+
[](https://badge.fury.io/rb/hashid-rails)
|
3
|
+
[](https://travis-ci.org/jcypret/hashid-rails)
|
4
|
+
[](https://codeclimate.com/github/jcypret/hashid-rails)
|
5
|
+
[](https://codeclimate.com/github/jcypret/hashid-rails/coverage)
|
6
|
+
|
7
|
+
This gem allows you to easily use [Hashids](http://hashids.org/ruby/) in your
|
8
|
+
Rails app. Instead of your models using sequential numbers like 1, 2, 3, they
|
9
|
+
will instead have unique short hashes like "yLA6m0oM", "5bAyD0LO", and
|
10
|
+
"wz3MZ49l". The database will still use integers under the hood, so this gem can
|
11
|
+
be added or removed at any time.
|
12
|
+
|
13
|
+
> IMPORTANT: If you need to maintain the same hashids from a pre-1.0 release,
|
14
|
+
> read the [upgrade notes](#upgrading-from-pre-10).
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem "hashid-rails", "~> 1.0"
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
```shell
|
27
|
+
$ bundle
|
28
|
+
```
|
29
|
+
|
30
|
+
## Basic Usage
|
31
|
+
|
32
|
+
1. Include Hashid Rails in the ActiveRecord model you'd like to enable hashids.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class Model < ActiveRecord::Base
|
36
|
+
include Hashid::Rails
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
2. Continue using `Model#find` passing in either a hashid or regular 'ol id.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
@person = Person.find(params[:hashid])
|
44
|
+
```
|
45
|
+
|
46
|
+
## Get hashid of model
|
47
|
+
|
48
|
+
You can access the hashid of any model using the `hashid` method.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
model = Model.find(params[:hashid])
|
52
|
+
#=> <Model>
|
53
|
+
model.hashid
|
54
|
+
#=> "yLA6m0oM"
|
55
|
+
```
|
56
|
+
|
57
|
+
Additionally, the `to_param` method is overriden to use hashid instead of id.
|
58
|
+
This means methods that take advantage of implicit ID will automatically work
|
59
|
+
with hashids.
|
60
|
+
|
61
|
+
```erb
|
62
|
+
Passing a hashid model to `link_to`…
|
63
|
+
<%= link_to "Model", model %>
|
64
|
+
|
65
|
+
will use `hashid` instead of `id`.
|
66
|
+
<a href="/models/yLA6m0oM">Model</a>
|
67
|
+
```
|
68
|
+
|
69
|
+
## Alternative Usage
|
70
|
+
|
71
|
+
You can use the `Model#find_by_hashid` method to find a record without falling
|
72
|
+
back on the standard `find` method.
|
73
|
+
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
# When a record is found, it returns the record.
|
77
|
+
@person = Person.find_by_hashid(params[:hashid])
|
78
|
+
#=> <Person>
|
79
|
+
|
80
|
+
# When no record, it returns nil
|
81
|
+
@person = Person.find_by_hashid(params[:hashid])
|
82
|
+
#=> nil
|
83
|
+
|
84
|
+
# A bang (!) version is also available and raises an exception when not found.
|
85
|
+
@person = Person.find_by_hashid!(params[:hashid])
|
86
|
+
#=> ActiveRecord::RecordNotFound
|
87
|
+
```
|
88
|
+
|
89
|
+
## Configuration (optional)
|
90
|
+
|
91
|
+
You can add an initializer for Hashid::Rails to customize the options passed to
|
92
|
+
the Hashids gem. This is completely optional. The configuration below shows the
|
93
|
+
default options.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
Hashid::Rails.configure do |config|
|
97
|
+
# The salt to use for generating hashid. Prepended with table name.
|
98
|
+
config.salt = ""
|
99
|
+
|
100
|
+
# The minimum length of generated hashids
|
101
|
+
config.min_hash_length = 6
|
102
|
+
|
103
|
+
# The alphabet to use for generating hashids
|
104
|
+
config.alphabet = "abcdefghijklmnopqrstuvwxyz" \
|
105
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
106
|
+
"1234567890"
|
107
|
+
|
108
|
+
# Whether to override the `find` method
|
109
|
+
config.override_find = true
|
110
|
+
|
111
|
+
# Whether to sign hashids to prevent conflicts with regular IDs (see https://github.com/jcypret/hashid-rails/issues/30)
|
112
|
+
config.sign_hashids = true
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
## Upgrading from Pre-1.0
|
117
|
+
|
118
|
+
The 1.0 release of this gem introduced hashid signing to prevent
|
119
|
+
conflicts with database IDs that could be mis-interpreted as hashids.
|
120
|
+
IDs are now signed when encoding and the signature verified when decoding.
|
121
|
+
The trade off is that hashids are different than in previous versions due to the added signature.
|
122
|
+
If you need to maintain the same hashids from a pre-1.0 version, set `sign_hashids` to false in the config.
|
123
|
+
|
124
|
+
Additionally, some of the config names have been modified to better match the parent [Hashid](https://github.com/peterhellberg/hashids.rb) project.
|
125
|
+
The config `secret` has been renamed to `salt` and the `length` renamed to `min_hash_length`.
|
126
|
+
Update the initializer config accordingly.
|
127
|
+
|
128
|
+
Lastly, `Hashid::Rails` is no longer imported into `ActiveRecord::Base` by default.
|
129
|
+
You can instead include `Hashid::Rails` selectively in the desired models,
|
130
|
+
or include it in `ApplicationRecord` for Rails 5 to apply to all subclassed models,
|
131
|
+
or add an initializer with `ActiveRecord::Base.send :include, Hashid::Rails` to match previous behavior.
|
132
|
+
|
133
|
+
## Development
|
134
|
+
|
135
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
136
|
+
`bin/console` for an interactive prompt that will allow you to experiment.
|
137
|
+
|
138
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
139
|
+
release a new version, update the version number in `version.rb`, and then run
|
140
|
+
`bundle exec rake release` to create a git tag for the version, push git commits
|
141
|
+
and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
142
|
+
|
143
|
+
## Contributing
|
144
|
+
|
145
|
+
1. Fork it ( https://github.com/[my-github-username]/hashid-rails/fork )
|
146
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
147
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
148
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
149
|
+
5. Create a new Pull Request
|
150
|
+
|
151
|
+
> NOTE: If it's a significant feature or change, consider creating an Issue for
|
152
|
+
> discussion before opening a PR.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "hashid/rails"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "hashid/rails/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "chimeon-hashid-rails"
|
9
|
+
spec.version = Hashid::Rails::VERSION
|
10
|
+
spec.authors = ["Team ChimeOn"]
|
11
|
+
spec.email = ["support@chimeon.com"]
|
12
|
+
|
13
|
+
spec.summary = "Fork of Justin Cypret work. Use Hashids in your Rails app models."
|
14
|
+
spec.description = <<-DESCRIPTION
|
15
|
+
This gem allows you to easily use [Hashids](http://hashids.org/ruby/)
|
16
|
+
in your Rails app. Instead of your models using sequential numbers like 1,
|
17
|
+
2, 3, they will instead have unique short hashes like "yLA6m0oM",
|
18
|
+
"5bAyD0LO", and "wz3MZ49l". The database will still use integers under
|
19
|
+
the hood, so this gem can be added or removed at any time.
|
20
|
+
DESCRIPTION
|
21
|
+
spec.homepage = "https://github.com/jcypret/hashid-rails"
|
22
|
+
spec.license = "MIT"
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`
|
25
|
+
.split("\x0")
|
26
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler"
|
32
|
+
spec.add_development_dependency "byebug"
|
33
|
+
spec.add_development_dependency "rake"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.4.0"
|
35
|
+
spec.add_development_dependency "rubocop"
|
36
|
+
spec.add_development_dependency "simplecov"
|
37
|
+
spec.add_development_dependency "sqlite3"
|
38
|
+
|
39
|
+
spec.add_runtime_dependency "activerecord", ">= 4.0"
|
40
|
+
spec.add_runtime_dependency "hashids", "~> 1.0"
|
41
|
+
end
|
data/lib/hashid/rails.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hashid/rails/version"
|
4
|
+
require "hashid/rails/configuration"
|
5
|
+
require "hashids"
|
6
|
+
require "active_record"
|
7
|
+
|
8
|
+
module Hashid
|
9
|
+
module Rails
|
10
|
+
# Arbitrary value to verify hashid
|
11
|
+
# https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#On_the_Internet_and_in_software
|
12
|
+
HASHID_TOKEN = 42
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.extend ClassMethods
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get configuration or load defaults
|
19
|
+
def self.configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
# Set configuration settings with a block
|
24
|
+
def self.configure
|
25
|
+
yield(configuration)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reset gem configuration to defaults
|
29
|
+
def self.reset
|
30
|
+
@configuration = Configuration.new
|
31
|
+
end
|
32
|
+
|
33
|
+
def hashid
|
34
|
+
self.class.encode_id(id)
|
35
|
+
end
|
36
|
+
alias to_param hashid
|
37
|
+
|
38
|
+
module ClassMethods
|
39
|
+
def relation
|
40
|
+
super.tap { |r| r.extend ClassMethods }
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_many(*args, &block)
|
44
|
+
options = args.extract_options!
|
45
|
+
options[:extend] = Array(options[:extend]).push(ClassMethods)
|
46
|
+
super(*args, options, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def encode_id(ids)
|
50
|
+
if ids.is_a?(Array)
|
51
|
+
ids.map { |id| hashid_encode(id) }
|
52
|
+
else
|
53
|
+
hashid_encode(ids)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# @param ids [String, Integer, Array<Integer, String>] id(s) to decode.
|
58
|
+
# @param fallback [Boolean] indicates whether to return the passed in
|
59
|
+
# id(s) if unable to decode or if already decoded.
|
60
|
+
def decode_id(ids, fallback: false)
|
61
|
+
if ids.is_a?(Array)
|
62
|
+
ids.map { |id| hashid_decode(id, fallback: fallback) }
|
63
|
+
else
|
64
|
+
hashid_decode(ids, fallback: fallback)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def find(*ids)
|
69
|
+
expects_array = ids.first.is_a?(Array)
|
70
|
+
|
71
|
+
uniq_ids = ids.flatten.compact.uniq
|
72
|
+
uniq_ids = uniq_ids.first unless expects_array || uniq_ids.size > 1
|
73
|
+
|
74
|
+
if Hashid::Rails.configuration.override_find
|
75
|
+
super(decode_id(uniq_ids, fallback: true))
|
76
|
+
else
|
77
|
+
super
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def find_by_hashid(hashid)
|
82
|
+
find_by(id: decode_id(hashid, fallback: false))
|
83
|
+
end
|
84
|
+
|
85
|
+
def find_by_hashid!(hashid)
|
86
|
+
find_by!(id: decode_id(hashid, fallback: false))
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def hashids
|
92
|
+
Hashids.new(*Hashid::Rails.configuration.for_table(table_name))
|
93
|
+
end
|
94
|
+
|
95
|
+
def hashid_encode(id)
|
96
|
+
if Hashid::Rails.configuration.sign_hashids
|
97
|
+
find_hashid_prefix + hashids.encode(HASHID_TOKEN, id)
|
98
|
+
else
|
99
|
+
find_hashid_prefix + hashids.encode(id)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def hashid_decode(id, fallback:)
|
104
|
+
fallback_value = fallback ? id : nil
|
105
|
+
decoded_hashid = hashids.decode(id_to_string(id))
|
106
|
+
|
107
|
+
if Hashid::Rails.configuration.sign_hashids
|
108
|
+
valid_hashid?(decoded_hashid) ? decoded_hashid.last : fallback_value
|
109
|
+
else
|
110
|
+
decoded_hashid.first || fallback_value
|
111
|
+
end
|
112
|
+
rescue Hashids::InputError
|
113
|
+
fallback_value
|
114
|
+
end
|
115
|
+
|
116
|
+
def id_to_string(id)
|
117
|
+
safe_id = id.to_s
|
118
|
+
return safe_id unless Hashid::Rails.configuration.use_prefix
|
119
|
+
return safe_id unless safe_id.start_with?(find_hashid_prefix)
|
120
|
+
|
121
|
+
safe_id[find_hashid_prefix.length..safe_id.length]
|
122
|
+
end
|
123
|
+
|
124
|
+
def find_hashid_prefix
|
125
|
+
if Hashid::Rails.configuration.use_prefix && respond_to?(:hashid_prefix)
|
126
|
+
"#{hashid_prefix}#{Hashid::Rails.configuration.hashid_prefix_separator}"
|
127
|
+
else
|
128
|
+
""
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def valid_hashid?(decoded_hashid)
|
133
|
+
decoded_hashid.size == 2 && decoded_hashid.first == HASHID_TOKEN
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hashid
|
4
|
+
module Rails
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :alphabet,
|
7
|
+
:hashid_prefix_separator,
|
8
|
+
:min_hash_length,
|
9
|
+
:override_find,
|
10
|
+
:salt,
|
11
|
+
:sign_hashids,
|
12
|
+
:use_prefix
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@salt = ""
|
16
|
+
@min_hash_length = 6
|
17
|
+
@alphabet = "abcdefghijklmnopqrstuvwxyz" \
|
18
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
19
|
+
"1234567890"
|
20
|
+
@override_find = true
|
21
|
+
@sign_hashids = true
|
22
|
+
@use_prefix = true
|
23
|
+
@hashid_prefix_separator = "!"
|
24
|
+
end
|
25
|
+
|
26
|
+
def for_table(table_name)
|
27
|
+
["#{table_name}#{salt}", min_hash_length, alphabet]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chimeon-hashid-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Team ChimeOn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.4.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.4.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
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'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activerecord
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: hashids
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.0'
|
139
|
+
description: |2
|
140
|
+
This gem allows you to easily use [Hashids](http://hashids.org/ruby/)
|
141
|
+
in your Rails app. Instead of your models using sequential numbers like 1,
|
142
|
+
2, 3, they will instead have unique short hashes like "yLA6m0oM",
|
143
|
+
"5bAyD0LO", and "wz3MZ49l". The database will still use integers under
|
144
|
+
the hood, so this gem can be added or removed at any time.
|
145
|
+
email:
|
146
|
+
- support@chimeon.com
|
147
|
+
executables: []
|
148
|
+
extensions: []
|
149
|
+
extra_rdoc_files: []
|
150
|
+
files:
|
151
|
+
- ".gitignore"
|
152
|
+
- ".rspec"
|
153
|
+
- ".rubocop.yml"
|
154
|
+
- CHANGELOG.md
|
155
|
+
- Gemfile
|
156
|
+
- Guardfile
|
157
|
+
- LICENSE.txt
|
158
|
+
- README.md
|
159
|
+
- Rakefile
|
160
|
+
- bin/console
|
161
|
+
- bin/setup
|
162
|
+
- chimeon-hashid-rails.gemspec
|
163
|
+
- lib/hashid/rails.rb
|
164
|
+
- lib/hashid/rails/configuration.rb
|
165
|
+
- lib/hashid/rails/version.rb
|
166
|
+
homepage: https://github.com/jcypret/hashid-rails
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
metadata: {}
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubygems_version: 3.0.3
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: Fork of Justin Cypret work. Use Hashids in your Rails app models.
|
189
|
+
test_files: []
|