public_code 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 391563ed134d7ff113b44b5608ddf2068dd18ca6048190aa1913e03a7313adc8
4
+ data.tar.gz: 7b1e6be8d4b8bb9abdac10b8aaabf5a269c37b2fe119437f1387562985537538
5
+ SHA512:
6
+ metadata.gz: d84156a63e7644dd3df2f9beb2c33b20cc12c2936db07c0f2d30770aad3cfaa2572439e91a0fabb33691fdcbbbaf333b1b09c577ebcf3e0aae7221db28b4a736
7
+ data.tar.gz: ea22466225e19e92fe4fbe4d737153bd0c03c0d8ffa26aaac497d2fb38548066157e3aba9442e3a8b66712b4233f5ae966d40b7e04a7ca545de6cf8149ddd800
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ### Added
6
+
7
+ - A model that calls `has_public_code` gives each new record a random 24 character code, and every address Rails builds for the record carries that code in place of its id.
8
+ - `find_by_public_code!` finds a record by its code among the records searched, whatever the code column is named.
9
+ - `PublicCode.generate` makes a code for a record that already exists, such as in a migration.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DYB
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # public_code
2
+
3
+ Gives an Active Record model a random code that its addresses carry and that
4
+ finds it, so no address shows a database id. The database id stays the
5
+ internal key that records point at each other with.
6
+
7
+ ## Install
8
+
9
+ ```ruby
10
+ gem "public_code"
11
+ ```
12
+
13
+ ## Use
14
+
15
+ Add a code column with a unique index, and fill it for records that already exist:
16
+
17
+ ```ruby
18
+ class AddPublicCodeToProducts < ActiveRecord::Migration[8.1]
19
+ class Product < ActiveRecord::Base
20
+ self.table_name = "products"
21
+ end
22
+
23
+ def up
24
+ add_column :products, :public_code, :string
25
+ Product.find_each { |product| product.update_columns(public_code: PublicCode.generate) }
26
+ change_column_null :products, :public_code, false
27
+ add_index :products, :public_code, unique: true
28
+ end
29
+
30
+ def down
31
+ remove_column :products, :public_code
32
+ end
33
+ end
34
+ ```
35
+
36
+ Name the code on the model:
37
+
38
+ ```ruby
39
+ class Product < ApplicationRecord
40
+ has_public_code
41
+ end
42
+ ```
43
+
44
+ A new product gets a random 24 character code of letters and digits. Every
45
+ address Rails builds for it carries the code in place of its id, so
46
+ `product_path(product)` is `/products/k3f9x2…`.
47
+
48
+ Find a record from the code in an address:
49
+
50
+ ```ruby
51
+ Product.find_by_public_code!(params[:id])
52
+ current_client.products.find_by_public_code!(params[:id])
53
+ ```
54
+
55
+ It raises `ActiveRecord::RecordNotFound` when no record among those searched
56
+ has that code, so a search limited to one client's records never finds
57
+ another client's.
58
+
59
+ A column with its own name works the same way:
60
+
61
+ ```ruby
62
+ has_public_code :code
63
+ ```
@@ -0,0 +1,13 @@
1
+ module PublicCode
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def has_public_code(column = :public_code)
7
+ has_secure_token column, length: PublicCode::LENGTH
8
+ define_method(:to_param) { public_send(column) }
9
+ define_singleton_method(:find_by_public_code!) { |code| find_by!(column => code) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module PublicCode
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require "active_record"
2
+ require "active_support"
3
+ require "public_code/version"
4
+ require "public_code/model"
5
+
6
+ module PublicCode
7
+ LENGTH = 24
8
+
9
+ def self.generate = ActiveRecord::Base.generate_unique_secure_token(length: LENGTH)
10
+ end
11
+
12
+ ActiveSupport.on_load(:active_record) { include PublicCode::Model }
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: public_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Schneider
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.1'
40
+ description: Gives an Active Record model a random code that its addresses carry and
41
+ that finds it, so no address shows a database id.
42
+ email:
43
+ - tylercschneider@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - LICENSE
50
+ - README.md
51
+ - lib/public_code.rb
52
+ - lib/public_code/model.rb
53
+ - lib/public_code/version.rb
54
+ homepage: https://github.com/DYB-Development/public_code
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/DYB-Development/public_code
59
+ source_code_uri: https://github.com/DYB-Development/public_code
60
+ changelog_uri: https://github.com/DYB-Development/public_code/blob/main/CHANGELOG.md
61
+ bug_tracker_uri: https://github.com/DYB-Development/public_code/issues
62
+ documentation_uri: https://github.com/DYB-Development/public_code#readme
63
+ rubygems_mfa_required: 'true'
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.3.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 4.0.20
79
+ specification_version: 4
80
+ summary: A random code for a record's addresses in place of its database id.
81
+ test_files: []