Skip to content

Commit

Permalink
feat: Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet committed Aug 18, 2024
1 parent de865bc commit 6b63379
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 37 deletions.
90 changes: 74 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,89 @@
# CoolId
# cool_id

TODO: Delete this and the text below, and describe your gem
a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid.

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.
```ruby
class User < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "usr"
end

## Installation
User.create!(name: "...").id
# => "usr_vktd1b5v84lr"

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.
class Customer < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
end

Install the gem and add to the application's Gemfile by executing:
Customer.create!(name: "...").id
# => "cus-UHNYBINU"
```

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
it can also lookup records by ids, similar to global id:

If bundler is not being used to manage dependencies, install the gem by executing:
```ruby
user = User.create!(name: "John Doe")
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">

$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
CoolId.locate("usr_vktd1b5v84lr")
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">

## Usage
# You can also parse the id without fetching the record
parsed = CoolId.parse("usr_vktd1b5v84lr")
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>

TODO: Write usage instructions here
parsed.model_class
# => User
```

## Development
## installation

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.
```bash
bundle add cool_id
```

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).
```ruby
gem "cool_id"
```

## Contributing
### per-model

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cool_id.
use string ids when creating a table

```ruby
create_table :users, id: :string do |t|
t.string :name
end
```

include the `CoolId::Model` concern in the active record model and set up a prefix

```ruby
class User < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "usr"
end
```

### all models

use string ids on all new generated migrations

```ruby
# config/initializers/generators.rb
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :string
end
```

setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it

```ruby
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include CoolId::Model
primary_abstract_class
enforce_cool_id_for_descendants
end
```
6 changes: 3 additions & 3 deletions lib/cool_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def generate_cool_id
CoolId.generate_id(@cool_id_config)
end

def ensure_cool_id_setup
def enforce_cool_id_for_descendants
@cool_id_setup_required = true
end

def skip_cool_id_setup
def skip_enforce_cool_id_for_descendants
@cool_id_setup_required = false
end

Expand All @@ -134,7 +134,7 @@ def set_cool_id

def ensure_cool_id_configured
if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_cool_id_setup' to opt out."
raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id_for_descendants' to opt out."
end
end
end
Expand Down
34 changes: 16 additions & 18 deletions spec/cool_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class User < ActiveRecord::Base
cool_id prefix: "usr"
end

class CustomUser < ActiveRecord::Base
class Customer < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
end
Expand Down Expand Up @@ -97,21 +97,19 @@ class CustomUser < ActiveRecord::Base

before(:each) do
ActiveRecord::Schema.define do
create_table :users, id: false do |t|
t.string :id, primary_key: true
create_table :users, id: :string do |t|
t.string :name
end

create_table :custom_users, id: false do |t|
t.string :id, primary_key: true
create_table :customers, id: :string do |t|
t.string :name
end
end
end

after(:each) do
ActiveRecord::Base.connection.drop_table :users
ActiveRecord::Base.connection.drop_table :custom_users
ActiveRecord::Base.connection.drop_table :customers
end

after(:all) do
Expand All @@ -130,8 +128,8 @@ class CustomUser < ActiveRecord::Base

it "generates a cool_id with custom settings" do
CoolId.separator = "-"
custom_user = CustomUser.create(name: "Alice")
expect(custom_user.id).to match(/^cus-[A-Z]{8}$/)
customer = Customer.create(name: "Alice")
expect(customer.id).to match(/^cus-[A-Z]{8}$/)
CoolId.reset_configuration
end

Expand Down Expand Up @@ -175,9 +173,9 @@ class CustomUser < ActiveRecord::Base
end

it "can locate a custom record using CoolId.locate" do
custom_user = CustomUser.create(name: "Alice")
located_custom_user = CoolId.locate(custom_user.id)
expect(located_custom_user).to eq(custom_user)
customer = Customer.create(name: "Alice")
located_customer = CoolId.locate(customer.id)
expect(located_customer).to eq(customer)
end

it "returns nil when trying to locate a non-existent record" do
Expand All @@ -190,10 +188,10 @@ class CustomUser < ActiveRecord::Base

it "works with different separators" do
user = User.create(name: "John Doe")
custom_user = CustomUser.create(name: "Jane Doe")
customer = Customer.create(name: "Jane Doe")

expect(CoolId.locate(user.id)).to eq(user)
expect(CoolId.locate(custom_user.id)).to eq(custom_user)
expect(CoolId.locate(customer.id)).to eq(customer)
end
end

Expand Down Expand Up @@ -247,21 +245,21 @@ class CustomUser < ActiveRecord::Base
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
include CoolId::Model
ensure_cool_id_setup
enforce_cool_id_for_descendants
end

expect {
class UnconfiguredModel < BaseRecord
end
UnconfiguredModel.new
}.to raise_error(CoolId::Error, "CoolId not configured for UnconfiguredModel. Use 'cool_id' to configure or 'skip_cool_id_setup' to opt out.")
}.to raise_error(CoolId::Error, "CoolId not configured for UnconfiguredModel. Use 'cool_id' to configure or 'skip_enforce_cool_id_for_descendants' to opt out.")
end

it "does not raise an error when cool_id is configured in a subclass" do
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
include CoolId::Model
ensure_cool_id_setup
enforce_cool_id_for_descendants
end

expect {
Expand All @@ -276,12 +274,12 @@ class ConfiguredModel < BaseRecord
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
include CoolId::Model
ensure_cool_id_setup
enforce_cool_id_for_descendants
end

expect {
class SkippedModel < BaseRecord
skip_cool_id_setup
skip_enforce_cool_id_for_descendants
end
SkippedModel.new
}.not_to raise_error
Expand Down

0 comments on commit 6b63379

Please sign in to comment.