Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Set a license, cleanup readme #2

Merged
merged 10 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cool_id (0.1.4)
cool_id (0.1.5)
activerecord (>= 6.0)
activesupport (>= 6.0)
nanoid (~> 2.0)
Expand Down
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) Peter Schilling

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
79 changes: 61 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cool_id

a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid.
a gem for rails that generates string primary key ids for active record models with a per-model prefix followed by a nanoid. this lets you create stripe style ids in your own rails app, and they'll be the same ids you see in your database.

```ruby
class User < ActiveRecord::Base
Expand All @@ -10,35 +10,60 @@ end

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

class Customer < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
end

Customer.create!(name: "...").id
# => "cus_UHNYBINU"
```

it can also lookup records by ids, similar to global id:
it can also lookup records similar to global id:

```ruby
user = User.create!(name: "John Doe")
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">

CoolId.locate("usr_vktd1b5v84lr")
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
```

# You can also parse the id without fetching the record
and parse ids

```ruby
parsed = CoolId.parse("usr_vktd1b5v84lr")
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>

parsed.model_class
# => User
```

and generate ids without creating a record

```ruby
# generate an id, e.g. for batch inserts or upserts
User.generate_cool_id
# => "usr_vktd1b5v84lr"

```

it takes parameters to change the alphabet or length

```ruby
class Customer < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
end

Customer.create!(name: "...").id
# => "cus_UHNYBINU"
```

and these can be configured globally

```ruby
CoolId.configure do |config|
config.separator = "-"
config.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.length = 8
end
```

## installation

add cool_id to your Gemfile:

```bash
bundle add cool_id
```
Expand All @@ -47,7 +72,7 @@ bundle add cool_id
gem "cool_id"
```

### per-model
### using cool_id in one model

use string ids when creating a table

Expand All @@ -66,9 +91,9 @@ class User < ActiveRecord::Base
end
```

### all models
### using cool_id on all models

use string ids on all new generated migrations
you have drank the coolaid. setup rails to use string ids on all new generated migrations

```ruby
# config/initializers/generators.rb
Expand All @@ -77,7 +102,7 @@ Rails.application.config.generators do |g|
end
```

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

```ruby
# app/models/application_record.rb
Expand All @@ -87,3 +112,21 @@ class ApplicationRecord < ActiveRecord::Base
enforce_cool_id_for_descendants
end
```

### graphql

if you use the graphql ruby node interface, you can implement [object identification](https://graphql-ruby.org/schema/object_identification)


```ruby
# app/graphql/app_schema.rb
class AppSchema < GraphQL::Schema
def self.id_from_object(object, type_definition, query_ctx)
object.id
end

def self.object_from_id(id, query_ctx)
CoolId.locate(id)
end
end
```
1 change: 1 addition & 0 deletions cool_id.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gem::Specification.new do |spec|
spec.summary = "generates cool ids"
spec.description = "generates primary keys using prefixed nanoids for ActiveRecord models"
spec.homepage = "https://github.com/schpet/cool_id"
spec.license = "ISC"
spec.required_ruby_version = ">= 3.0.0"

# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
Expand Down
2 changes: 1 addition & 1 deletion lib/cool_id/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CoolId
VERSION = "0.1.4"
VERSION = "0.1.5"
end