Module: InertiaRails::Generators::Helper
- Included in:
- Inertia::Generators::ControllerGenerator, Inertia::Generators::ScaffoldControllerGenerator, ControllerTemplateBase
- Defined in:
- lib/inertia_rails/generators/helper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attributes_to_serialize ⇒ Object
- #custom_form_attributes ⇒ Object
- #default_value(attribute) ⇒ Object
- #inertia_base_path ⇒ Object
- #inertia_component_name ⇒ Object
- #inertia_js_version ⇒ Object
- #inertia_model_form_type ⇒ Object
- #inertia_model_type ⇒ Object
- #input_type(attribute) ⇒ Object
- #js_edit_resource_path ⇒ Object
- #js_new_resource_path ⇒ Object
- #js_resource_path ⇒ Object
- #js_resources_path ⇒ Object
- #omit_input_attributes ⇒ Object
- #ts_type(attribute) ⇒ Object
Class Method Details
.guess_inertia_template ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/inertia_rails/generators/helper.rb', line 26 def guess_inertia_template if Rails.root.join('tailwind.config.js').exist? || Rails.root.join('tailwind.config.ts').exist? 'inertia_tw_templates' else 'inertia_templates' end end |
.guess_the_default_framework ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/inertia_rails/generators/helper.rb', line 7 def guess_the_default_framework package = Rails.root.join('package.json').read case package when %r{@inertiajs/react} 'react' when %r{@inertiajs/svelte} package.match?(/"svelte": "\^5/) ? 'svelte' : 'svelte4' when %r{@inertiajs/vue3} 'vue' else Thor::Shell::Basic.new.say_error 'Could not determine the Inertia.js framework you are using.' exit 1 end end |
.guess_typescript ⇒ Object
22 23 24 |
# File 'lib/inertia_rails/generators/helper.rb', line 22 def guess_typescript Rails.root.join('tsconfig.json').exist? end |
Instance Method Details
#attributes_to_serialize ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/inertia_rails/generators/helper.rb', line 51 def attributes_to_serialize [:id] + attributes.reject do |attribute| attribute.password_digest? || attribute. || attribute. end.map(&:column_name) end |
#custom_form_attributes ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/inertia_rails/generators/helper.rb', line 59 def custom_form_attributes attributes.select do |attribute| attribute.password_digest? || attribute. || attribute. end end |
#default_value(attribute) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/inertia_rails/generators/helper.rb', line 129 def default_value(attribute) case attribute.type when :boolean 'false' else "''" end end |
#inertia_base_path ⇒ Object
35 36 37 |
# File 'lib/inertia_rails/generators/helper.rb', line 35 def inertia_base_path (class_path + [file_name]).map(&:camelize).join('/') end |
#inertia_component_name ⇒ Object
39 40 41 |
# File 'lib/inertia_rails/generators/helper.rb', line 39 def inertia_component_name singular_name.camelize end |
#inertia_js_version ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/inertia_rails/generators/helper.rb', line 87 def inertia_js_version @inertia_js_version ||= Gem::Version.new( JSON.parse(`npm ls @inertiajs/core --json`).then do |json| json['dependencies'].values.first['version'] end ) end |
#inertia_model_form_type ⇒ Object
47 48 49 |
# File 'lib/inertia_rails/generators/helper.rb', line 47 def inertia_model_form_type "#{inertia_component_name}FormType" end |
#inertia_model_type ⇒ Object
43 44 45 |
# File 'lib/inertia_rails/generators/helper.rb', line 43 def inertia_model_type "#{inertia_component_name}Type" end |
#input_type(attribute) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/inertia_rails/generators/helper.rb', line 110 def input_type(attribute) case attribute.type when :text, :rich_text 'text_area' when :integer, :float, :decimal 'number' when :datetime, :timestamp, :time 'datetime-local' when :date 'date' when :boolean 'checkbox' when :attachments, :attachment 'file' else 'text' end end |
#js_edit_resource_path ⇒ Object
75 76 77 |
# File 'lib/inertia_rails/generators/helper.rb', line 75 def js_edit_resource_path "#{route_url}/${#{singular_table_name}.id}/edit" end |
#js_new_resource_path ⇒ Object
79 80 81 |
# File 'lib/inertia_rails/generators/helper.rb', line 79 def js_new_resource_path "#{route_url}/new" end |
#js_resource_path ⇒ Object
71 72 73 |
# File 'lib/inertia_rails/generators/helper.rb', line 71 def js_resource_path "#{route_url}/${#{singular_table_name}.id}" end |
#js_resources_path ⇒ Object
83 84 85 |
# File 'lib/inertia_rails/generators/helper.rb', line 83 def js_resources_path route_url end |
#omit_input_attributes ⇒ Object
67 68 69 |
# File 'lib/inertia_rails/generators/helper.rb', line 67 def omit_input_attributes ['id'] + attributes.select { |attribute| attribute. || attribute. }.map(&:column_name) end |
#ts_type(attribute) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/inertia_rails/generators/helper.rb', line 95 def ts_type(attribute) case attribute.type when :float, :decimal, :integer 'number' when :boolean 'boolean' when :attachment '{ filename: string; url: string }' when :attachments '{ filename: string; url: string }[]' else 'string' end end |