pretty_id 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2adaf9801acbc841856a03333afaaddf61d1eba3
4
+ data.tar.gz: 478f9e9224476be744f41cf237d8e774ed2af44a
5
+ SHA512:
6
+ metadata.gz: dcf2bbba6f40717818e2eecee56d154557b620313c9666d7643e3ed03e443c81e1dbd369e9d369b898e537fa03e8dc57df9dbe87b14d8cfd7751c19a971926e8
7
+ data.tar.gz: da70deca1e75cb700629ae154d02358ad50185443644b2369ba39a9691bd9e7b1528ff45c73a7fe2fa9f1d88474ccbd330a0879c03b638ced897f4e9dbb872cd
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: Czw8TYLlNKX5ZLpHVZAmZVjxdKSZdroYH
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/config/database.yml
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
8
+ spec/dummy/public/uploads/*
9
+ tmp/*
10
+ coverage/*
11
+ *.gem
12
+ Gemfile.lock
13
+ spec/dummy/db/test.sqlite3
14
+ gemfiles/*.lock
15
+ .ruby-gemset
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ cache: bundler
3
+ script: bundle exec rspec
4
+ notifications:
5
+ email: false
6
+
7
+ rvm:
8
+ - 1.9.3
9
+ - 2.0.0
10
+ - 2.1.1
11
+
12
+ gemfile:
13
+ - gemfiles/rails_3.gemfile
14
+ - gemfiles/rails_4.0.gemfile
15
+ - gemfiles/rails_4.1.gemfile
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise 'rails-3' do
2
+ gem 'rails', '3.2.7'
3
+ end
4
+
5
+ appraise 'rails-4.0' do
6
+ gem 'rails', '4.0.0'
7
+ end
8
+
9
+ appraise 'rails-4.1' do
10
+ gem 'rails', '4.1.0'
11
+ end
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Department of Better Technology
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ pretty_id
2
+ =======
3
+
4
+ [![Travis][0]](https://travis-ci.org/dobtco/pretty_id)
5
+ [![Code Climate][1]](https://codeclimate.com/github/dobtco/pretty_id)
6
+ [![Coveralls][2]](https://coveralls.io/r/dobtco/pretty_id)
7
+ [![RubyGem][3]](http://rubygems.org/gems/pretty_id)
8
+
9
+ Add random, unique "pretty" ids to your ActiveRecord models.
10
+
11
+ ## Usage
12
+
13
+ #### 1. Install the gem
14
+ ```ruby
15
+ gem 'pretty_id'
16
+ ```
17
+
18
+ #### 2. Add a `pretty_id` column (and index!) to your model
19
+ ```ruby
20
+ add_column :books, :pretty_id, :string
21
+ add_index :books, :pretty_id
22
+ ```
23
+
24
+ #### 3. Add `has_pretty_id` to your model
25
+ ```ruby
26
+ class Book < ActiveRecord::Base
27
+ has_pretty_id
28
+ end
29
+ ```
30
+
31
+ ## Options
32
+ ```ruby
33
+ class Book < ActiveRecord::Base
34
+ has_pretty_id column: :another_string_column, # default: :pretty_id
35
+ length: 12 # default: 8
36
+ end
37
+ ```
38
+
39
+ ## Warnings
40
+
41
+ Pretty IDs are generated using the [Bignum#to_s](http://www.ruby-doc.org/core-2.1.1/Bignum.html#method-i-to_s) method, and since we're seeding with a random value, there's no guarantee that they'll always be 8 characters long. However, the IDs are always checked against existing values in the database to ensure uniqueness.
42
+
43
+ [0]: http://img.shields.io/travis/dobtco/pretty_id.svg
44
+ [1]: http://img.shields.io/codeclimate/github/dobtco/pretty_id.svg
45
+ [2]: http://img.shields.io/coveralls/dobtco/pretty_id.svg
46
+ [3]: http://img.shields.io/gem/v/pretty_id.svg
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "3.2.7"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,31 @@
1
+ module PrettyId
2
+ module Core
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def has_pretty_id(options = {})
9
+ default_options = {
10
+ column: :pretty_id,
11
+ length: 8
12
+ }
13
+
14
+ options = default_options.merge(options)
15
+
16
+ define_method(:"generate_#{options[:column]}") do
17
+ new_pretty_id = loop do
18
+ random_pretty_id = rand(36**options[:length]).to_s(36)
19
+ exists_hash = {}
20
+ exists_hash[options[:column]] = random_pretty_id
21
+ break random_pretty_id unless self.class.exists?(exists_hash)
22
+ end
23
+
24
+ self.send "#{options[:column]}=", new_pretty_id
25
+ end
26
+
27
+ before_create :"generate_#{options[:column]}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module PrettyId
2
+ VERSION = '0.0.1'
3
+ end
data/lib/pretty_id.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'pretty_id/core'
2
+ ActiveRecord::Base.send :include, PrettyId::Core
data/pretty_id.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "pretty_id/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "pretty_id"
9
+ s.version = PrettyId::VERSION
10
+
11
+ s.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
12
+ s.authors = ['Adam Becker']
13
+ s.summary = 'Add random, unique "pretty" ids to your ActiveRecord models.'
14
+ s.email = 'adam@dobt.co'
15
+ s.license = 'MIT'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {features,spec}/*`.split("\n")
19
+
20
+ s.homepage = 'http://github.com/dobtco/pretty_id'
21
+
22
+ s.add_dependency 'rails'
23
+
24
+ s.add_development_dependency 'appraisal', '1.0.0'
25
+ s.add_development_dependency 'coveralls'
26
+ s.add_development_dependency 'rspec-rails'
27
+ s.add_development_dependency 'sqlite3'
28
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ class UserWithPrettyId < ActiveRecord::Base
4
+ self.table_name = 'users'
5
+ has_pretty_id
6
+ end
7
+
8
+ class UserWithPrettyIdAlt < ActiveRecord::Base
9
+ self.table_name = 'users'
10
+ has_pretty_id column: :pretty_id_alt
11
+ end
12
+
13
+ describe PrettyId do
14
+ it 'assigns before creation' do
15
+ u = UserWithPrettyId.new
16
+ expect(u.pretty_id).to be_nil
17
+ u.save
18
+ expect(u.pretty_id).to_not be_nil
19
+ end
20
+
21
+ it 'does not change' do
22
+ u = UserWithPrettyId.create
23
+ original_pretty_id = u.pretty_id
24
+ u.update_attributes(pretty_id_alt: 'foo')
25
+ expect(u.reload.pretty_id).to eq original_pretty_id
26
+ end
27
+
28
+ it 'can assign to other columns' do
29
+ u = UserWithPrettyIdAlt.create
30
+ expect(u.pretty_id).to be_blank
31
+ expect(u.pretty_id_alt).to be_present
32
+ end
33
+
34
+ describe 'preventing duplicates' do
35
+ before do
36
+ UserWithPrettyId.stub(:exists?).and_return(true, false)
37
+ UserWithPrettyId.any_instance.stub(:rand).and_return(1, 2)
38
+ end
39
+
40
+ it 'prevents duplicates' do
41
+ u = UserWithPrettyId.create
42
+ expect(u.pretty_id).to eq '2'
43
+ end
44
+ end
45
+ end
data/spec/setup.rb ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
2
+
3
+ ActiveRecord::Schema.define(:version => 1) do
4
+ create_table :users do |t|
5
+ t.string :pretty_id
6
+ t.string :pretty_id_alt
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ Coveralls::SimpleCov::Formatter,
6
+ SimpleCov::Formatter::HTMLFormatter
7
+ ]
8
+
9
+ require 'rails/all'
10
+ require 'pretty_id'
11
+ require 'rspec/rails'
12
+ require_relative 'setup'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Becker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
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-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
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
+ description:
84
+ email: adam@dobt.co
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".coveralls.yml"
90
+ - ".gitignore"
91
+ - ".ruby-version"
92
+ - ".travis.yml"
93
+ - Appraisals
94
+ - Gemfile
95
+ - LICENSE.md
96
+ - README.md
97
+ - gemfiles/rails_3.gemfile
98
+ - gemfiles/rails_4.0.gemfile
99
+ - gemfiles/rails_4.1.gemfile
100
+ - lib/pretty_id.rb
101
+ - lib/pretty_id/core.rb
102
+ - lib/pretty_id/version.rb
103
+ - pretty_id.gemspec
104
+ - spec/pretty_id_spec.rb
105
+ - spec/setup.rb
106
+ - spec/spec_helper.rb
107
+ homepage: http://github.com/dobtco/pretty_id
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.0.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Add random, unique "pretty" ids to your ActiveRecord models.
131
+ test_files:
132
+ - spec/pretty_id_spec.rb
133
+ - spec/setup.rb
134
+ - spec/spec_helper.rb