acts_as_hashids 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30d8efbd94952a6f6812f5040e72e7fb86c8ed99
4
+ data.tar.gz: 2ce7556d45dd3900403240456388e16e9ff9f9f9
5
+ SHA512:
6
+ metadata.gz: a1bcf96eb0ec873e1832bf1c93faf8147eb5570d066b77b57dbe3b4112e7e5378be952c4d7ae30f22ccba5f6bbf557da2468a629ebb48ae02e42fe1c2dde9047
7
+ data.tar.gz: fc9ee0ce4799714295d117a237fc6a0fad52211708255f4093cf1e09c8a2198200ee4dcef098b549f9497f86957b1c3e0d5c11c9cf5da00de4b7477dea3a7b0b
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.db
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ require: rubocop-rspec
2
+ AllCops:
3
+ TargetRubyVersion: 2.3
4
+ Exclude:
5
+ - 'bin/**/*'
6
+ - 'tmp/**/*'
7
+ - "vendor/**/*"
8
+ Metrics/AbcSize:
9
+ Max: 40
10
+ Metrics/LineLength:
11
+ Max: 120
12
+ AllowURI: true
13
+ URISchemes:
14
+ - http
15
+ - https
16
+ Metrics/MethodLength:
17
+ Max: 20
18
+ Metrics/CyclomaticComplexity:
19
+ Max: 10
20
+ Metrics/PerceivedComplexity:
21
+ Max: 10
22
+ Documentation:
23
+ Enabled: false
24
+ Style/SignalException:
25
+ EnforcedStyle: only_raise
26
+ Style/IndentHash:
27
+ EnforcedStyle: consistent
28
+ Style/IndentArray:
29
+ EnforcedStyle: consistent
30
+ Lint/EndAlignment:
31
+ AlignWith: variable
32
+ Style/FrozenStringLiteralComment:
33
+ Enabled: false
34
+ RSpec/AnyInstance:
35
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ sudo: required
3
+ rvm:
4
+ - 2.1.0
5
+ - 2.2.0
6
+ - 2.3.0
7
+ cache: bundler
8
+ script:
9
+ - bundle exec rake
10
+ services:
11
+ - docker
12
+ notifications:
13
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_hashids.gemspec
4
+ gemspec
5
+
6
+ gem 'simplecov', '~> 0.11.2', require: false
7
+ gem 'coveralls', '~> 0.8.13', require: false
8
+ gem 'codeclimate-test-reporter', '~> 0.5.0', require: false
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Daisuke Taniwaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # acts_as_hashids
2
+
3
+ [![Gem Version][gem-image]][gem-link]
4
+ [![Dependency Status][deps-image]][deps-link]
5
+ [![Build Status][build-image]][build-link]
6
+ [![Coverage Status][cov-image]][cov-link]
7
+ [![Code Climate][gpa-image]][gpa-link]
8
+
9
+ Use [Hashids](https://github.com/peterhellberg/hashids.rb) in ActiveRecord effectively.
10
+
11
+ ## Installation
12
+
13
+ Add the acts_as_hashids gem to your Gemfile.
14
+
15
+ ```ruby
16
+ gem "acts_as_hashids"
17
+ ```
18
+
19
+ And run `bundle install`.
20
+
21
+ ## Usage
22
+
23
+ Activate the function in any model of `ActiveRecord::Base`.
24
+
25
+ ```ruby
26
+ class Foo < ActiveRecord::Base
27
+ acts_as_hashids
28
+ end
29
+
30
+ foo = Foo.create
31
+ # => #<Foo:0x007feb5978a7c0 id: 3>
32
+
33
+ foo.to_param
34
+ # => "ePQgabdg"
35
+
36
+ Foo.find(3)
37
+ # => #<Foo:0x007feb5978a7c0 id: 3>
38
+
39
+ Foo.find("ePQgabdg")
40
+ # => #<Foo:0x007feb5978a7c0 id: 3>
41
+
42
+ Foo.find_by_hashids("ePQgabdg")
43
+ # => #<Foo:0x007feb5978a7c0 id: 3>
44
+
45
+ Foo.with_hashids("ePQgabdg").first
46
+ # => #<Foo:0x007feb5978a7c0 id: 3>
47
+ ```
48
+
49
+ ### Use in Rails
50
+
51
+ Only one thing you need to hash ids is put `acts_as_hashids` in `ApplicationRecord`, then you will see hash ids in routes URL and they are handled correctly as long as you use `find` to find records.
52
+
53
+ ## Options
54
+
55
+ ### length
56
+
57
+ You can customize the length of hash ids per model. The default length is 8.
58
+
59
+ ```ruby
60
+ class Foo < ActiveRecord::Base
61
+ acts_as_hashids length: 2
62
+ end
63
+
64
+ Foo.create.to_param
65
+ # => "Rx"
66
+ ```
67
+
68
+ ### secret
69
+
70
+ You can customize the secret of hash ids per model. The default secret is the class name. The name of base class is used for STI.
71
+
72
+ ```ruby
73
+ class Foo1 < ActiveRecord::Base
74
+ acts_as_hashids secret: 'my secret'
75
+ end
76
+
77
+ class Foo2 < ActiveRecord::Base
78
+ acts_as_hashids secret: 'my secret'
79
+ end
80
+
81
+ Foo1.create.to_param
82
+ # => "RxQce3a2"
83
+
84
+ Foo2.create.to_param
85
+ # => "RxQce3a2"
86
+ ```
87
+
88
+ ## Test
89
+
90
+ Execute the command below to run rspec and rubocop.
91
+
92
+ ```
93
+ bundle exec rake
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it
99
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
100
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
101
+ 4. Push to the branch (`git push origin my-new-feature`)
102
+ 5. Create new [Pull Request](../../pull/new/master)
103
+
104
+ ## Copyright
105
+
106
+ Copyright (c) 2014 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
107
+
108
+
109
+
110
+
111
+ [gem-image]: https://badge.fury.io/rb/acts_as_hashids.svg
112
+ [gem-link]: http://badge.fury.io/rb/acts_as_hashids
113
+ [build-image]: https://secure.travis-ci.org/dtaniwaki/acts_as_hashids.png
114
+ [build-link]: http://travis-ci.org/dtaniwaki/acts_as_hashids
115
+ [deps-image]: https://gemnasium.com/dtaniwaki/acts_as_hashids.svg
116
+ [deps-link]: https://gemnasium.com/dtaniwaki/acts_as_hashids
117
+ [cov-image]: https://coveralls.io/repos/dtaniwaki/acts_as_hashids/badge.png
118
+ [cov-link]: https://coveralls.io/r/dtaniwaki/acts_as_hashids
119
+ [gpa-image]: https://codeclimate.com/github/dtaniwaki/acts_as_hashids.png
120
+ [gpa-link]: https://codeclimate.com/github/dtaniwaki/acts_as_hashids
121
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+ RuboCop::RakeTask.new
8
+
9
+ task(:default).clear
10
+ task default: [:spec, :rubocop]
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_hashids/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'acts_as_hashids'
8
+ spec.version = ActsAsHashids::VERSION
9
+ spec.authors = ['dtaniwaki']
10
+ spec.email = ['daisuketaniwaki@gmail.com']
11
+
12
+ spec.summary = 'Hashids in ActiveRecord effectively.'
13
+ spec.description = 'Use Hashids in ActiveRecord effectively.'
14
+ spec.homepage = 'https://github.com/dtaniwaki/acts_as_hashids'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'hashids', '~> 1.0'
23
+ spec.add_dependency 'activerecord', '~> 4.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.11'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
29
+ spec.add_development_dependency 'rubocop', '= 0.37.2'
30
+ spec.add_development_dependency 'rubocop-rspec', '= 1.4.0'
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_hashids"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ module ActsAsHashids
2
+ class Exception < ::Exception; end
3
+ end
4
+
5
+ require 'acts_as_hashids/version'
6
+
7
+ if defined?(Rails::Railtie)
8
+ require 'acts_as_hashids/railtie'
9
+ elsif defined?(ActiveRecord)
10
+ require 'acts_as_hashids/methods'
11
+ ActiveRecord::Base.send(:include, ActsAsHashids::Methods)
12
+ end
@@ -0,0 +1,64 @@
1
+ require 'hashids'
2
+
3
+ module ActsAsHashids
4
+ module Core
5
+ extend ActiveSupport::Concern
6
+
7
+ def to_param
8
+ id && self.class.hashids.encode(id)
9
+ end
10
+
11
+ module FinderMethods
12
+ def find(ids = nil, &block)
13
+ return detect(&block) if block.present? && respond_to?(:detect)
14
+
15
+ encoded_ids = Array(ids).map { |id| id.is_a?(String) ? id : hashids.encode(id) }.flatten
16
+ res = with_hashids(encoded_ids).all
17
+ if ids.is_a?(Array)
18
+ raise_record_not_found_exception! encoded_ids, res.size, encoded_ids.size if res.size != encoded_ids.size
19
+ else
20
+ raise_record_not_found_exception! encoded_ids[0], res.size, encoded_ids.size if res.empty?
21
+ res = res[0]
22
+ end
23
+ res
24
+ end
25
+
26
+ private
27
+
28
+ def raise_record_not_found_exception!(ids, result_size, expected_size)
29
+ if Array(ids).size == 1
30
+ error = "Couldn't find #{name} with '#{primary_key}'=#{ids.inspect}"
31
+ else
32
+ error = "Couldn't find all #{name.pluralize} with '#{primary_key}': "
33
+ error << "(#{ids.map(&:inspect).join(', ')}) (found #{result_size} results, but was looking for #{expected_size})"
34
+ end
35
+
36
+ raise ActiveRecord::RecordNotFound, error
37
+ end
38
+ end
39
+
40
+ module ClassMethods
41
+ include FinderMethods
42
+
43
+ def with_hashids(*ids)
44
+ ids = ids.flatten
45
+ decoded_ids = ids.map { |id| hashids.decode(id) }.flatten
46
+ raise ActsAsHashids::Exception, "Decode error: #{ids.inspect}" if ids.size != decoded_ids.size
47
+
48
+ where(id: decoded_ids)
49
+ end
50
+
51
+ def has_many(*args, &block) # rubocop:disable Style/PredicateName
52
+ options = args.extract_options!
53
+ options[:extend] = (options[:extend] || []).concat([FinderMethods])
54
+ super(*args, options, &block)
55
+ end
56
+
57
+ def relation
58
+ r = super
59
+ r.extend FinderMethods
60
+ r
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'core'
2
+
3
+ module ActsAsHashids
4
+ module Methods
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def acts_as_hashids(options = {})
9
+ include ActsAsHashids::Core unless ancestors.include?(ActsAsHashids::Core)
10
+
11
+ define_singleton_method :hashids_secret do
12
+ secret = options[:secret]
13
+ (secret.respond_to?(:call) ? secret.call : secret) || base_class.name
14
+ end
15
+
16
+ define_singleton_method :hashids do
17
+ length = options[:length] || 8
18
+ Hashids.new(hashids_secret, length)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'acts_as_hashids/methods'
2
+
3
+ module ActsAsHashids
4
+ class Railtie < Rails::Railtie
5
+ initializer 'acts_as_hashids.include_methods' do
6
+ ActiveSupport.on_load :active_record do
7
+ ActiveRecord::Base.send(:include, ActsAsHashids::Methods)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsHashids
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_hashids
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dtaniwaki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashids
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.37.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.37.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.4.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.4.0
125
+ description: Use Hashids in ActiveRecord effectively.
126
+ email:
127
+ - daisuketaniwaki@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - acts_as_hashids.gemspec
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/acts_as_hashids.rb
144
+ - lib/acts_as_hashids/core.rb
145
+ - lib/acts_as_hashids/methods.rb
146
+ - lib/acts_as_hashids/railtie.rb
147
+ - lib/acts_as_hashids/version.rb
148
+ homepage: https://github.com/dtaniwaki/acts_as_hashids
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.5.1
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Hashids in ActiveRecord effectively.
172
+ test_files: []
173
+ has_rdoc: