Module: InertiaRails::Generators::Helper

Constant Summary collapse

DEFAULT_PACKAGE_PATH =
Rails.root.join('package.json')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guess_inertia_template(package_json_path = DEFAULT_PACKAGE_PATH) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/inertia_rails/generators/helper.rb', line 29

def guess_inertia_template(package_json_path = DEFAULT_PACKAGE_PATH)
  if package_json_path.read.include?('"tailwindcss"')
    'inertia_tw_templates'
  else
    'inertia_templates'
  end
end

.guess_the_default_framework(package_json_path = DEFAULT_PACKAGE_PATH) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/inertia_rails/generators/helper.rb', line 9

def guess_the_default_framework(package_json_path = DEFAULT_PACKAGE_PATH)
  package_json = JSON.parse(package_json_path.read)
  dependencies = package_json['dependencies'] || {}

  if dependencies['@inertiajs/react']
    'react'
  elsif dependencies['@inertiajs/svelte']
    'svelte'
  elsif dependencies['@inertiajs/vue3']
    'vue'
  else
    Thor::Shell::Basic.new.say_error 'Could not determine the Inertia.js framework you are using.'
    exit 1
  end
end

.uses_typescript?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/inertia_rails/generators/helper.rb', line 25

def uses_typescript?
  Rails.root.join('tsconfig.json').exist?
end

Instance Method Details

#attributes_to_serializeObject



54
55
56
57
58
59
60
# File 'lib/inertia_rails/generators/helper.rb', line 54

def attributes_to_serialize
  [:id] + attributes.reject do |attribute|
    attribute.password_digest? ||
      attribute.attachment? ||
      attribute.attachments?
  end.map(&:column_name)
end

#custom_form_attributesObject



62
63
64
65
66
67
68
# File 'lib/inertia_rails/generators/helper.rb', line 62

def custom_form_attributes
  attributes.select do |attribute|
    attribute.password_digest? ||
      attribute.attachment? ||
      attribute.attachments?
  end
end

#default_value(attribute) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/inertia_rails/generators/helper.rb', line 124

def default_value(attribute)
  case attribute.type
  when :boolean
    'false'
  else
    "''"
  end
end

#inertia_base_pathObject



38
39
40
# File 'lib/inertia_rails/generators/helper.rb', line 38

def inertia_base_path
  (class_path + [file_name.pluralize]).join('/')
end

#inertia_component_nameObject



42
43
44
# File 'lib/inertia_rails/generators/helper.rb', line 42

def inertia_component_name
  singular_name.camelize
end

#inertia_model_form_typeObject



50
51
52
# File 'lib/inertia_rails/generators/helper.rb', line 50

def inertia_model_form_type
  "#{inertia_component_name}FormType"
end

#inertia_model_typeObject



46
47
48
# File 'lib/inertia_rails/generators/helper.rb', line 46

def inertia_model_type
  "#{inertia_component_name}Type"
end

#input_type(attribute) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inertia_rails/generators/helper.rb', line 105

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_pathObject



78
79
80
# File 'lib/inertia_rails/generators/helper.rb', line 78

def js_edit_resource_path
  "#{route_url}/${#{singular_table_name}.id}/edit"
end

#js_new_resource_pathObject



82
83
84
# File 'lib/inertia_rails/generators/helper.rb', line 82

def js_new_resource_path
  "#{route_url}/new"
end

#js_resource_pathObject



74
75
76
# File 'lib/inertia_rails/generators/helper.rb', line 74

def js_resource_path
  "#{route_url}/${#{singular_table_name}.id}"
end

#js_resources_pathObject



86
87
88
# File 'lib/inertia_rails/generators/helper.rb', line 86

def js_resources_path
  route_url
end

#omit_input_attributesObject



70
71
72
# File 'lib/inertia_rails/generators/helper.rb', line 70

def omit_input_attributes
  ['id'] + attributes.select { |attribute| attribute.attachment? || attribute.attachments? }.map(&:column_name)
end

#ts_type(attribute) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/inertia_rails/generators/helper.rb', line 90

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