prefixed_ids 0.1.0 → 1.0.0

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: 11c3803dce3921dff8333129c9cbfa8f34ad50463bda15e3517dd2cf74b17205
4
- data.tar.gz: f2a460deb4832039b85668764420150831a3f7b7c79cda9b233cf51907fa5bfe
3
+ metadata.gz: 59a5bd4a556be1cc707fbfc22826ff52c2c7ace6f69c9328eb4ec9e8552a939d
4
+ data.tar.gz: 13d03f6fa3ef80f5527750456fe16ca5262db9c2278f187d49a46f3cd229b113
5
5
  SHA512:
6
- metadata.gz: ef6760313ffd3a32edf92f4b87f6f9d63b8c9209308be4b0d7d6d54bc6f10b7f78fd31f9efe28e260025f3ef13e1837add383d920051564fbc443803c90400e1
7
- data.tar.gz: '01869ac0b8c994fed97c77df7f92adca5cb93a31a9ab09a1cda897eac8d60d8cf4d3960e556023c962de57981cc9cc2e9b41691335ed1a51f99d2c6f890f945a'
6
+ metadata.gz: 97ed861903460640f0e836602aa17015e391600b900437edac0c321707b95bc47aa1ee4ac86b9a303b3531e6e5fbb13ae7195c13ee19bcfe688b57702a0eb1a8
7
+ data.tar.gz: 7d6712c197f5278fc2841033f5d52e17ade941639f6a84b99e428838ae404eb97ea4699587338abb88aaae7bb0aca2e368fb4b961c154990ee27c69ad0f6e9d9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Chris Oliver
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 CHANGED
@@ -1,28 +1,58 @@
1
- # PrefixedIds
1
+ <p align="center">
2
+ <h1>Prefixed IDs</h1>
3
+ </p>
2
4
 
3
- 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/prefixed_ids`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ### 🆔 Friendly Prefixed IDs for your Ruby on Rails models
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ [![Build Status](https://github.com/excid3/prefixed_ids/workflows/Tests/badge.svg)](https://github.com/excid3/prefixed_ids/actions) [![Gem Version](https://badge.fury.io/rb/prefixed_ids.svg)](https://badge.fury.io/rb/prefixed_ids)
6
8
 
7
- ## Installation
9
+ Generate prefixed IDs for your models with a friendly prefix. For example:
8
10
 
11
+ ```ruby
12
+ user_12345abcd
13
+ acct_23lksjdg3
14
+ ```
15
+
16
+ ## 🚀 Installation
9
17
  Add this line to your application's Gemfile:
10
18
 
11
19
  ```ruby
