inertia_rails-contrib 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +21 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +187 -0
  5. data/lib/generators/inertia/controller/controller_generator.rb +16 -0
  6. data/lib/generators/inertia/controller/templates/controller.rb.tt +10 -0
  7. data/lib/generators/inertia/install/install_generator.rb +151 -0
  8. data/lib/generators/inertia/install/templates/assets/inertia.svg +1 -0
  9. data/lib/generators/inertia/install/templates/assets/react.svg +1 -0
  10. data/lib/generators/inertia/install/templates/assets/svelte.svg +1 -0
  11. data/lib/generators/inertia/install/templates/assets/vite_ruby.svg +1 -0
  12. data/lib/generators/inertia/install/templates/assets/vue.svg +1 -0
  13. data/lib/generators/inertia/install/templates/controller.rb +7 -0
  14. data/lib/generators/inertia/install/templates/initializer.rb +4 -0
  15. data/lib/generators/inertia/install/templates/react/InertiaExample.jsx +60 -0
  16. data/lib/generators/inertia/install/templates/react/InertiaExample.module.css +80 -0
  17. data/lib/generators/inertia/install/templates/react/inertia.js +34 -0
  18. data/lib/generators/inertia/install/templates/svelte/InertiaExample.svelte +116 -0
  19. data/lib/generators/inertia/install/templates/svelte/inertia.js +30 -0
  20. data/lib/generators/inertia/install/templates/svelte/svelte.config.js +7 -0
  21. data/lib/generators/inertia/install/templates/vue/InertiaExample.vue +117 -0
  22. data/lib/generators/inertia/install/templates/vue/inertia.js +33 -0
  23. data/lib/generators/inertia/scaffold/scaffold_generator.rb +16 -0
  24. data/lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb +56 -0
  25. data/lib/generators/inertia/scaffold_controller/templates/controller.rb.tt +102 -0
  26. data/lib/generators/inertia_templates/controller/controller_generator.rb +12 -0
  27. data/lib/generators/inertia_templates/controller/templates/react/view.jsx.tt +8 -0
  28. data/lib/generators/inertia_templates/controller/templates/svelte/view.svelte.tt +2 -0
  29. data/lib/generators/inertia_templates/controller/templates/vue/view.vue.tt +4 -0
  30. data/lib/generators/inertia_templates/scaffold/scaffold_generator.rb +12 -0
  31. data/lib/generators/inertia_templates/scaffold/templates/react/Edit.jsx.tt +36 -0
  32. data/lib/generators/inertia_templates/scaffold/templates/react/Form.jsx.tt +111 -0
  33. data/lib/generators/inertia_templates/scaffold/templates/react/Index.jsx.tt +26 -0
  34. data/lib/generators/inertia_templates/scaffold/templates/react/New.jsx.tt +27 -0
  35. data/lib/generators/inertia_templates/scaffold/templates/react/One.jsx.tt +26 -0
  36. data/lib/generators/inertia_templates/scaffold/templates/react/Show.jsx.tt +39 -0
  37. data/lib/generators/inertia_templates/scaffold/templates/svelte/Edit.svelte.tt +37 -0
  38. data/lib/generators/inertia_templates/scaffold/templates/svelte/Form.svelte.tt +96 -0
  39. data/lib/generators/inertia_templates/scaffold/templates/svelte/Index.svelte.tt +42 -0
  40. data/lib/generators/inertia_templates/scaffold/templates/svelte/New.svelte.tt +30 -0
  41. data/lib/generators/inertia_templates/scaffold/templates/svelte/One.svelte.tt +28 -0
  42. data/lib/generators/inertia_templates/scaffold/templates/svelte/Show.svelte.tt +46 -0
  43. data/lib/generators/inertia_templates/scaffold/templates/vue/Edit.vue.tt +36 -0
  44. data/lib/generators/inertia_templates/scaffold/templates/vue/Form.vue.tt +94 -0
  45. data/lib/generators/inertia_templates/scaffold/templates/vue/Index.vue.tt +31 -0
  46. data/lib/generators/inertia_templates/scaffold/templates/vue/New.vue.tt +29 -0
  47. data/lib/generators/inertia_templates/scaffold/templates/vue/One.vue.tt +26 -0
  48. data/lib/generators/inertia_templates/scaffold/templates/vue/Show.vue.tt +44 -0
  49. data/lib/generators/inertia_tw_templates/controller/controller_generator.rb +12 -0
  50. data/lib/generators/inertia_tw_templates/controller/templates/react/view.jsx.tt +8 -0
  51. data/lib/generators/inertia_tw_templates/controller/templates/svelte/view.svelte.tt +2 -0
  52. data/lib/generators/inertia_tw_templates/controller/templates/vue/view.vue.tt +4 -0
  53. data/lib/generators/inertia_tw_templates/scaffold/scaffold_generator.rb +12 -0
  54. data/lib/generators/inertia_tw_templates/scaffold/templates/react/Edit.jsx.tt +43 -0
  55. data/lib/generators/inertia_tw_templates/scaffold/templates/react/Form.jsx.tt +122 -0
  56. data/lib/generators/inertia_tw_templates/scaffold/templates/react/Index.jsx.tt +43 -0
  57. data/lib/generators/inertia_tw_templates/scaffold/templates/react/New.jsx.tt +30 -0
  58. data/lib/generators/inertia_tw_templates/scaffold/templates/react/One.jsx.tt +26 -0
  59. data/lib/generators/inertia_tw_templates/scaffold/templates/react/Show.jsx.tt +54 -0
  60. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/Edit.svelte.tt +45 -0
  61. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/Form.svelte.tt +120 -0
  62. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/Index.svelte.tt +49 -0
  63. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/New.svelte.tt +33 -0
  64. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/One.svelte.tt +28 -0
  65. data/lib/generators/inertia_tw_templates/scaffold/templates/svelte/Show.svelte.tt +58 -0
  66. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/Edit.vue.tt +44 -0
  67. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/Form.vue.tt +127 -0
  68. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/Index.vue.tt +43 -0
  69. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/New.vue.tt +32 -0
  70. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/One.vue.tt +26 -0
  71. data/lib/generators/inertia_tw_templates/scaffold/templates/vue/Show.vue.tt +56 -0
  72. data/lib/inertia_rails-contrib.rb +3 -0
  73. data/lib/inertia_rails_contrib/generators/controller_template_base.rb +52 -0
  74. data/lib/inertia_rails_contrib/generators/scaffold_template_base.rb +35 -0
  75. data/lib/inertia_rails_contrib/generators_helper.rb +88 -0
  76. data/lib/inertia_rails_contrib/version.rb +5 -0
  77. data/lib/inertia_rails_contrib.rb +8 -0
  78. metadata +153 -0
