inertia_rails 3.6.1 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d06f57228ce3ff3b4dba1d2b7bdf99deaa110c8b85c474c106baa989dd1bf8c
4
- data.tar.gz: ec01c88da7419b8dbf1cc1243eced6fd38c10a7fba1c21d75def08ae1516cf01
3
+ metadata.gz: 0f5f574a84809fccf65c7db0f58d34cb622b2ea4d403d05519736d2c44865f69
4
+ data.tar.gz: f62406133f1fa0698a8d2d24418a98f75e01d0400de356e879b86bbb4f657317
5
5
  SHA512:
6
- metadata.gz: 6506c7ae154865ed78172e6f6bc2f28ff09cc8ee00faf25cb9d687f53143c9cbe7e105dbc297c028ceb29189070e63ba5484ecdf4c7217115b2d46b090389321
7
- data.tar.gz: 7891ba539d642509920d1ab0d17c7c79e6bfbb1399c93c1c298a1ed20367b5a758352c644fc2794ea2309053a452643d807a26a1167b67a93e68febeabe9170e
6
+ metadata.gz: 691824db56620d1812c14db827f9dbffb2e6da89ba4c46f688ef8aa177afa8dc8db1e08a329559fc07e395d22adef18e30ead862e422a4a32001999767b3429f
7
+ data.tar.gz: e2331144a98cafd06a09e6019477265915638d328ac006868e3ea2922f513330af5241e28702d4aeb0b52a4e4cb077a77788954c924fcdcccde86bd18198f52d
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [3.7.0] - 2025-03-17
8
+
9
+ * Docs updates
10
+ * Configuration via ENV variables (#196, @skryukov)
11
+ * Routing improvements for the inertia routes helper (#195, @skryukov)
12
+ * When automatically determining the component path, the component name can now be omitted, instead of requiring `render inertia: true` (#199, @skryukov)
13
+
7
14
  ## [3.6.1] - 2025-02-04
8
15
 
9
16
  * Install generator tweaks @skryukov
@@ -29,6 +29,23 @@ module InertiaRails
29
29
 
30
30
  OPTION_NAMES = DEFAULTS.keys.freeze
31
31
 
32
+ class << self
33
+ def default
34
+ new(**DEFAULTS, **env_options)
35
+ end
36
+
37
+ private
38
+
39
+ def env_options
40
+ DEFAULTS.keys.each_with_object({}) do |key, hash|
41
+ value = ENV.fetch("INERTIA_#{key.to_s.upcase}", nil)
42
+ next if value.nil?
43
+
44
+ hash[key] = %w[true false].include?(value) ? value == 'true' : value
45
+ end
46
+ end
47
+ end
48
+
32
49
  protected attr_reader :controller
33
50
  protected attr_reader :options
34
51
 
@@ -36,9 +53,9 @@ module InertiaRails
36
53
  @controller = controller
37
54
  @options = attrs.extract!(*OPTION_NAMES)
38
55
 
39
- unless attrs.empty?
40
- raise ArgumentError, "Unknown options for #{self.class}: #{attrs.keys}"
41
- end
56
+ return if attrs.empty?
57
+
58
+ raise ArgumentError, "Unknown options for #{self.class}: #{attrs.keys}"
42
59
  end
43
60
 
44
61
  def bind_controller(controller)
@@ -56,7 +73,7 @@ module InertiaRails
56
73
  end
57
74
 
58
75
  def merge(config)
59
- Configuration.new(**@options.merge(config.options))
76
+ Configuration.new(**@options, **config.options)
60
77
  end
61
78
 
62
79
  # Internal: Finalizes the configuration for a specific controller.
@@ -70,23 +87,22 @@ module InertiaRails
70
87
  end
71
88
 
72
89
  OPTION_NAMES.each do |option|
73
- define_method(option) {
74
- evaluate_option @options[option]
75
- } unless method_defined?(option)
76
- define_method("#{option}=") { |value|
90
+ unless method_defined?(option)
91
+ define_method(option) do
92
+ evaluate_option options[option]
93
+ end
94
+ end
95
+ define_method("#{option}=") do |value|
77
96
  @options[option] = value
78
- }
79
- end
80
-
81
- def self.default
82
- new(**DEFAULTS)
97
+ end
83
98
  end
84
99
 
85
- private
100
+ private
86
101
 
87
102
  def evaluate_option(value)
88
103
  return value unless value.respond_to?(:call)
89
104
  return value.call unless controller
105
+
90
106
  controller.instance_exec(&value)
91
107
  end
92
108
  end
@@ -19,31 +19,41 @@ module InertiaRails
19
19
  :clear_history
20
20
  )
21
21
 
22
- def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil, encrypt_history: nil, clear_history: nil)
22
+ def initialize(component, controller, request, response, render_method, props: nil, view_data: nil,
23
+ deep_merge: nil, encrypt_history: nil, clear_history: nil)
24
+ if component.is_a?(Hash) && !props.nil?
25
+ raise ArgumentError,
26
+ 'Parameter `props` is not allowed when passing a Hash as the first argument'
27
+ end
28
+
23
29
  @controller = controller
24
30
  @configuration = controller.__send__(:inertia_configuration)
25
31
  @component = resolve_component(component)
26
32
  @request = request
27
33
  @response = response
28
34
  @render_method = render_method
29
- @props = props || controller.__send__(:inertia_view_assigns)
35
+ @props = props || (component.is_a?(Hash) ? component : controller.__send__(:inertia_view_assigns))
30
36
  @view_data = view_data || {}
31
- @deep_merge = !deep_merge.nil? ? deep_merge : configuration.deep_merge_shared_data
32
- @encrypt_history = !encrypt_history.nil? ? encrypt_history : configuration.encrypt_history
37
+ @deep_merge = deep_merge.nil? ? configuration.deep_merge_shared_data : deep_merge
38
+ @encrypt_history = encrypt_history.nil? ? configuration.encrypt_history : encrypt_history
33
39
  @clear_history = clear_history || controller.session[:inertia_clear_history] || false
34
40
  end
35
41
 
36
42
  def render
37
- if @response.headers["Vary"].blank?
38
- @response.headers["Vary"] = 'X-Inertia'
39
- else
40
- @response.headers["Vary"] = "#{@response.headers["Vary"]}, X-Inertia"
41
- end
43
+ @response.headers['Vary'] = if @response.headers['Vary'].blank?
44
+ 'X-Inertia'
45
+ else
46
+ "#{@response.headers['Vary']}, X-Inertia"
47
+ end
42
48
  if @request.headers['X-Inertia']
43
49
  @response.set_header('X-Inertia', 'true')
44
50
  @render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json]
