cool_id 0.1.3 → 0.1.5

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: 6a61d8d06c3414dcec52ecd9a3f28696d0fafb683b2b206e025c33a1deb03de9
4
- data.tar.gz: bc13a903ed6d5239eae6ee216f5da45699a525d755e914f9f9c2b81b6e33d26c
3
+ metadata.gz: 5284982eed15e82c55f1e8a1c853c83feb97d76d7e04fb247b74e6edcbfdcf0e
4
+ data.tar.gz: 21f44586d37a4a27474dcc984179cc6b8e927ae43da371e5d80874c82a314d43
5
5
  SHA512:
6
- metadata.gz: 77aac33c12253962d7beabffc5d7101379f40d991b580c67ea0a306cd82f8d2befda21b23fbd3c3735a516863ef4c3565b18084f67ed001172b7a395527f3f4a
7
- data.tar.gz: d3c269343bd10098fdd902424c10c22e676a0cea67d5dff404628863ea4d085319017b31d6c602364da024d36c30a470e1bd3ade7b687379bbbd3f011a72191c
6
+ metadata.gz: 9f8a012d217a3435e87f9df231fce5e850e92cc7ded853fff79cca4f721058f0175912aab2a25c8671defb208453e137884c59a9ec8041a8d1d2aad2f032cb74
7
+ data.tar.gz: ad7af3d92a6f4b5a067fe535020d9cdd44d53ddd6584ba89555a74bcc83a4cc6a959f7e4cfb857c1961d85dbfe55bd5d0395afe1aed65680d2ae3dab34ab5688
data/README.md CHANGED
@@ -1,31 +1,89 @@
1
- # CoolId
1
+ # cool_id
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cool_id`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ```ruby
6
+ class User < ActiveRecord::Base
7
+ include CoolId::Model
8
+ cool_id prefix: "usr"
9
+ end
6
10
 
7
- ## Installation
11
+ User.create!(name: "...").id
12
+ # => "usr_vktd1b5v84lr"
8
13
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+ class Customer < ActiveRecord::Base
15
+ include CoolId::Model
16
+ cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
17
+ end
10
18
 
11
- Install the gem and add to the application's Gemfile by executing:
19
+ Customer.create!(name: "...").id
20
+ # => "cus_UHNYBINU"
21
+ ```
12
22
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ it can also lookup records by ids, similar to global id:
14
24
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
25
+ ```ruby
26
+ user = User.create!(name: "John Doe")
27
+ # => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
16
28
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
29
+ CoolId.locate("usr_vktd1b5v84lr")
30
+ # => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
18
31
 
19
- ## Usage
32
+ # You can also parse the id without fetching the record
33
+ parsed = CoolId.parse("usr_vktd1b5v84lr")
34
+ # => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
20
35
 
21
- TODO: Write usage instructions here
36
+ parsed.model_class
37
+ # => User
38
+ ```
22
39
 
23
- ## Development
40
+ ## installation
24
41
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+ ```bash
43
+ bundle add cool_id
44
+ ```
26
45
 
27
- 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`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+ ```ruby
47
+ gem "cool_id"
48
+ ```
28
49
 
29
- ## Contributing
50
+ ### per-model
30
51
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cool_id.
52
+ use string ids when creating a table
53
+
54
+ ```ruby
55
+ create_table :users, id: :string do |t|
56
+ t.string :name
57
+ end
58
+ ```
59
+
60
+ include the `CoolId::Model` concern in the active record model and set up a prefix
61
+
62
+ ```ruby
63
+ class User < ActiveRecord::Base
64
+ include CoolId::Model
65
+ cool_id prefix: "usr"
66
+ end
67
+ ```
68
+
69
+ ### all models
70
+
71
+ use string ids on all new generated migrations
72
+
73
+ ```ruby
74
+ # config/initializers/generators.rb
75
+ Rails.application.config.generators do |g|
76
+ g.orm :active_record, primary_key_type: :string
77
+ end
78
+ ```
79
+
80
+ setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
81
+
82
+ ```ruby
83
+ # app/models/application_record.rb
84
+ class ApplicationRecord < ActiveRecord::Base
85
+ include CoolId::Model
86
+ primary_abstract_class
87
+ enforce_cool_id_for_descendants
88
+ end
89
+ ```
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/cool_id.rb CHANGED
@@ -106,11 +106,11 @@ module CoolId
106
106
  CoolId.generate_id(@cool_id_config)
107
107
  end
108
108
 
109
- def ensure_cool_id_setup
109
+ def enforce_cool_id_for_descendants
110
110
  @cool_id_setup_required = true
111
111
  end
112
112
 
113
- def skip_cool_id_setup
113
+ def skip_enforce_cool_id_for_descendants
114
114
  @cool_id_setup_required = false
115
115
  end
116
116
 
@@ -134,7 +134,7 @@ module CoolId
134
134
 
135
135
  def ensure_cool_id_configured
136
136
  if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
137
- raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_cool_id_setup' to opt out."
137
+ raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id_for_descendants' to opt out."
138
138
  end
139
139
  end
140
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cool_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Schilling