Skip to content

Instantly share code, notes, and snippets.

@Yardboy
Created April 27, 2020 20:20
Show Gist options
  • Save Yardboy/ca4c21d99dc589d2553bb1aabb94fe17 to your computer and use it in GitHub Desktop.
Save Yardboy/ca4c21d99dc589d2553bb1aabb94fe17 to your computer and use it in GitHub Desktop.
js.erb template
import $ from 'jquery'
import _ from 'lodash'
<% klasses = [] %>
<% Dir[Rails.root.join('app', 'javascript', 'pages', '*_page.js*')].each do |file| %>
<%
page = file.match(%r{.*/pages/(.*).js?}).captures.first
next if page == 'base_page'
klasses << page.classify
%>
<%= "import #{klasses.last} from './#{page}.js'" %>
<% end %>
const Pages = {}
<% klasses.each do |klass| %>
<%= "Pages.#{klass} = #{klass}" %>
<% end %>
const runner = () => {
const $body = $('body')
const controller = _.startCase($body.data('controller')).replace(' ', '')
const action = _.startCase($body.data('action')).replace(' ', '')
const module = loadPageComponent(controller, action)
if (module) {
onload(module)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment