Module: Clapton::ClaptonHelper
- Defined in:
- app/helpers/clapton/clapton_helper.rb
Instance Method Summary collapse
- #clapton_component_tag(component, params) ⇒ Object
- #clapton_javascript_tag(entry_point = "application", importmap: nil) ⇒ Object
- #clapton_tag ⇒ Object
Instance Method Details
#clapton_component_tag(component, params) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'app/helpers/clapton/clapton_helper.rb', line 51 def clapton_component_tag(component, params) state_class = component.to_s.gsub("Component", "State") if Object.const_defined?(state_class) data = { component: component.to_s, state: Object.const_get(state_class).new(params).to_h } else data = { component: component.to_s, state: {} } end tag.div(class: "clapton-component", data: { clapton: data }) end |
#clapton_javascript_tag(entry_point = "application", importmap: nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/helpers/clapton/clapton_helper.rb', line 4 def clapton_javascript_tag(entry_point = "application", importmap: nil) all_components = Dir.glob(Rails.root.join("app", "components", "**", "*.rb")) clapton_json = JSON.parse " {\n \"imports\": {\n \"client\": \"/clapton/client.js\",\n \"components\": \"/clapton/components.js\",\n \"c\": \"/clapton/c.js\",\n \#{ all_components.map do\n |component| \"\\\"\#{File.basename(component, \".rb\").camelize}\\\": \\\"/clapton/\#{File.basename(component, \".rb\").camelize}.js\\\"\"\n end.join(\",\\n\") }\n }\n }\n JSON\n if defined?(javascript_importmap_tags)\n importmap ||= Rails.application.importmap\n json = { imports: JSON.parse(importmap.to_json(resolver: self))[\"imports\"].merge(clapton_json[\"imports\"]) }\n safe_join [\n javascript_inline_importmap_tag(json.to_json),\n javascript_importmap_module_preload_tags(importmap, entry_point:),\n javascript_import_module_tag(entry_point),\n tag.script(type: \"module\", src: \"/clapton/client.js\"),\n ], \"\\n\"\n else\n html = <<~HTML\n <script type=\"importmap\">\n \#{clapton_json.to_json}\n </script>\n <script type=\"module\" src=\"/clapton/client.js\"></script>\n HTML\n html.html_safe\n end\nend\n" |
#clapton_tag ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/clapton/clapton_helper.rb', line 38 def clapton_tag datas = [] @components.each do |component| state_class = component[0].to_s.gsub("Component", "State") if Object.const_defined?(state_class) datas << { component: component[0].to_s, state: Object.const_get(state_class).new(component[1]).to_h } else datas << { component: component[0].to_s, state: {} } end end tag.div(id: "clapton", data: { clapton: datas }) end |