@@ -0,0 +1,116 @@
1
+ <script>
2
+ import svelteSvg from '/assets/svelte.svg'
3
+ import inertiaSvg from '/assets/inertia.svg'
4
+ import viteRubySvg from '/assets/vite_ruby.svg'
5
+
6
+ export let name
7
+
8
+ let count = 0
9
+
10
+ function handleClick() {
11
+ count += 1
12
+ }
13
+ </script>
14
+
15
+ <svelte:head>
16
+ <title>Inertia + Vite Ruby + Svelte Example</title>
17
+ </svelte:head>
18
+
19
+ <div class="inertia_example">
20
+ <h1>Hello {name}!</h1>
21
+
22
+ <div>
23
+ <a href="https://inertia-rails.netlify.app" target="_blank">
24
+ <img class="logo" src={inertiaSvg} alt="Inertia logo" />
25
+ </a>
26
+ <a href="https://vite-ruby.netlify.app" target="_blank">
27
+ <img class="logo vite" src={viteRubySvg} alt="Vite Ruby logo" />
28
+ </a>
29
+ <a href="https://svelte.dev" target="_blank">
30
+ <img class="logo svelte" src={svelteSvg} alt="Svelte logo" />
31
+ </a>
32
+ </div>
33
+
34
+ <h2>Inertia + Vite Ruby + Svelte</h2>
35
+
36
+ <div class="card">
37
+ <button on:click={handleClick}>
38
+ count is {count}
39
+ </button>
40
+ <p>
41
+ Edit <code>app/frontend/pages/InertiaExample.svelte</code> and save to test
42
+ HMR
43
+ </p>
44
+ </div>
45
+ <p class="read-the-docs">
46
+ Click on the Inertia, Vite Ruby, and Svelte logos to learn more
47
+ </p>
48
+ </div>
49
+
50
+ <style>
51
+ .inertia_example {
52
+ font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
53
+ line-height: 1.5;
54
+ font-weight: 400;
55
+ color: #213547;
56
+ background-color: #ffffff;
57
+ max-width: 1280px;
58
+ margin: 0 auto;
59
+ padding: 2rem;
60
+ text-align: center;
61
+ }
62
+
63
+ h1 {
64
+ font-size: 3.2em;
65
+ line-height: 1.1;
66
+ }
67
+
68
+ h2 {
69
+ font-size: 2.6em;
70
+ line-height: 1.1;
71
+ }
72
+
73
+ button {
74
+ border-radius: 8px;
75
+ border: 1px solid transparent;
76
+ padding: 0.6em 1.2em;
77
+ font-size: 1em;
78
+ font-weight: 500;
79
+ font-family: inherit;
80
+ background-color: #f9f9f9;
81
+ cursor: pointer;
82
+ transition: border-color 0.25s;
83
+ }
84
+ button:hover {
85
+ border-color: #646cff;
86
+ }
87
+ button:focus,
88
+ button:focus-visible {
89
+ outline: 4px auto -webkit-focus-ring-color;
90
+ }
91
+
92
+ .logo {
93
+ display: inline-block;
94
+ height: 6em;
95
+ padding: 1.5em;
96
+ will-change: filter;
97
+ transition: filter 300ms;
98
+ }
99
+ .logo:hover {
100
+ filter: drop-shadow(0 0 2em #646cffaa);
101
+ }
102
+ .logo.vite:hover {
103
+ filter: drop-shadow(0 0 2em #e4023baa);
104
+ }
105
+ .logo.svelte:hover {
106
+ filter: drop-shadow(0 0 2em #ff3e00aa);
107
+ }
108
+
109
+ .card {
110
+ padding: 2em;
111
+ }
112
+
113
+ .read-the-docs {
114
+ color: #888;
115
+ }
116
+ </style>
@@ -0,0 +1,30 @@
1
+ import { createInertiaApp } from '@inertiajs/svelte'
2
+
3
+ createInertiaApp({
4
+ // Set default page title
5
+ // see https://inertia-rails.netlify.app/guide/title-and-meta
6
+ //
7
+ // title: title => title ? `${title} - App` : 'App',
8
+
9
+ // Disable progress bar
10
+ //
11
+ // see https://inertia-rails.netlify.app/guide/progress-indicators
12
+ // progress: false,
13
+
14
+ resolve: (name) => {
15
+ const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
16
+ return pages[`../pages/${name}.svelte`]
17
+
18
+ // To use a default layout, import the Layout component
19
+ // and use the following lines.
20
+ // see https://inertia-rails.netlify.app/guide/pages#default-layouts
21
+ //
22
+ // const page = pages[`./pages/${name}.vue`]
23
+ // page.default.layout ||= (page) => createElement(Layout, null, page)
24
+ // return page
25
+ },
26
+
27
+ setup({ el, App, props }) {
28
+ new App({ target: el, props })
29
+ },
30
+ })
@@ -0,0 +1,7 @@
1
+ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
2
+
3
+ export default {
4
+ // Consult https://svelte.dev/docs#compile-time-svelte-preprocess
5
+ // for more information about preprocessors
6
+ preprocess: vitePreprocess(),
7
+ }
@@ -0,0 +1,117 @@
1
+ <template>
2
+ <Head title="Inertia + Vite Ruby + Vue Example" />
3
+
4
+ <div class="root">
5
+ <h1 class="h1">Hello {{ name }}!</h1>
6
+
7
+ <div>
8
+ <a href="https://inertia-rails.netlify.app" target="_blank">
9
+ <img class="logo" :src="inertiaSvg" alt="Inertia logo" />
10
+ </a>
11
+ <a href="https://vite-ruby.netlify.app" target="_blank">
12
+ <img class="logo vite" :src="viteRubySvg" alt="Vite Ruby logo" />
13
+ </a>
14
+ <a href="https://vuejs.org" target="_blank">
15
+ <img class="logo vue" :src="vueSvg" alt="Vue logo" />
16
+ </a>
17
+ </div>
18
+
19
+ <h2 class="h2">Inertia + Vite Ruby + Vue</h2>
20
+
21
+ <div class="card">
22
+ <button class="button" type="button" @click="count++">
23
+ count is {{ count }}
24
+ </button>
25
+ <p>
26
+ Edit <code>app/frontend/pages/InertiaExample.vue</code> and save to test
27
+ HMR
28
+ </p>
29
+ </div>
30
+ <p class="readTheDocs">
31
+ Click on the Inertia, Vite Ruby, and Vue logos to learn more
32
+ </p>
33
+ </div>
34
+ </template>
35
+
36
+ <script setup>
37
+ import { Head } from '@inertiajs/vue3'
38
+ import { ref } from 'vue'
39
+
40
+ import vueSvg from '/assets/vue.svg'
41
+ import inertiaSvg from '/assets/inertia.svg'
42
+ import viteRubySvg from '/assets/vite_ruby.svg'
43
+
44
+ defineProps({
45
+ name: String,
46
+ })
47
+
48
+ const count = ref(0)
49
+ </script>
50
+
51
+ <style scoped>
52
+ .root {
53
+ font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
54
+ line-height: 1.5;
55
+ font-weight: 400;
56
+ color: #213547;
57
+ background-color: #ffffff;
58
+ max-width: 1280px;
59
+ margin: 0 auto;
60
+ padding: 2rem;
61
+ text-align: center;
62
+ }
63
+
64
+ .h1 {
65
+ font-size: 3.2em;
66
+ line-height: 1.1;
67
+ }
68
+
69
+ .h2 {
70
+ font-size: 2.6em;
71
+ line-height: 1.1;
72
+ }
73
+
74
+ .button {
75
+ border-radius: 8px;
76
+ border: 1px solid transparent;
77
+ padding: 0.6em 1.2em;
78
+ font-size: 1em;
79
+ font-weight: 500;
80
+ font-family: inherit;
81
+ background-color: #f9f9f9;
82
+ cursor: pointer;
83
+ transition: border-color 0.25s;
84
+ }
85
+ .button:hover {
86
+ border-color: #646cff;
87
+ }
88
+ .button:focus,
89
+ .button:focus-visible {
90
+ outline: 4px auto -webkit-focus-ring-color;
91
+ }
92
+
93
+ .logo {
94
+ display: inline-block;
95
+ height: 6em;
96
+ padding: 1.5em;
97
+ will-change: filter;
98
+ transition: filter 300ms;
99
+ }
100
+ .logo:hover {
101
+ filter: drop-shadow(0 0 2em #646cffaa);
102
+ }
103
+ .logo.vite:hover {
104
+ filter: drop-shadow(0 0 2em #e4023baa);
105
+ }
106
+ .logo.vue:hover {
107
+ filter: drop-shadow(0 0 2em #41b883aa);
108
+ }
109
+
110
+ .card {
111
+ padding: 2em;
112
+ }
113
+
114
+ .readTheDocs {
115
+ color: #888;
116
+ }
117
+ </style>
@@ -0,0 +1,33 @@
1
+ import { createInertiaApp } from '@inertiajs/vue3'
2
+ import { createApp, h } from 'vue'
3
+
4
+ createInertiaApp({
5
+ // Set default page title
6
+ // see https://inertia-rails.netlify.app/guide/title-and-meta
7
+ //
8
+ // title: title => title ? `${title} - App` : 'App',
9
+
10
+ // Disable progress bar
11
+ //
12
+ // see https://inertia-rails.netlify.app/guide/progress-indicators
13
+ // progress: false,
14
+
15
+ resolve: (name) => {
16
+ const pages = import.meta.glob('../pages/**/*.vue', { eager: true })
17
+ return pages[`../pages/${name}.vue`]
18
+
19
+ // To use a default layout, import the Layout component
20
+ // and use the following lines.
21
+ // see https://inertia-rails.netlify.app/guide/pages#default-layouts
22
+ //
23
+ // const page = pages[`./pages/${name}.vue`]
24
+ // page.default.layout ||= (page) => createElement(Layout, null, page)
25
+ // return page
26
+ },
27
+
28
+ setup({ el, App, props, plugin }) {
29
+ createApp({ render: () => h(App, props) })
30
+ .use(plugin)
31
+ .mount(el)
32
+ },
33
+ })
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/rails/resource/resource_generator"
4
+
5
+ module Inertia
6
+ module Generators
7
+ class ScaffoldGenerator < Rails::Generators::ResourceGenerator # :nodoc:
8
+ remove_hook_for :resource_controller
9
+ remove_class_option :actions
10
+
11
+ class_option :resource_route, type: :boolean
12
+
13
+ hook_for :scaffold_controller, required: true
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,56 @@
1
+ require "rails/generators/resource_helpers"
2
+ require "inertia_rails_contrib/generators_helper"
3
+
4
+ module Inertia
5
+ module Generators
6
+ # This class is a modified copy of Rails::Generators::ScaffoldControllerGenerator.
7
+ # We don't use inheritance because some gems (i.e. jsbuilder) monkey-patch it.
8
+ class ScaffoldControllerGenerator < Rails::Generators::NamedBase
9
+ include InertiaRailsContrib::GeneratorsHelper
10
+ include Rails::Generators::ResourceHelpers
11
+
12
+ source_root File.expand_path("./templates", __dir__)
13
+
14
+ check_class_collision suffix: "Controller"
15
+
16
+ class_option :helper, type: :boolean
17
+ class_option :orm, banner: "NAME", type: :string, required: true,
18
+ desc: "ORM to generate the controller for"
19
+
20
+ class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
21
+
22
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
23
+
24
+ def create_controller_files
25
+ template "controller.rb", File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
26
+ end
27
+
28
+ hook_for :inertia_templates, as: :scaffold, required: true, default: InertiaRailsContrib::GeneratorsHelper.guess_inertia_template
29
+
30
+ hook_for :resource_route, in: :rails, required: true do |route|
31
+ invoke route unless options.skip_routes?
32
+ end
33
+
34
+ hook_for :test_framework, in: :rails, as: :scaffold
35
+
36
+ # Invoke the helper using the controller name (pluralized)
37
+ hook_for :helper, in: :rails, as: :scaffold do |invoked|
38
+ invoke invoked, [controller_name]
39
+ end
40
+
41
+ private
42
+
43
+ def permitted_params
44
+ attachments, others = attributes_names.partition { |name| attachments?(name) }
45
+ params = others.map { |name| ":#{name}" }
46
+ params += attachments.map { |name| "#{name}: []" }
47
+ params.join(", ")
48
+ end
49
+
50
+ def attachments?(name)
51
+ attribute = attributes.find { |attr| attr.name == name }
52
+ attribute&.attachments?
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,102 @@
1
+ <% module_namespacing do -%>
2
+ class <%= controller_class_name %>Controller < ApplicationController
3
+ before_action :set_<%= singular_table_name %>, only: %i[ show edit update destroy ]
4
+
5
+ <% if regular_class_path.any? -%>
6
+ wrap_parameters :<%= singular_table_name %>
7
+
8
+ <% end -%>
9
+ inertia_share flash: -> { flash.to_hash }
10
+
11
+ # GET <%= route_url %>
12
+ def index
13
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
14
+ render inertia: '<%= "#{inertia_base_path}/Index" %>', props: {
15
+ <%= plural_table_name %>: @<%= plural_table_name %>.map do |<%= singular_table_name %>|
16
+ <%= "serialize_#{singular_table_name}" %>(<%= singular_table_name %>)
17
+ end
18
+ }
19
+ end
20
+
21
+ # GET <%= route_url %>/1
22
+ def show
23
+ render inertia: '<%= "#{inertia_base_path}/Show" %>', props: {
24
+ <%= singular_table_name %>: <%= "serialize_#{singular_table_name}" %>(@<%= singular_table_name %>)
25
+ }
26
+ end
27
+
28
+ # GET <%= route_url %>/new
29
+ def new
30
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
31
+ render inertia: '<%= "#{inertia_base_path}/New" %>', props: {
32
+ <%= singular_table_name %>: <%= "serialize_#{singular_table_name}" %>(@<%= singular_table_name %>)
33
+ }
34
+ end
35
+
36
+ # GET <%= route_url %>/1/edit
37
+ def edit
38
+ render inertia: '<%= "#{inertia_base_path}/Edit" %>', props: {
39
+ <%= singular_table_name %>: <%= "serialize_#{singular_table_name}" %>(@<%= singular_table_name %>)
40
+ }
41
+ end
42
+
43
+ # POST <%= route_url %>
44
+ def create
45
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
46
+
47
+ if @<%= orm_instance.save %>
48
+ redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %>
49
+ else
50
+ redirect_to <%= new_helper %>, inertia: { errors: @<%= singular_table_name %>.errors }
51
+ end
52
+ end
53
+
54
+ # PATCH/PUT <%= route_url %>/1
55
+ def update
56
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
57
+ redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %>
58
+ else
59
+ redirect_to <%= edit_helper %>, inertia: { errors: @<%= singular_table_name %>.errors }
60
+ end
61
+ end
62
+
63
+ # DELETE <%= route_url %>/1
64
+ def destroy
65
+ @<%= orm_instance.destroy %>
66
+ redirect_to <%= index_helper %>_url, notice: <%= %("#{human_name} was successfully destroyed.") %>
67
+ end
68
+
69
+ private
70
+ # Use callbacks to share common setup or constraints between actions.
71
+ def set_<%= singular_table_name %>
72
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
73
+ end
74
+
75
+ # Only allow a list of trusted parameters through.
76
+ def <%= "#{singular_table_name}_params" %>
77
+ <%- if attributes_names.empty? -%>
78
+ params.fetch(:<%= singular_table_name %>, {})
79
+ <%- else -%>
80
+ params.require(:<%= singular_table_name %>).permit(<%= permitted_params %>)
81
+ <%- end -%>
82
+ end
83
+
84
+ def <%= "serialize_#{singular_table_name}" %>(<%= singular_table_name %>)
85
+ <%= singular_table_name %>.as_json(only: [
86
+ <%= attributes_to_serialize.map { |attribute| ":#{attribute}" }.join(", ") %>
87
+ ])<% if attributes.any?(&:attachment?) -%>.tap do |hash|
88
+ <% attributes.filter(&:attachment?).map do |attribute| -%>
89
+ hash["<%= attribute.column_name %>"] = {filename: <%= singular_table_name %>.<%= attribute.column_name %>.filename, url: url_for(<%= singular_table_name %>.<%= attribute.column_name %>)} if <%= singular_table_name %>.<%= attribute.column_name %>.attached?
90
+ <% end -%>
91
+ end<% end -%><% if attributes.any?(&:attachments?) -%>.tap do |hash|
92
+ <% attributes.filter(&:attachments?).map do |attribute| -%>
93
+ hash["<%= attribute.column_name %>"] =
94
+ <%= singular_table_name %>.<%= attribute.column_name %>.flat_map do |file|
95
+ {filename: file.filename.to_s, url: url_for(file)}
96
+ end
97
+ <% end -%>
98
+ end
99
+ <% end -%>
100
+ end
101
+ end
102
+ <% end -%>
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "inertia_rails_contrib/generators/controller_template_base"
4
+
5
+ module InertiaTemplates
6
+ module Generators
7
+ class ControllerGenerator < InertiaRailsContrib::Generators::ControllerTemplateBase
8
+ hide!
9
+ source_root File.expand_path("./templates", __dir__)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ export default function <%= @action.camelize %>() {
2
+ return (
3
+ <>
4
+ <h1><%= class_name %>#<%= @action %></h1>
5
+ <p>Find me in <%= @path %></p>
6
+ </>
7
+ );
8
+ }
@@ -0,0 +1,2 @@
1
+ <h1><%= class_name %>#<%= @action %></h1>
2
+ <p>Find me in <%= @path %></p>
@@ -0,0 +1,4 @@
1
+ <template>
2
+ <h1><%= class_name %>#<%= @action %></h1>
3
+ <p>Find me in <%= @path %></p>
4
+ </template>
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "inertia_rails_contrib/generators/scaffold_template_base"
4
+
5
+ module InertiaTemplates
6
+ module Generators
7
+ class ScaffoldGenerator < InertiaRailsContrib::Generators::ScaffoldTemplateBase
8
+ hide!
9
+ source_root File.expand_path("./templates", __dir__)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ import { Link, Head } from '@inertiajs/react'
2
+ import Form from './Form'
3
+
4
+ export default function Edit({ <%= singular_table_name %> }) {
5
+ return (
6
+ <>
7
+ <Head title="Editing <%= human_name.downcase %>" />
8
+
9
+ <h1>Editing <%= human_name.downcase %></h1>
10
+
11
+ <Form
12
+ <%= singular_table_name %>={<%= singular_table_name %>}
13
+ onSubmit={(form) => {
14
+ form.transform((data) => ({ <%= singular_table_name %>: data }))
15
+ <% if attributes.any?(&:attachments?) -%>
16
+ form.post(`<%= js_resource_path %>`, {
17
+ headers: { 'X-HTTP-METHOD-OVERRIDE': 'put' },
18
+ })
19
+ }}
20
+ <% else -%>
21
+ form.patch(`<%= js_resource_path %>`)
22
+ }
23
+ <% end -%>
24
+ submitText="Update <%= human_name.downcase %>"
25
+ />
26
+
27
+ <br />
28
+
29
+ <div>
30
+ <Link href={`<%= js_resource_path %>`}>Show this <%= human_name.downcase %></Link>
31
+ {' | '}
32
+ <Link href="<%= js_resources_path %>">Back to <%= human_name.pluralize.downcase %></Link>
33
+ </div>
34
+ </>
35
+ )
36
+ }
@@ -0,0 +1,111 @@
1
+ import { useForm } from '@inertiajs/react'
2
+
3
+ export default function Form({ <%= singular_table_name %>, onSubmit, submitText }) {
4
+ const form = useForm({
5
+ <% attributes.each do |attribute| -%>
6
+ <% if attribute.password_digest? -%>
7
+ password: '',
8
+ password_confirmation: '',
9
+ <% else -%>
10
+ <%= attribute.column_name %>: <%= singular_table_name %>.<%= attribute.column_name %> || <%= default_value(attribute) %>,
11
+ <% end -%>
12
+ <% end -%>
13
+ })
14
+ const { data, setData, errors, processing } = form
15
+
16
+ const handleSubmit = (e) => {
17
+ e.preventDefault()
18
+ onSubmit(form)
19
+ }
20
+
21
+ return (
22
+ <form onSubmit={handleSubmit}>
23
+ <% attributes.each do |attribute| -%>
24
+ <% if attribute.password_digest? -%>
25
+ <div>
26
+ <label style={{ display: 'block' }} htmlFor="password">
27
+ Password
28
+ </label>
29
+ <input
30
+ type="password"
31
+ name="password"
32
+ id="password"
33
+ onChange={(e) => setData('password', e.target.value)}
34
+ />
35
+ {errors.password && (
36
+ <div style={{ color: 'red' }}>{errors.password.join(', ')}</div>
37
+ )}
38
+ </div>
39
+
40
+ <div>
41
+ <label style={{ display: 'block' }} htmlFor="password_confirmation">
42
+ Password Confirmation
43
+ </label>
44
+ <input
45
+ type="password"
46
+ name="password_confirmation"
47
+ id="password_confirmation"
48
+ onChange={(e) => setData('password_confirmation', e.target.value)}
49
+ />
50
+ {errors.password_confirmation && (
51
+ <div style={{ color: 'red' }}>{errors.password_confirmation.join(', ')}</div>
52
+ )}
53
+ </div>
54
+ <% else -%>
55
+ <div>
56
+ <label style={{ display: 'block' }} htmlFor="<%= attribute.singular_name %>">
57
+ <%= attribute.human_name %>
58
+ </label>
59
+ <% if input_type(attribute) == "text_area" -%>
60
+ <textarea
61
+ name="<%= attribute.singular_name %>"
62
+ id="<%= attribute.singular_name %>"
63
+ value={data.<%= attribute.column_name %>}
64
+ onChange={(e) => setData('<%= attribute.column_name %>', e.target.value)}
65
+ />
66
+ <% elsif attribute.attachment? -%>
67
+ <input
68
+ type="file"
69
+ name="<%= attribute.singular_name %>"
70
+ id="<%= attribute.singular_name %>"
71
+ onChange={(e) => setData('<%= attribute.column_name %>', e.target.files[0])}
72
+ />
73
+ <% elsif attribute.attachments? -%>
74
+ <input
75
+ type="file"
76
+ multiple="multiple"
77
+ name="<%= attribute.singular_name %>[]"
78
+ id="<%= attribute.singular_name %>"
79
+ onChange={(e) => setData('<%= attribute.column_name %>', Array.from(e.target.files))}
80
+ />
81
+ <% elsif attribute.field_type == :check_box -%>
82
+ <input
83
+ type="<%= input_type(attribute) %>"
84
+ name="<%= attribute.singular_name %>"
85
+ id="<%= attribute.singular_name %>"
86
+ checked={data.<%= attribute.column_name %>}
87
+ onChange={(e) => setData('<%= attribute.column_name %>', e.target.checked)}
88
+ />
89
+ <% else -%>
90
+ <input
91
+ type="<%= input_type(attribute) %>"
92
+ name="<%= attribute.singular_name %>"
93
+ id="<%= attribute.singular_name %>"
94
+ value={data.<%= attribute.column_name %>}
95
+ onChange={(e) => setData('<%= attribute.column_name %>', e.target.value)}
96
+ />
97
+ <% end -%>
98
+ {errors.<%= attribute.column_name %> && (
99
+ <div style={{ color: 'red' }}>{errors.<%= attribute.column_name %>.join(', ')}</div>
100
+ )}
101
+ </div>
102
+ <% end -%>
103
+ <% end -%>
104
+ <div>
105
+ <button type="submit" disabled={processing}>
106
+ {submitText}
107
+ </button>
108
+ </div>
109
+ </form>
110
+ )
111
+ }