hashid-rails 0.1.2 → 0.2.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
  SHA1:
3
- metadata.gz: 7b2ea9d51584971cbf828a309a551da690745fbd
4
- data.tar.gz: 04f8a2f59a6fa30308b3103b7698153795b6b46c
3
+ metadata.gz: 71c60b5b414a413c97192b5c81cc6c0495a71a06
4
+ data.tar.gz: d4e88b4c3ee0ff8a1412eb19cf7bbae4ee29ed37
5
5
  SHA512:
6
- metadata.gz: ef4dfa2c27773687fa4bf0b27e65bc415201fe1f022fdd31631c96ab2d4cef9b8c3b16a5209f5c12d9953bc1a3cb75906083d6b0c32aa83b8a440758a0f72752
7
- data.tar.gz: 1c0d95fee24c8ce7d1d67df9ae4bb0740e57101d0e402e3398bd4ca2aa7f120b745bba65b7d53f353c40505110bc1a3d916a79778027fd9b693295fd2e818603
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
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in hashid-rails.gemspec
4
4
  gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Hashid Rails
2
+ [ ![Codeship Status for jcypret/hashid-rails](https://codeship.com/projects/79614b80-93bc-0133-e91e-767635853833/status?branch=master)](https://codeship.com/projects/124848)
3
+ [![Code Climate](https://codeclimate.com/github/jcypret/hashid-rails/badges/gpa.svg)](https://codeclimate.com/github/jcypret/hashid-rails)
4
+ [![Test Coverage](https://codeclimate.com/github/jcypret/hashid-rails/badges/coverage.svg)](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 Rails app. Instead of your models using sequential numbers like 1, 2, 3, they will instead have unique short hashes like "yLA6m0oM", "5bAyD0LO", and "wz3MZ49l". The database will still use integers under the hood, so this gem can be added or removed at any time.
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 = People.find(params[:hashid])
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 `bin/console` for an interactive prompt that will allow you to experiment.
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 release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- Hashids.new(table_name, 6)
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
 
@@ -1,5 +1,5 @@
1
1
  module Hashid
2
2
  module Rails
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  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: 0.1.2
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: 2015-04-13 00:00:00.000000000 Z
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
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.1