45
51
  else
46
- return render_ssr if configuration.ssr_enabled rescue nil
52
+ begin
53
+ return render_ssr if configuration.ssr_enabled
54
+ rescue StandardError
55
+ nil
56
+ end
47
57
  @render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page)
48
58
  end
49
59
  end
@@ -54,13 +64,13 @@ module InertiaRails
54
64
  uri = URI("#{configuration.ssr_url}/render")
55
65
  res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
56
66
 
57
- controller.instance_variable_set("@_inertia_ssr_head", res['head'].join.html_safe)
67
+ controller.instance_variable_set('@_inertia_ssr_head', res['head'].join.html_safe)
58
68
  @render_method.call html: res['body'].html_safe, layout: layout, locals: view_data.merge(page: page)
59
69
  end
60
70
 
61
71
  def layout
62
72
  layout = configuration.layout
63
- layout.nil? ? true : layout
73
+ layout.nil? || layout
64
74
  end
65
75
 
66
76
  def shared_data
@@ -81,19 +91,19 @@ module InertiaRails
81
91
  end
82
92
 
83
93
  def computed_props
84
- _props = merge_props(shared_data, props)
94
+ merged_props = merge_props(shared_data, props)
85
95
 
86
- deep_transform_props _props do |prop, path|
96
+ deep_transform_props merged_props do |prop, path|
87
97
  next [DONT_KEEP_PROP] unless keep_prop?(prop, path)
