vident 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +29 -0
- data/LICENSE.txt +21 -0
- data/README.md +349 -0
- data/Rakefile +14 -0
- data/examples/ex1.gif +0 -0
- data/lib/tasks/vident.rake +37 -0
- data/lib/vident/attributes/not_typed.rb +78 -0
- data/lib/vident/attributes/typed.rb +180 -0
- data/lib/vident/attributes/typed_niling_struct.rb +27 -0
- data/lib/vident/attributes/types.rb +16 -0
- data/lib/vident/base.rb +286 -0
- data/lib/vident/component.rb +32 -0
- data/lib/vident/railtie.rb +10 -0
- data/lib/vident/root_component/base.rb +239 -0
- data/lib/vident/root_component/using_phlex_html.rb +44 -0
- data/lib/vident/root_component/using_view_component.rb +44 -0
- data/lib/vident/stable_id.rb +24 -0
- data/lib/vident/typed_component.rb +48 -0
- data/lib/vident/version.rb +5 -0
- data/lib/vident.rb +33 -0
- data/sig/vident.rbs +4 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 481f74d4370e94387dfa81c6d6287e8a5a8dd022d6e353d656cfa3807f069b03
|
4
|
+
data.tar.gz: b462c2871a4f48b2193ad91a630a773d37efb581d5efae91699523602d18b68f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d9eea045322f4bad4bcbc03bf4985163674385c08db20aa5aec398edabf56c428aeac532bacdbfd32f3188d3e5a7cc5f6b3bea4c9ba1c51209befd32c5810128
|
7
|
+
data.tar.gz: 06b566851995e5b4873dcca265345900b89cf516a13ae50fe4a6d08c848764e8fd6f7fd0c520d71985347a29befbcae8bd514639c525541bded0617f4188899a
|
data/.standard.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
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 stevegeek@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
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in vident.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "puma"
|
9
|
+
|
10
|
+
gem "rails", ">= 7"
|
11
|
+
# TODO: move examples to another app?
|
12
|
+
gem "sprockets-rails"
|
13
|
+
gem "tailwindcss-rails"
|
14
|
+
gem "turbo-rails"
|
15
|
+
gem "importmap-rails"
|
16
|
+
gem "stimulus-rails"
|
17
|
+
|
18
|
+
gem "dry-struct"
|
19
|
+
|
20
|
+
gem "phlex", "~> 0.5.3", require: "phlex/rails"
|
21
|
+
gem "view_component", "2.74.1"
|
22
|
+
|
23
|
+
gem "sqlite3"
|
24
|
+
|
25
|
+
gem "rake", "~> 13.0"
|
26
|
+
|
27
|
+
gem "minitest", "~> 5.0"
|
28
|
+
|
29
|
+
gem "standard", "~> 1.3"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Stephen Ierodiaconou
|
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,349 @@
|
|
1
|
+
# Vident
|
2
|
+
|
3
|
+
Vident helps you create flexible & maintainable component libraries for your application.
|
4
|
+
|
5
|
+
Vident makes using Stimulus with your [`ViewComponent`](https://viewcomponent.org/) or [`Phlex`](https://phlex.fun) components easier.
|
6
|
+
|
7
|
+
## Things still to do...
|
8
|
+
|
9
|
+
This is a work in progress. Here's what's left to do for first release:
|
10
|
+
|
11
|
+
- Iterate on the interfaces and functionality
|
12
|
+
- Add tests
|
13
|
+
- Make the gem more configurable to fit more use cases
|
14
|
+
- Create an example library of a few components for some design system
|
15
|
+
- Create a demo app with `lookbook` and those components
|
16
|
+
- Add more documentation
|
17
|
+
- split `vident` into `vident` + `vident-rails` gems (and maybe `vident-rspec`) (Phlex can be used outside of Rails)
|
18
|
+
- possibly also split into `vident-phlex` and `vident-view_component` gems ?
|
19
|
+
|
20
|
+
# Motivation
|
21
|
+
|
22
|
+
I love working with Stimulus, but I find manually crafting the data attributes for
|
23
|
+
targets and actions error prone and tedious. Vident aims to make this process easier
|
24
|
+
and keep me thinking in Ruby.
|
25
|
+
|
26
|
+
I have been using Vident with `ViewComponent` in production apps for a while now and it has been constantly
|
27
|
+
evolving.
|
28
|
+
|
29
|
+
This gem is a work in progress and I would love to get your feedback and contributions!
|
30
|
+
|
31
|
+
## What does Vident provide?
|
32
|
+
|
33
|
+
- `Vident::Component`: A mixin for your `ViewComponent` components or `Phlex` components that provides the a helper to create the
|
34
|
+
root element component (in templated or template-less components).
|
35
|
+
|
36
|
+
- `Vident::TypedComponent`: like `Vident::Component` but uses `dry-types` to define typed attributes for your components.
|
37
|
+
|
38
|
+
- `Vident::RootComponent::*` which are components for creating the 'root' element in your view components. Similar to `Primer::BaseComponent` but
|
39
|
+
exposes a simple API for configuring and adding Stimulus controllers, targets and actions. Normally you create these
|
40
|
+
using the `root` helper method on `Vident::Component`/`Vident::TypedComponent`.
|
41
|
+
|
42
|
+
# Example
|
43
|
+
|
44
|
+
Consider the following ERB that might be part of an application's views. The app uses `ViewComponent`, `Stimulus` and `Vident`.
|
45
|
+
|
46
|
+
The Greeter is a component that displays a text input and a button. When the button is clicked, the text input's value is
|
47
|
+
used to greet the user. At the same time the button changes to be a 'reset' button, which resets the greeting when clicked again.
|
48
|
+
|
49
|
+
![ex1.gif](examples%2Fex1.gif)
|
50
|
+
|
51
|
+
```erb
|
52
|
+
<%# app/views/home/index.html.erb %>
|
53
|
+
|
54
|
+
<!-- ... -->
|
55
|
+
|
56
|
+
<!-- render the Greeter ViewComponent (that uses Vident) -->
|
57
|
+
<%= render ::GreeterComponent.new do |greeter| %>
|
58
|
+
<%# this component has a slot called `trigger` that renders a `ButtonComponent` (which also uses Vident) %>
|
59
|
+
<% greeter.trigger(
|
60
|
+
|
61
|
+
# The button component has attributes that are typed
|
62
|
+
before_clicked: "Greet",
|
63
|
+
after_clicked: "Greeted! Reset?",
|
64
|
+
|
65
|
+
# A stimulus action is added to the button that triggers the `greet` action on the greeter stimulus controller.
|
66
|
+
# This action will be added to any defined on the button component itself
|
67
|
+
actions: [
|
68
|
+
greeter.action(:click, :greet),
|
69
|
+
],
|
70
|
+
|
71
|
+
# We can also override the default button classes of our component, or set other HTML attributes
|
72
|
+
html_options: {
|
73
|
+
class: "bg-red-500 hover:bg-red-700"
|
74
|
+
}
|
75
|
+
) %>
|
76
|
+
<% end %>
|
77
|
+
|
78
|
+
<!-- ... -->
|
79
|
+
```
|
80
|
+
|
81
|
+
The output HTML of the above, using Vident, is:
|
82
|
+
|
83
|
+
```html
|
84
|
+
<div class="greeter-component"
|
85
|
+
data-controller="greeter-component"
|
86
|
+
data-greeter-component-pre-click-class="text-md text-gray-500"
|
87
|
+
data-greeter-component-post-click-class="text-xl text-blue-700"
|
88
|
+
id="greeter-component-1599855-6">
|
89
|
+
<input type="text"
|
90
|
+
data-greeter-component-target="name"
|
91
|
+
class="shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
92
|
+
<button class="button-component ml-4 whitespace-no-wrap bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded bg-red-500 hover:bg-red-700"
|
93
|
+
data-controller="button-component"
|
94
|
+
data-action="click->greeter-component#greet button-component#changeMessage"
|
95
|
+
data-button-component-after-clicked-message="Greeted! Reset?"
|
96
|
+
data-button-component-before-clicked-message="Greet"
|
97
|
+
id="button-component-7799479-7">Greet</button>
|
98
|
+
<!-- you can also use the `target_tag` helper to render targets -->
|
99
|
+
<span class="ml-4 text-md text-gray-500"
|
100
|
+
data-greeter-component-target="output">
|
101
|
+
...
|
102
|
+
</span>
|
103
|
+
</div>
|
104
|
+
```
|
105
|
+
|
106
|
+
Let's look at the components in more detail.
|
107
|
+
|
108
|
+
The main component is the `GreeterComponent`:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# app/components/greeter_component.rb
|
112
|
+
|
113
|
+
class GreeterComponent < ViewComponent::Base
|
114
|
+
include Vident::Component
|
115
|
+
|
116
|
+
renders_one :trigger, ButtonComponent
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
```erb
|
121
|
+
<%# app/components/greeter_component.html.erb %>
|
122
|
+
|
123
|
+
<%# Rendering the `root` element creates a tag which has stimulus `data-*`s, a unique id & other attributes set. %>
|
124
|
+
<%# The stimulus controller name (identifier) is derived from the component name, and then used to generate the relavent data attribute names. %>
|
125
|
+
|
126
|
+
<%= render root named_classes: {
|
127
|
+
pre_click: "text-md text-gray-500", # named classes are exposed to Stimulus as `data-<controller>-<name>-class` attributes
|
128
|
+
post_click: "text-xl text-blue-700"
|
129
|
+
} do |greeter| %>
|
130
|
+
|
131
|
+
<%# `greeter` is the root element and exposes methods to generate stimulus targets and actions %>
|
132
|
+
<input type="text"
|
133
|
+
<%= greeter.as_target(:name) %>
|
134
|
+
class="shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
|
135
|
+
|
136
|
+
<%# Render the slot %>
|
137
|
+
<%= trigger %>
|
138
|
+
|
139
|
+
<%# you can also use the `target_tag` helper to render targets %>
|
140
|
+
<%= greeter.target_tag(
|
141
|
+
:span,
|
142
|
+
:output,
|
143
|
+
# Stimulus named classes can be referenced to set class attributes at render time
|
144
|
+
class: "ml-4 #{greeter.named_classes(:pre_click)}"
|
145
|
+
) do %>
|
146
|
+
...
|
147
|
+
<% end %>
|
148
|
+
<% end %>
|
149
|
+
|
150
|
+
```
|
151
|
+
|
152
|
+
```js
|
153
|
+
// app/components/greeter_component_controller.js
|
154
|
+
|
155
|
+
import { Controller } from "@hotwired/stimulus"
|
156
|
+
|
157
|
+
// This is a Stimulus controller that is automatically registered for the `GreeterComponent`
|
158
|
+
// and is 'sidecar' to the component. You can see that while in the ERB we use Ruby naming conventions
|
159
|
+
// with snake_case Symbols, here they are converted to camelCase names. We can also just use camelCase
|
160
|
+
// in the ERB if we want.
|
161
|
+
export default class extends Controller {
|
162
|
+
static targets = [ "name", "output" ]
|
163
|
+
static classes = [ "preClick", "postClick" ]
|
164
|
+
|
165
|
+
greet() {
|
166
|
+
this.clicked = !this.clicked;
|
167
|
+
this.outputTarget.classList.toggle(this.preClickClasses, !this.clicked);
|
168
|
+
this.outputTarget.classList.toggle(this.postClickClasses, this.clicked);
|
169
|
+
|
170
|
+
if (this.clicked)
|
171
|
+
this.outputTarget.textContent = `Hello, ${this.nameTarget.value}!`
|
172
|
+
else
|
173
|
+
this.clear();
|
174
|
+
}
|
175
|
+
|
176
|
+
clear() {
|
177
|
+
this.outputTarget.textContent = '...';
|
178
|
+
this.nameTarget.value = '';
|
179
|
+
}
|
180
|
+
}
|
181
|
+
```
|
182
|
+
|
183
|
+
The slot renders a `ButtonComponent` component:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# app/components/button_component.rb
|
187
|
+
|
188
|
+
class ButtonComponent < ViewComponent::Base
|
189
|
+
# This component uses Vident::TypedComponent which uses dry-types to define typed attributes.
|
190
|
+
include Vident::TypedComponent
|
191
|
+
|
192
|
+
# The attributes can specify an expected type, a default value and if nil is allowed.
|
193
|
+
attribute :after_clicked, String, default: "Greeted!"
|
194
|
+
attribute :before_clicked, String, allow_nil: false
|
195
|
+
|
196
|
+
# This example is a templateless ViewComponent.
|
197
|
+
def call
|
198
|
+
# The button is rendered as a <button> tag with an click action on its own controller.
|
199
|
+
render root(
|
200
|
+
element_tag: :button,
|
201
|
+
|
202
|
+
# We can define actions as arrays of Symbols, or pass manually manually crafted strings.
|
203
|
+
# Here we specify the action name only, implying an action on the current components controller
|
204
|
+
# and the default event type of `click`.
|
205
|
+
actions: [:change_message],
|
206
|
+
# Alternatively: [:click, :change_message] or ["click", "changeMessage"] or even "click->button-component#changeMessage"
|
207
|
+
|
208
|
+
# A couple of data values are also set which will be available to the controller
|
209
|
+
data_maps: [{after_clicked_message: after_clicked, before_clicked_message: before_clicked}],
|
210
|
+
|
211
|
+
# The <button> tag has a default styling set directly on it. Note that
|
212
|
+
# if not using utility classes, you can style the component using its
|
213
|
+
# canonical class name (which is equal to the component's stimulus identifier),
|
214
|
+
# in this case `button-component`.
|
215
|
+
html_options: {class: "ml-4 whitespace-no-wrap bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"}
|
216
|
+
) do
|
217
|
+
@before_clicked
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
```js
|
224
|
+
// app/components/button_component_controller.js
|
225
|
+
|
226
|
+
import { Controller } from "@hotwired/stimulus"
|
227
|
+
|
228
|
+
export default class extends Controller {
|
229
|
+
// The action is in camelCase.
|
230
|
+
changeMessage() {
|
231
|
+
this.clicked = !this.clicked;
|
232
|
+
// The data attributes have their naming convention converted to camelCase.
|
233
|
+
this.element.textContent = this.clicked ? this.data.get("afterClickedMessage") : this.data.get("beforeClickedMessage");
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
```
|
238
|
+
|
239
|
+
## Installation
|
240
|
+
|
241
|
+
Add this line to your application's Gemfile:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
gem 'vident'
|
245
|
+
```
|
246
|
+
|
247
|
+
Also ensure you have installed your chosen view component library, eg:
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
gem 'view_component'
|
251
|
+
```
|
252
|
+
|
253
|
+
or
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
gem 'phlex' # Must be version 0.5 or higher
|
257
|
+
```
|
258
|
+
|
259
|
+
or **both**!
|
260
|
+
|
261
|
+
If you want to use typed attributes you must also include `dry-struct`
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
gem 'dry-struct'
|
265
|
+
```
|
266
|
+
|
267
|
+
And then execute:
|
268
|
+
|
269
|
+
$ bundle install
|
270
|
+
|
271
|
+
## Making 'sidecar' Stimulus Controllers work
|
272
|
+
|
273
|
+
### When using `stimulus-rails`, `sprockets-rails` & `importmap-rails`
|
274
|
+
|
275
|
+
Pin any JS modules from under `app/views` and `app/components` which are sidecar with their respective components.
|
276
|
+
|
277
|
+
Add to `config/importmap.rb`:
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
components_directories = [Rails.root.join("app/components"), Rails.root.join("app/views")]
|
281
|
+
components_directories.each do |components_path|
|
282
|
+
prefix = components_path.basename.to_s
|
283
|
+
components_path.glob("**/*_controller.js").each do |controller|
|
284
|
+
name = controller.relative_path_from(components_path).to_s.remove(/\.js$/)
|
285
|
+
pin "#{prefix}/#{name}", to: name
|
286
|
+
end
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
Note we don't use `pin_all_from` as it is meant to work with a subdirectory in `assets.paths`
|
291
|
+
See this for more: https://stackoverflow.com/a/73228193/268602
|
292
|
+
|
293
|
+
Then we need to ensure that sprockets picks up those files in build, so add
|
294
|
+
to the `app/assets/config/manifest.js`:
|
295
|
+
|
296
|
+
```js
|
297
|
+
//= link_tree ../../components .js
|
298
|
+
//= link_tree ../../views .js
|
299
|
+
```
|
300
|
+
|
301
|
+
We also need to add to `assets.paths`. Add to your to `config/application.rb`
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
config.importmap.cache_sweepers.append(Rails.root.join("app/components"), Rails.root.join("app/views"))
|
305
|
+
config.assets.paths.append("app/components", "app/views")
|
306
|
+
```
|
307
|
+
|
308
|
+
### When using `webpacker`
|
309
|
+
|
310
|
+
TODO
|
311
|
+
|
312
|
+
### When using `propshaft`
|
313
|
+
|
314
|
+
TODO
|
315
|
+
|
316
|
+
## Using TypeScript for Stimulus Controllers
|
317
|
+
|
318
|
+
TODO
|
319
|
+
|
320
|
+
## Usage
|
321
|
+
|
322
|
+
TODO: Write usage instructions here
|
323
|
+
|
324
|
+
## Development
|
325
|
+
|
326
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
327
|
+
|
328
|
+
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).
|
329
|
+
|
330
|
+
## Running the Examples in `test/dummy`
|
331
|
+
|
332
|
+
```bash
|
333
|
+
cd test/dummy
|
334
|
+
bundle install
|
335
|
+
rails assets:precompile
|
336
|
+
rails s
|
337
|
+
```
|
338
|
+
|
339
|
+
## Contributing
|
340
|
+
|
341
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stevegeek/vident. 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]/vident/blob/master/CODE_OF_CONDUCT.md).
|
342
|
+
|
343
|
+
## License
|
344
|
+
|
345
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
346
|
+
|
347
|
+
## Code of Conduct
|
348
|
+
|
349
|
+
Everyone interacting in the Vident project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/vident/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "standard/rake"
|
13
|
+
|
14
|
+
task default: %i[test standard]
|
data/examples/ex1.gif
ADDED
Binary file
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "stimulus/manifest"
|
2
|
+
|
3
|
+
namespace :vident do
|
4
|
+
task :stimulus_controller_imports do
|
5
|
+
paths = {
|
6
|
+
"app/components" => "../../../app/components",
|
7
|
+
"app/views" => "../../../app/views",
|
8
|
+
"app/javascript/controllers" => "."
|
9
|
+
}
|
10
|
+
manifests = []
|
11
|
+
paths.each do |controllers_path, import_path|
|
12
|
+
vc_controllers_path = Rails.root.join(controllers_path)
|
13
|
+
manifests << Stimulus::Manifest.extract_controllers_from(vc_controllers_path).collect do |controller_path|
|
14
|
+
controller_path = controller_path.relative_path_from(vc_controllers_path).to_s
|
15
|
+
module_path = controller_path.split(".").first
|
16
|
+
controller_class_name = module_path.camelize.gsub(/::/, "__")
|
17
|
+
tag_name = module_path.remove(/_controller/).tr("_", "-").gsub(/\//, "--")
|
18
|
+
|
19
|
+
<<~JS
|
20
|
+
import #{controller_class_name} from "#{import_path}/#{controller_path.gsub(".ts", "")}"
|
21
|
+
application.register("#{tag_name}", #{controller_class_name});
|
22
|
+
JS
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
File.open(Rails.root.join("app/javascript/controllers/index.js"), "w+") do |index|
|
27
|
+
index.puts "// This file is auto-generated by ./bin/rails stimulus_controller_imports"
|
28
|
+
index.puts "// Run that command whenever you add a new controller or create them with"
|
29
|
+
index.puts "// ./bin/rails generate stimulus controllerName"
|
30
|
+
index.puts
|
31
|
+
index.puts %(import { application } from "./application")
|
32
|
+
manifests.each do |manifest|
|
33
|
+
index.puts manifest
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Vident
|
4
|
+
module Attributes
|
5
|
+
module NotTyped
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def attributes
|
9
|
+
@__attributes ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def prepare_attributes(attributes)
|
13
|
+
@__attributes ||= {}
|
14
|
+
attribute_names.each do |attr_name|
|
15
|
+
options = self.class.attribute_options
|
16
|
+
default = options&.dig(attr_name, :default)
|
17
|
+
allow_nil = options[attr_name] ? options[attr_name].fetch(:allow_nil, true) : true
|
18
|
+
if attributes&.include? attr_name
|
19
|
+
value = attributes[attr_name]
|
20
|
+
@__attributes[attr_name] = (value.nil? && default) ? default : value
|
21
|
+
else
|
22
|
+
@__attributes[attr_name] = default
|
23
|
+
end
|
24
|
+
raise ArgumentError, "Attribute #{attr_name} cannot be nil" if @__attributes[attr_name].nil? && !allow_nil
|
25
|
+
instance_variable_set(self.class.attribute_ivar_names[attr_name], @__attributes[attr_name])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def attribute_names
|
30
|
+
self.class.attribute_names
|
31
|
+
end
|
32
|
+
|
33
|
+
def attribute(key)
|
34
|
+
attributes[key]
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_hash
|
38
|
+
attributes.dup
|
39
|
+
end
|
40
|
+
|
41
|
+
class_methods do
|
42
|
+
def inherited(subclass)
|
43
|
+
subclass.instance_variable_set(:@attribute_ivar_names, @attribute_ivar_names.clone)
|
44
|
+
super
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :attribute_ivar_names, :attribute_names, :attribute_options
|
48
|
+
|
49
|
+
def attribute(name, **options)
|
50
|
+
@attribute_names ||= []
|
51
|
+
@attribute_names << name
|
52
|
+
@attribute_ivar_names ||= {}
|
53
|
+
@attribute_ivar_names[name] = :"@#{name}"
|
54
|
+
@attribute_options ||= {}
|
55
|
+
@attribute_options[name] = options
|
56
|
+
define_attribute_delegate(name) if delegates?(options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def delegates?(options)
|
60
|
+
options[:delegates] != false
|
61
|
+
end
|
62
|
+
|
63
|
+
def define_attribute_delegate(attr_name)
|
64
|
+
# Define reader & presence check method
|
65
|
+
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
66
|
+
def #{attr_name}
|
67
|
+
@#{attr_name}
|
68
|
+
end
|
69
|
+
|
70
|
+
def #{attr_name}?
|
71
|
+
send(:#{attr_name}).present?
|
72
|
+
end
|
73
|
+
RUBY
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|