12
- gem 'prefixed_ids'
20
+ bundle add 'prefixed_ids'
13
21
  ```
14
22
 
15
- And then execute:
23
+ ## 📝 Usage
16
24
 
17
- $ bundle install
25
+ First, you'll need to generate a migration to add the prefix_id column to your model(s).
18
26
 
19
- Or install it yourself as:
27
+ ```ruby
28
+ class AddPrefixIdToUsers< ActiveRecord::Migration
29
+ def change
30
+ add_index :users, :prefix_id, unique: true
31
+ end
32
+ end
33
+ ```
20
34
 
21
- $ gem install prefixed_ids
35
+ It's important the `prefix_id` column is indexed and unique.
22
36
 
23
- ## Usage
37
+ Then you can add `has_prefix_id :my_prefix` to your models to autogenerate prefixed IDs.
24
38
 
25
- TODO: Write usage instructions here
39
+ ```ruby
40
+ class User < ApplicationRecord
41
+ has_prefix_id :user
42
+ end
43
+ ```
44
+
45
+ This will generate a value like `user_1234abcd` in the User's `prefix_id` column.
46
+
47
+ ### Customizing
48
+
49
+ You can customize the prefix, length, and attribute name for PrefixedIds.
50
+
51
+ ```ruby
52
+ class Account < ApplicationRecord
53
+ has_prefix_id :acct, attribute: :my_id, length: 32
54
+ end
55
+ ```
26
56
 
27
57
  ## Development
28
58
 
@@ -30,11 +60,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
30
60
 
31
61
  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).
32
62
 
33
- ## Contributing
63
+ ## 🙏 Contributing
34
64
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/prefixed_ids. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/prefixed_ids/blob/master/CODE_OF_CONDUCT.md).
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/excid3/prefixed_ids. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/prefixed_ids/blob/master/CODE_OF_CONDUCT.md).
36
66
 
37
- ## License
67
+ ## 📝 License
38
68
 
39
69
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
70
 
data/Rakefile CHANGED
@@ -1,12 +1,18 @@
1
- # frozen_string_literal: true
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
2
7
 
3
8
  require "bundler/gem_tasks"
9
+
4
10
  require "rake/testtask"
5
11
 
6
12
  Rake::TestTask.new(:test) do |t|
7
13
  t.libs << "test"
8
- t.libs << "lib"
9
- t.test_files = FileList["test/**/*_test.rb"]
14
+ t.pattern = "test/**/*_test.rb"
15
+ t.verbose = false
10
16
  end
11
17
 
12
18
  task default: :test
data/lib/prefixed_ids.rb CHANGED
@@ -1,8 +1,27 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "prefixed_ids/version"
1
+ require "prefixed_ids/version"
2
+ require "prefixed_ids/engine"
4
3
 
5
4
  module PrefixedIds
6
- class Error < StandardError; end
7
- # Your code goes here...
5
+ MINIMUM_TOKEN_LENGTH = 24
6
+
7
+ module Attribute
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def has_prefix_id(prefix, attribute: :prefix_id, length: MINIMUM_TOKEN_LENGTH)
12
+ if length < MINIMUM_TOKEN_LENGTH
13
+ raise MinimumLengthError, "Token requires a minimum length of #{MINIMUM_TOKEN_LENGTH} characters."
14
+ end
15
+
16
+ # Load securerandom only when has_secure_token is used.
17
+ require "active_support/core_ext/securerandom"
18
+ define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_unique_prefix_id(prefix, length: length) }
19
+ before_create { send("#{attribute}=", self.class.generate_unique_prefix_id(prefix, length: length)) unless send("#{attribute}?") }
20
+ end
21
+
22
+ def generate_unique_prefix_id(prefix, length: MINIMUM_TOKEN_LENGTH)
23
+ prefix.to_s + "_" + SecureRandom.base58(length)
24
+ end
25
+ end
26
+ end
8
27
  end
@@ -0,0 +1,9 @@
1
+ module PrefixedIds
2
+ class Engine < ::Rails::Engine
3
+ initializer "prefixed_ids.model" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include PrefixedIds::Attribute
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module PrefixedIds
4
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
5
3
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :prefixed_ids do
3
+ # # Task goes here
4
+ # end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefixed_ids
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-02-17 00:00:00.000000000 Z
12
- dependencies: []
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: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: standardrb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
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'
13
55
  description: Prefixed IDs generates IDs with friendly prefixes for your models
14
56
  email:
15
57
  - excid3@gmail.com
@@ -17,18 +59,13 @@ executables: []
17
59
  extensions: []
18
60
  extra_rdoc_files: []
19
61
  files:
20
- - ".github/workflows/main.yml"
21
- - ".gitignore"
22
- - CODE_OF_CONDUCT.md
23
- - Gemfile
24
- - LICENSE.txt
62
+ - MIT-LICENSE
25
63
  - README.md
26
64
  - Rakefile
27
- - bin/console
28
- - bin/setup
29
65
  - lib/prefixed_ids.rb
66
+ - lib/prefixed_ids/engine.rb
30
67
  - lib/prefixed_ids/version.rb
31
- - prefixed_ids.gemspec
68
+ - lib/tasks/prefixed_ids_tasks.rake
32
69
  homepage: https://github.com/excid3/prefixed_ids
33
70
  licenses:
34
71
  - MIT
@@ -44,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
81
  requirements:
45
82
  - - ">="
46
83
  - !ruby/object:Gem::Version
47
- version: 2.3.0
84
+ version: '0'
48
85
  required_rubygems_version: !ruby/object:Gem::Requirement
49
86
  requirements:
50
87
  - - ">="
@@ -1,18 +0,0 @@
1
- name: Ruby
2
-
3
- on: [push,pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
- - name: Set up Ruby
11
- uses: ruby/setup-ruby@v1
12
- with:
13
- ruby-version: 3.0.0
14
- - name: Run the default task
15
- run: |
16
- gem install bundler -v 2.2.3
17
- bundle install
18
- bundle exec rake
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at excid3@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in prefixed_ids.gemspec
6
- gemspec
7
-
8
- gem "rake", "~> 13.0"
9
-
10
- gem "minitest", "~> 5.0"
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Chris Oliver
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "bundler/setup"
5
- require "prefixed_ids"
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require "irb"
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/prefixed_ids.gemspec DELETED
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/prefixed_ids/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "prefixed_ids"
7
- spec.version = PrefixedIds::VERSION
8
- spec.authors = ["Chris Oliver"]
9
- spec.email = ["excid3@gmail.com"]
10
-
11
- spec.summary = "Prefixed IDs generates IDs with friendly prefixes for your models"
12
- spec.description = "Prefixed IDs generates IDs with friendly prefixes for your models"
13
- spec.homepage = "https://github.com/excid3/prefixed_ids"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/excid3/prefixed_ids"
19
- spec.metadata["changelog_uri"] = "https://github.com/excid3/prefixed_ids"
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
- end
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
29
-
30
- # Uncomment to register a new dependency of your gem
31
- # spec.add_dependency "example-gem", "~> 1.0"
32
-
33
- # For more information and examples about making a new gem, checkout our
34
- # guide at: https://bundler.io/guides/creating_gem.html
35
- end