stealth_dom_id 0.2.0 → 0.3.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -6
- data/lib/stealth_dom_id/core.rb +7 -7
- data/lib/stealth_dom_id/railtie.rb +4 -0
- data/lib/stealth_dom_id/version.rb +1 -1
- data/lib/stealth_dom_id.rb +3 -6
- data/stealth_dom_id.gemspec +2 -3
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf34f14ddf5664d87736392bf155b9e62537479b04f54cc71fa079907ab5a152
|
4
|
+
data.tar.gz: 1b0a148be68c737e327a8c39f5bb611c7ac4cc1785b706750b9234dbcabc8278
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d84dad44771f169545508d05df8d205e36f831ff7eaba8ec18014d41482acbc8980216b0f4ee82cb512d2cef537f883a3230c71536d4b9ec1d19e58f231f318b
|
7
|
+
data.tar.gz: 97d53062743fe65d01ca52b140c56183d00107881c954c8893b0eead75b370394fdd499351438718e7c2b5468aec6a0d56dfbac9fbd64191c3e50f0409e660b5
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# stealth_dom_id
|
2
2
|
|
3
|
-
stealth_dom_id extends Rails' [`dom_id`](https://github.com/rails/rails/blob/main/actionview/lib/action_view/record_identifier.rb) helper to generate DOM IDs using alternative
|
3
|
+
stealth_dom_id extends Rails' [`dom_id`](https://github.com/rails/rails/blob/main/actionview/lib/action_view/record_identifier.rb) helper to generate DOM IDs using an alternative attribute instead of database primary keys. This helps prevent exposing internal database IDs
|
4
4
|
|
5
5
|
|
6
6
|
## Installation
|
@@ -20,21 +20,21 @@ gem install stealth_dom_id
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
This gem extends Rails' `dom_id` helper to use alternative
|
23
|
+
This gem extends Rails' `dom_id` helper to use an alternative attribute instead of exposing database IDs in your HTML elements.
|
24
24
|
|
25
25
|
Instead of:
|
26
26
|
```erb
|
27
27
|
<%= dom_id(@user) %>
|
28
28
|
# => "user_1"
|
29
29
|
|
30
|
-
<%= dom_id(@user,
|
30
|
+
<%= dom_id(@user, attribute: :public_id) %>
|
31
31
|
# => Outputs: "user_a1b2c3"
|
32
32
|
```
|
33
33
|
|
34
|
-
|
34
|
+
`attribute` is optional. `prefix` attribute is, just with `dom_id`, also supported.
|
35
35
|
|
36
36
|
```erb
|
37
|
-
<%= dom_id(@user, :admin,
|
37
|
+
<%= dom_id(@user, :admin, attribute: :public_id) %>
|
38
38
|
# => Outputs: "admin_user_a1b2c3"
|
39
39
|
```
|
40
40
|
|
data/lib/stealth_dom_id/core.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module StealthDomId
|
2
|
-
class
|
2
|
+
class AttributeError < ArgumentError; end
|
3
3
|
|
4
4
|
module Core
|
5
|
-
def dom_id(record_or_class, prefix = nil,
|
5
|
+
def dom_id(record_or_class, prefix = nil, attribute: nil)
|
6
6
|
unless record_or_class.is_a?(Class)
|
7
|
-
record_id = if
|
8
|
-
|
7
|
+
record_id = if attribute
|
8
|
+
record_key_for_dom_id_by_attribute(record_or_class, attribute: attribute)
|
9
9
|
else
|
10
10
|
record_key_for_dom_id(record_or_class)
|
11
11
|
end
|
@@ -23,12 +23,12 @@ module StealthDomId
|
|
23
23
|
JOIN = "_".freeze
|
24
24
|
NEW = "new".freeze
|
25
25
|
|
26
|
-
def
|
27
|
-
key = [convert_to_model(record).send(
|
26
|
+
def record_key_for_dom_id_by_attribute(record, attribute:)
|
27
|
+
key = [convert_to_model(record).send(attribute)]
|
28
28
|
|
29
29
|
key ? key.join(JOIN) : key
|
30
30
|
rescue NoMethodError => e
|
31
|
-
raise
|
31
|
+
raise AttributeError, "[StealthDomId] Attribute '#{attribute}' not found on #{record.class}"
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
data/lib/stealth_dom_id.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
# require "active_support"
|
4
|
+
require "stealth_dom_id/version"
|
4
5
|
require "stealth_dom_id/core"
|
5
|
-
require "
|
6
|
+
require "stealth_dom_id/railtie" if defined?(Rails)
|
6
7
|
|
7
8
|
module StealthDomId
|
8
9
|
class Error < StandardError; end
|
9
10
|
end
|
10
|
-
|
11
|
-
ActiveSupport.on_load(:action_view) do
|
12
|
-
include StealthDomId::Core
|
13
|
-
end
|
data/stealth_dom_id.gemspec
CHANGED
@@ -8,13 +8,12 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Rails Designer Developers"]
|
9
9
|
spec.email = ["devs@railsdesigner.com"]
|
10
10
|
|
11
|
-
spec.summary = "Extends Rails `dom_id` helper to support custom
|
12
|
-
spec.description = "
|
11
|
+
spec.summary = "Extends Rails `dom_id` helper to support custom attribute-based identifiers (example “slug”)"
|
12
|
+
spec.description = "stealth_dom_id extends Rails' `dom_id` helper to generate DOM IDs using an alternative attribute instead of database primary keys. This helps prevent exposing internal database IDs."
|
13
13
|
spec.homepage = "https://github.com/Rails-Designer/stealth_dom_id/"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 3.0.0"
|
16
16
|
|
17
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
18
17
|
spec.metadata["source_code_uri"] = "https://github.com/Rails-Designer/stealth_dom_id/"
|
19
18
|
|
20
19
|
spec.files = Dir["{bin,app,config,db,lib,public}/**/*", "Rakefile", "README.md", "stealth_dom_id.gemspec", "Gemfile", "Gemfile.lock"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stealth_dom_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rails Designer Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionview
|
@@ -50,8 +50,8 @@ dependencies:
|
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '8.1'
|
53
|
-
description:
|
54
|
-
alternative
|
53
|
+
description: stealth_dom_id extends Rails' `dom_id` helper to generate DOM IDs using
|
54
|
+
an alternative attribute instead of database primary keys. This helps prevent exposing
|
55
55
|
internal database IDs.
|
56
56
|
email:
|
57
57
|
- devs@railsdesigner.com
|
@@ -75,7 +75,6 @@ homepage: https://github.com/Rails-Designer/stealth_dom_id/
|
|
75
75
|
licenses:
|
76
76
|
- MIT
|
77
77
|
metadata:
|
78
|
-
homepage_uri: https://github.com/Rails-Designer/stealth_dom_id/
|
79
78
|
source_code_uri: https://github.com/Rails-Designer/stealth_dom_id/
|
80
79
|
post_install_message:
|
81
80
|
rdoc_options: []
|
@@ -95,5 +94,6 @@ requirements: []
|
|
95
94
|
rubygems_version: 3.5.23
|
96
95
|
signing_key:
|
97
96
|
specification_version: 4
|
98
|
-
summary: Extends Rails `dom_id` helper to support custom
|
97
|
+
summary: Extends Rails `dom_id` helper to support custom attribute-based identifiers
|
98
|
+
(example “slug”)
|
99
99
|
test_files: []
|