hashid-rails 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4ea795a68ca182b4cf66429705dc4281272502ef4e9922551082ddb1dfad022
4
- data.tar.gz: 38f04be13d9159eadd37075091f90ddb57bef4eaac679718681deae67cfc33d1
3
+ metadata.gz: ff1db9b357cb0791da90ba58f86c3193d8f02fa9585099c18a90edb274bb4dbe
4
+ data.tar.gz: 11b31881df97678fda33e38eb42153b245b9f0672e823f3cae57219586a178b9
5
5
  SHA512:
6
- metadata.gz: 94cecc8c493920fb3aa1e0495e307989d6d1051ca8284580f11be3090f55a3c9821c34f8cb4248511c70537c618db1066f5ebdb3a654a7e1182c700f67e7eb30
7
- data.tar.gz: 2ed1f2740f8f1d67fd2ce1997b6c06d6de4d8925eca00eb13dd9251a75406bac09635429d8a7e8177a4ef3b62af88825330e93fd7530746fcbd30ffeebd221cc
6
+ metadata.gz: 9fedf05af138f2efbeb4e1caedafc0ab844946cba287e085bd4446f49a218f4f44926602b36d26a5d06b2792d49f7b0e37121230c561d6b13ad351fa8e3e56f0
7
+ data.tar.gz: db9d9f9740bca29e8b0bf33ac2d1598a98a34516368a7d503bf10bfe71f8cbc147819934675eb1dda46bfddf32acff25705a0133f4ddc85a2f424e4842e41426
@@ -0,0 +1,35 @@
1
+ name: build
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+
9
+ steps:
10
+ - uses: actions/checkout@v1
11
+
12
+ - name: Install Ruby (2.6)
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+
17
+ - name: Install SQLite
18
+ run: sudo apt install -y libsqlite3-dev
19
+
20
+ - name: Setup Code Climate test-reporter
21
+ run: |
22
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
23
+ chmod +x ./cc-test-reporter
24
+ ./cc-test-reporter before-build
25
+
26
+ - name: Build and test with RSpec
27
+ run: |
28
+ gem install bundler
29
+ bundle install --jobs 4 --retry 3
30
+ bundle exec rspec
31
+
32
+ - name: Publish code coverage
33
+ run: |
34
+ export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
35
+ ./cc-test-reporter after-build -r ${{secrets.CC_TEST_REPORTER_ID}}
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0 (2019-10-29)
4
+ - Add support for model-level config ([#67](https://github.com/jcypret/hashid-rails/pull/67))
5
+
3
6
  ## 1.2.2 (2018-07-29)
4
7
  ### Fixed
5
8
  - Handle exception raised when using a letter-only alphabet and attempting to
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Hashid Rails
2
2
  [![Gem Version](https://badge.fury.io/rb/hashid-rails.svg)](https://badge.fury.io/rb/hashid-rails)
3
- [![Build Status](https://travis-ci.org/jcypret/hashid-rails.svg?branch=master)](https://travis-ci.org/jcypret/hashid-rails)
3
+ ![Build Status](https://github.com/jcypret/hashid-rails/workflows/build/badge.svg?branch=master)
4
4
  [![Code Climate](https://codeclimate.com/github/jcypret/hashid-rails/badges/gpa.svg)](https://codeclimate.com/github/jcypret/hashid-rails)
5
5
  [![Test Coverage](https://codeclimate.com/github/jcypret/hashid-rails/badges/coverage.svg)](https://codeclimate.com/github/jcypret/hashid-rails/coverage)
6
6
 
@@ -54,7 +54,7 @@ model.hashid
54
54
  #=> "yLA6m0oM"
55
55
  ```
56
56
 
57
- Additionally, the `to_param` method is overriden to use hashid instead of id.
57
+ Additionally, the `to_param` method is overridden to use hashid instead of id.
58
58
  This means methods that take advantage of implicit ID will automatically work
59
59
  with hashids.
60
60
 
@@ -94,8 +94,9 @@ default options.
94
94
 
95
95
  ```ruby
96
96
  Hashid::Rails.configure do |config|
97
- # The salt to use for generating hashid. Prepended with table name.
97
+ # The salt to use for generating hashid. Prepended with pepper (table name).
98
98
  config.salt = ""
99
+ config.pepper = table_name
99
100
 
100
101
  # The minimum length of generated hashids
101
102
  config.min_hash_length = 6
@@ -113,6 +114,22 @@ Hashid::Rails.configure do |config|
113
114
  end
114
115
  ```
115
116
 
117
+ ### Model-Level Config
118
+
119
+ You can also customize the hashid configuration at the model level.
120
+ `hashid_config` supports all the same options as the `Hashid::Rails.configure`
121
+ block and allows for each model to have a different config. This can be useful
122
+ for setting a custom salt/pepper. For instance, the pepper defaults to the table
123
+ name, so if you rename the table, you can keep the same hashids by setting the
124
+ pepper to the old table name.
125
+
126
+ ```ruby
127
+ class Model < ActiveRecord::Base
128
+ include Hashid::Rails
129
+ hashid_config pepper: "old_table_name"
130
+ end
131
+ ```
132
+
116
133
  ## Upgrading from Pre-1.0
117
134
 
118
135
  The 1.0 release of this gem introduced hashid signing to prevent
@@ -147,3 +164,6 @@ and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
147
164
  3. Commit your changes (`git commit -am 'Add some feature'`)
148
165
  4. Push to the branch (`git push origin my-new-feature`)
149
166
  5. Create a new Pull Request
167
+
168
+ > NOTE: If it's a significant feature or change, consider creating an Issue for
169
+ > discussion before opening a PR.
@@ -36,6 +36,23 @@ module Hashid
36
36
  alias to_param hashid
37
37
 
38
38
  module ClassMethods
39
+ def hashid_config(options = {})
40
+ config = Hashid::Rails.configuration.dup
41
+ config.pepper = table_name
42
+ options.each do |attr, value|
43
+ config.public_send("#{attr}=", value)
44
+ end
45
+ @hashid_configuration = config
46
+ end
47
+
48
+ def hashid_configuration
49
+ @hashid_configuration || hashid_config
50
+ end
51
+
52
+ def reset_hashid_config
53
+ @hashid_configuration = nil
54
+ end
55
+
39
56
  def relation
40
57
  super.tap { |r| r.extend ClassMethods }
41
58
  end
@@ -71,7 +88,7 @@ module Hashid
71
88
  uniq_ids = ids.flatten.compact.uniq
72
89
  uniq_ids = uniq_ids.first unless expects_array || uniq_ids.size > 1
73
90
 
74
- if Hashid::Rails.configuration.override_find
91
+ if hashid_configuration.override_find
75
92
  super(decode_id(uniq_ids, fallback: true))
76
93
  else
77
94
  super
@@ -89,11 +106,11 @@ module Hashid
89
106
  private
90
107
 
91
108
  def hashids
92
- Hashids.new(*Hashid::Rails.configuration.for_table(table_name))
109
+ Hashids.new(*hashid_configuration.to_args)
93
110
  end
94
111
 
95
112
  def hashid_encode(id)
96
- if Hashid::Rails.configuration.sign_hashids
113
+ if hashid_configuration.sign_hashids
97
114
  hashids.encode(HASHID_TOKEN, id)
98
115
  else
99
116
  hashids.encode(id)
@@ -104,7 +121,7 @@ module Hashid
104
121
  fallback_value = fallback ? id : nil
105
122
  decoded_hashid = hashids.decode(id.to_s)
106
123
 
107
- if Hashid::Rails.configuration.sign_hashids
124
+ if hashid_configuration.sign_hashids
108
125
  valid_hashid?(decoded_hashid) ? decoded_hashid.last : fallback_value
109
126
  else
110
127
  decoded_hashid.first || fallback_value
@@ -4,6 +4,7 @@ module Hashid
4
4
  module Rails
5
5
  class Configuration
6
6
  attr_accessor :salt,
7
+ :pepper,
7
8
  :min_hash_length,
8
9
  :alphabet,
9
10
  :override_find,
@@ -11,6 +12,7 @@ module Hashid
11
12
 
12
13
  def initialize
13
14
  @salt = ""
15
+ @pepper = ""
14
16
  @min_hash_length = 6
15
17
  @alphabet = "abcdefghijklmnopqrstuvwxyz" \
16
18
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
@@ -19,8 +21,8 @@ module Hashid
19
21
  @sign_hashids = true
20
22
  end
21
23
 
22
- def for_table(table_name)
23
- ["#{table_name}#{salt}", min_hash_length, alphabet]
24
+ def to_args
25
+ ["#{pepper}#{salt}", min_hash_length, alphabet]
24
26
  end
25
27
  end
26
28
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hashid
4
4
  module Rails
5
- VERSION = "1.2.2"
5
+ VERSION = "1.3.0"
6
6
  end
7
7
  end
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: 1.2.2
4
+ version: 1.3.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: 2018-07-29 00:00:00.000000000 Z
11
+ date: 2019-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,6 +148,7 @@ executables: []
148
148
  extensions: []
149
149
  extra_rdoc_files: []
150
150
  files:
151
+ - ".github/workflows/ruby.yml"
151
152
  - ".gitignore"
152
153
  - ".rspec"
153
154
  - ".rubocop.yml"
@@ -182,8 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
183
  - !ruby/object:Gem::Version
183
184
  version: '0'
184
185
  requirements: []
185
- rubyforge_project:
186
- rubygems_version: 2.7.6
186
+ rubygems_version: 3.0.3
187
187
  signing_key:
188
188
  specification_version: 4
189
189
  summary: Use Hashids in your Rails app models.