refs-rails 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 +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +8 -0
- data/lib/refs/railtie.rb +22 -0
- data/lib/refs/version.rb +3 -0
- data/lib/refs/view_helper.rb +9 -0
- data/lib/refs-rails.rb +3 -0
- data/lib/refs.rb +17 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8561ce93112916b2afc63ffc4bc5b27ee0ff1a3c8dd7d08a4277514622c29318
|
|
4
|
+
data.tar.gz: e21a8bc89a8eca3242110babd0c5adfc24722ead44dcc7af4020e3b2e4ba36fb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4a3efb4ced8683972ac8dc3dd0efe003d0cf686dd52da1c390fd80c4f1f6bdd1bb2baee40da4392e9848c9e6826b243618d10fe479307526787ff4daa7ec7496
|
|
7
|
+
data.tar.gz: 8914ddd01fc6b789ff9023fab2647a8a5bb82c808f95149454d3c9ac01e7135a162c87c351ce755d45254616cc048f1609d9d1e833889aaf9d39f0933674144b
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 James Kerr
|
|
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/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Refs (for Rails)
|
|
2
|
+
|
|
3
|
+
A Rails gem for managing string identifiers in a centralized, type-safe way. Good for form IDs, CSS classes, data attributes, and any other string constants you want to keep organized.
|
|
4
|
+
|
|
5
|
+
## Why Refs?
|
|
6
|
+
|
|
7
|
+
Instead of scattering magic strings throughout your Rails application:
|
|
8
|
+
|
|
9
|
+
```erb
|
|
10
|
+
<!-- Scattered magic strings -->
|
|
11
|
+
<form id="user-settings-form"></form>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```rb
|
|
15
|
+
turbo_stream.replace "user-settings-form"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Define them once and reference them everywhere:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
# config/refs.rb
|
|
22
|
+
Refs.define do
|
|
23
|
+
ref :user_settings_form
|
|
24
|
+
ref :user_settings_panel
|
|
25
|
+
ref :user_settings_email_input
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```erb
|
|
30
|
+
<form id="<%= ref.user_settings_form %>"></form>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```rb
|
|
34
|
+
turbo_stream.replace ref.user_settings_form
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Add this line to your application's Gemfile:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
gem "refs-rails"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### 1. Define Your References
|
|
48
|
+
|
|
49
|
+
Create a file at `config/refs.rb` in your Rails application:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
# config/refs.rb
|
|
53
|
+
Refs.define do
|
|
54
|
+
# Form identifiers
|
|
55
|
+
ref :login_form
|
|
56
|
+
ref :signup_form
|
|
57
|
+
ref :user_settings_form
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2. Use in Views
|
|
62
|
+
|
|
63
|
+
Each ref you define is turned into a method on the `ref` object and automatically available in your views and controllers:
|
|
64
|
+
|
|
65
|
+
```erb
|
|
66
|
+
<form id="<%= ref.login_form %>"></form>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```rb
|
|
70
|
+
render turbo_stream: turbo_stream.replace ref.login_form
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Benefits
|
|
74
|
+
|
|
75
|
+
This library helps centralize all your DOM ids, which are frequently needed in a default Rails app with a hotwire frontend. It will help you find your bugs faster with method errors when the template renders, rather than JavaScript errors when you are click-testing.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
81
|
+
|
|
82
|
+
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).
|
|
83
|
+
|
|
84
|
+
## Contributing
|
|
85
|
+
|
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jameskerr/refs-rails.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/refs/railtie.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "rails/railtie"
|
|
2
|
+
require "refs/view_helper"
|
|
3
|
+
|
|
4
|
+
class Refs
|
|
5
|
+
class Railtie < Rails::Railtie
|
|
6
|
+
config.refs = ActiveSupport::OrderedOptions.new
|
|
7
|
+
config.refs.config_path = "config/refs.rb"
|
|
8
|
+
|
|
9
|
+
initializer "refs-rails.setup" do |app|
|
|
10
|
+
file = Rails.root.join(config.refs.config_path)
|
|
11
|
+
app.config.to_prepare do
|
|
12
|
+
load file
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
initializer "refs-rails.action_controller" do
|
|
17
|
+
ActiveSupport.on_load :action_controller do
|
|
18
|
+
include Refs::ViewHelper
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/refs/version.rb
ADDED
data/lib/refs-rails.rb
ADDED
data/lib/refs.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class Refs
|
|
2
|
+
def self.ref(name)
|
|
3
|
+
define_method(name) { name.to_s }
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def self.define(&block)
|
|
7
|
+
class_eval(&block) if block_given?
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.instance
|
|
11
|
+
@instance ||= new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.reset!
|
|
15
|
+
@instance = nil
|
|
16
|
+
end
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: refs-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- James Kerr
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.0'
|
|
26
|
+
email:
|
|
27
|
+
- kerr@hey.com
|
|
28
|
+
executables: []
|
|
29
|
+
extensions: []
|
|
30
|
+
extra_rdoc_files: []
|
|
31
|
+
files:
|
|
32
|
+
- CHANGELOG.md
|
|
33
|
+
- LICENSE.txt
|
|
34
|
+
- README.md
|
|
35
|
+
- Rakefile
|
|
36
|
+
- lib/refs-rails.rb
|
|
37
|
+
- lib/refs.rb
|
|
38
|
+
- lib/refs/railtie.rb
|
|
39
|
+
- lib/refs/version.rb
|
|
40
|
+
- lib/refs/view_helper.rb
|
|
41
|
+
homepage: https://github.com/jameskerr/refs-rails
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
changelog_uri: https://github.com/jameskerr/refs-rails/blob/main/CHANGELOG.md
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 3.2.0
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 3.7.1
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: A simple way to manage string references in a Rails application.
|
|
63
|
+
test_files: []
|