-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "cool_id/version" | ||
require "nanoid" | ||
require "active_support/concern" | ||
require "active_record" | ||
|
||
module CoolId | ||
class Error < StandardError; end | ||
# Your code goes here... | ||
|
||
# defaults copped from | ||
# https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api | ||
DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz" | ||
DEFAULT_SEPARATOR = "_" | ||
DEFAULT_LENGTH = 12 | ||
|
||
def self.generate_id(prefix: "", separator: DEFAULT_SEPARATOR, length: DEFAULT_LENGTH, alphabet: DEFAULT_ALPHABET) | ||
id = Nanoid.generate(size: length, alphabet: alphabet) | ||
[prefix, id].reject(&:empty?).join(separator) | ||
end | ||
|
||
module Model | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
attr_accessor :cool_id_prefix, :cool_id_separator, :cool_id_alphabet, :cool_id_length | ||
|
||
def generate_cool_id | ||
CoolId.generate_id( | ||
prefix: cool_id_prefix, | ||
separator: cool_id_separator || DEFAULT_SEPARATOR, | ||
length: cool_id_length || DEFAULT_LENGTH, | ||
alphabet: cool_id_alphabet || DEFAULT_ALPHABET | ||
) | ||
end | ||
end | ||
|
||
included do | ||
before_create :set_cool_id | ||
|
||
private | ||
|
||
def set_cool_id | ||
self.id = self.class.generate_cool_id if id.blank? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_record" | ||
|
||
# frozen_string_literal: true | ||
|
||
class User < ActiveRecord::Base | ||
include CoolId::Model | ||
self.cool_id_prefix = "usr" | ||
end | ||
|
||
class CustomUser < ActiveRecord::Base | ||
include CoolId::Model | ||
self.cool_id_prefix = "cus" | ||
self.cool_id_separator = "-" | ||
self.cool_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
self.cool_id_length = 8 | ||
end | ||
|
||
RSpec.describe CoolId do | ||
it "has a version number" do | ||
expect(CoolId::VERSION).not_to be nil | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
describe ".generate_id" do | ||
it "generates an ID with default parameters" do | ||
id = CoolId.generate_id | ||
expect(id).to match(/^[0-9a-z]{12}$/) | ||
end | ||
|
||
it "generates an ID with custom prefix, separator, and length" do | ||
id = CoolId.generate_id(prefix: "test", separator: "-", length: 10) | ||
expect(id).to match(/^test-[0-9a-z]{10}$/) | ||
end | ||
|
||
it "generates an ID without prefix when prefix is empty" do | ||
id = CoolId.generate_id(prefix: "", length: 15) | ||
expect(id).to match(/^[0-9a-z]{15}$/) | ||
end | ||
|
||
it "generates an ID with custom alphabet" do | ||
id = CoolId.generate_id(alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 10) | ||
expect(id).to match(/^[A-Z]{10}$/) | ||
end | ||
end | ||
|
||
describe CoolId::Model do | ||
before(:all) do | ||
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | ||
end | ||
|
||
before(:each) do | ||
ActiveRecord::Schema.define do | ||
create_table :users, id: false do |t| | ||
t.string :id, primary_key: true | ||
t.string :name | ||
end | ||
|
||
create_table :custom_users, id: false do |t| | ||
t.string :id, primary_key: true | ||
t.string :name | ||
end | ||
end | ||
end | ||
|
||
after(:each) do | ||
ActiveRecord::Base.connection.drop_table :users | ||
ActiveRecord::Base.connection.drop_table :custom_users | ||
end | ||
|
||
after(:all) do | ||
ActiveRecord::Base.connection.close | ||
end | ||
|
||
it "generates a cool_id for a new record" do | ||
user = User.create(name: "John Doe") | ||
expect(user.id).to match(/^usr_[0-9a-z]{12}$/) | ||
end | ||
|
||
it "does not overwrite an existing id" do | ||
user = User.create(id: "custom-id", name: "Jane Doe") | ||
expect(user.id).to eq("custom-id") | ||
end | ||
|
||
it "generates a cool_id with custom settings" do | ||
custom_user = CustomUser.create(name: "Alice") | ||
expect(custom_user.id).to match(/^cus-[A-Z]{8}$/) | ||
end | ||
end | ||
end |