Skip to content

Commit 8bfa8c9

Browse files
committed
Add observed attributes generator
1 parent cf574dd commit 8bfa8c9

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

Gemfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ gemspec
66

77
gem "importmap-rails"
88
gem "propshaft"
9-
109
gem "puma"
11-
1210
gem "sqlite3", "~> 1.4"
13-
14-
# Start debugger with binding.b [https://github.com/ruby/debug]
15-
# gem "debug", ">= 1.0.0"
11+
gem "debug", ">= 1.0.0"
1612
gem "capybara"
1713
gem "selenium-webdriver"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ GEM
103103
connection_pool (2.4.1)
104104
crass (1.0.6)
105105
date (3.3.4)
106+
debug (1.9.2)
107+
irb (~> 1.10)
108+
reline (>= 0.3.8)
106109
drb (2.2.1)
107110
erubi (1.12.0)
108111
globalid (1.2.1)
@@ -229,6 +232,7 @@ DEPENDENCIES
229232
appraisal
230233
capybara
231234
custom_elements-rails!
235+
debug (>= 1.0.0)
232236
importmap-rails
233237
propshaft
234238
puma

lib/generators/custom_element_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class CustomElementGenerator < Rails::Generators::NamedBase
22
source_root File.expand_path("templates", __dir__)
33

4+
argument :attributes, type: :array, default: [], banner: "Observed attributes"
5+
46
def copy_custom_element
57
template "custom_element.js", "app/javascript/custom_elements/#{file_name}_element.js"
68
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
export default class extends HTMLElement {
2+
<% if attributes.any? %>
3+
static observedAttributes = <%= attributes.map { _1.name } %>
4+
<% end %>
25
constructor() {
36
super()
47
}
58

69
connectedCallback() {
710
console.log("<%= file_name %>")
811
}
12+
<% attributes.each do |attribute| -%>
13+
14+
get <%= attribute.name %>() {
15+
return this.getAttribute("<%= attribute.name %>")
16+
}
17+
<% end -%>
918
}

0 commit comments

Comments
 (0)