88
98
 
89
99
  transformed_prop = case prop
90
- when BaseProp
91
- prop.call(controller)
92
- when Proc
93
- controller.instance_exec(&prop)
94
- else
95
- prop
96
- end
100
+ when BaseProp
101
+ prop.call(controller)
102
+ when Proc
103
+ controller.instance_exec(&prop)
104
+ else
105
+ prop
106
+ end
97
107
 
98
108
  [KEEP_PROP, transformed_prop]
99
109
  end
@@ -119,7 +129,7 @@ module InertiaRails
119
129
  end
120
130
 
121
131
  def deep_transform_props(props, parent_path = [], &block)
122
- props.reduce({}) do |transformed_props, (key, prop)|
132
+ props.each_with_object({}) do |(key, prop), transformed_props|
123
133
  current_path = parent_path + [key]
124
134
 
125
135
  if prop.is_a?(Hash) && prop.any?
@@ -129,8 +139,6 @@ module InertiaRails
129
139
  action, transformed_prop = block.call(prop, current_path)
130
140
  transformed_props.merge!(key => transformed_prop) if action == KEEP_PROP
131
141
  end
132
-
133
- transformed_props
134
142
  end
135
143
  end
136
144
 
@@ -165,9 +173,11 @@ module InertiaRails
165
173
  end
166
174
 
167
175
  def resolve_component(component)
168
- return component unless component.is_a? TrueClass
169
-
170
- configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name)
176
+ if component == true || component.is_a?(Hash)
177
+ configuration.component_path_resolver(path: controller.controller_path, action: controller.action_name)
178
+ else
179
+ component
180
+ end
171
181
  end
172
182
 
173
183
  def keep_prop?(prop, path)
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "3.6.1"
2
+ VERSION = "3.7.0"
3
3
  end
data/lib/inertia_rails.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'inertia_rails/renderer'
2
4
  require 'inertia_rails/engine'
3
5
 
@@ -17,7 +19,7 @@ ActionController::Renderers.add :inertia do |component, options|
17
19
  view_data: options[:view_data],
18
20
  deep_merge: options[:deep_merge],
19
21
  encrypt_history: options[:encrypt_history],
20
- clear_history: options[:clear_history],
22
+ clear_history: options[:clear_history]
21
23
  ).render
22
24
  end
23
25
 
@@ -1,10 +1,25 @@
1
1
  module InertiaRails
2
2
  module InertiaMapper
3
- def inertia(args, &block)
4
- route = args.keys.first
5
- component = args.values.first
3
+ def inertia(*args, **options)
4
+ path = args.any? ? args.first : options
5
+ route, component = extract_route_and_component(path)
6
+ scope module: nil do
7
+ get(route, to: 'inertia_rails/static#static', defaults: { component: component }, **options)
8
+ end
9
+ end
10
+
11
+ private
6
12
 
7
- get(route => 'inertia_rails/static#static', defaults: { component: component })
13
+ def extract_route_and_component(path)
14
+ if path.is_a?(Hash)
15
+ path.first
16
+ elsif resource_scope?
17
+ [path, InertiaRails.configuration.component_path_resolver(path: [@scope[:module], @scope[:controller]].compact.join('/'), action: path)]
18
+ elsif @scope[:module].blank?
19
+ [path, path]
20
+ else
21
+ [path, InertiaRails.configuration.component_path_resolver(path: @scope[:module], action: path)]
22
+ end
8
23
  end
9
24
  end
10
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.1
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
@@ -9,7 +9,7 @@ authors:
9
9
  - Eugene Granovsky
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-02-05 00:00:00.000000000 Z
12
+ date: 2025-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties