Class: InertiaRails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia_rails/configuration.rb

Constant Summary collapse

DEFAULTS =
{
  # Whether to combine hashes with the same keys instead of replacing them.
  deep_merge_shared_data: false,

  # Overrides Rails default rendering behavior to render using Inertia by default.
  default_render: false,

  # Allows the user to hook into the default rendering behavior and change it to fit their needs
  component_path_resolver: ->(path:, action:) { "#{path}/#{action}" },

  # A function that transforms the props before they are sent to the client.
  prop_transformer: ->(props:) { props },

  # DEPRECATED: Let Rails decide which layout should be used based on the
  # controller configuration.
  layout: true,

  # Whether to encrypt the history state in the client.
  encrypt_history: false,

  # SSR options.
  ssr_enabled: false,
  ssr_url: 'http://localhost:13714',

  # Used to detect version drift between server and client.
  version: nil,

  # Allows configuring the base controller for StaticController.
  parent_controller: '::ApplicationController',

  # Whether to include empty `errors` hash to the props when no errors are present.
  always_include_errors_hash: nil,

  # Whether to use `<script>` element for initial page rendering instead of the `data-page` attribute.
  use_script_element_for_initial_page: false,

  # DOM id to use for the root Inertia.js element.
  root_dom_id: 'app',

  # Flash keys from Rails flash to expose to frontend.
  # Set to nil to disable Rails flash integration (use only flash.inertia).
  flash_keys: %i[notice alert].freeze,
}.freeze
OPTION_NAMES =
DEFAULTS.keys.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller: nil, **attrs) ⇒ Configuration

Returns a new instance of Configuration.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
# File 'lib/inertia_rails/configuration.rb', line 71

def initialize(controller: nil, **attrs)
  @controller = controller
  @options = attrs.extract!(*OPTION_NAMES)

  return if attrs.empty?

  raise ArgumentError, "Unknown options for #{self.class}: #{attrs.keys}"
end

Class Method Details

.defaultObject



52
53
54
# File 'lib/inertia_rails/configuration.rb', line 52

def default
  new(**DEFAULTS, **env_options)
end

Instance Method Details

#bind_controller(controller) ⇒ Object



80
81
82
# File 'lib/inertia_rails/configuration.rb', line 80

def bind_controller(controller)
  Configuration.new(**@options, controller: controller)
end

#component_path_resolver(path:, action:) ⇒ Object



104
105
106
# File 'lib/inertia_rails/configuration.rb', line 104

def component_path_resolver(path:, action:)
  @options[:component_path_resolver].call(path: path, action: action)
end

#freezeObject



84
85
86
87
# File 'lib/inertia_rails/configuration.rb', line 84

def freeze
  @options.freeze
  super
end

#merge(config) ⇒ Object



94
95
96
# File 'lib/inertia_rails/configuration.rb', line 94

def merge(config)
  Configuration.new(**@options, **config.options)
end

#merge!(config) ⇒ Object



89
90
91
92
# File 'lib/inertia_rails/configuration.rb', line 89

def merge!(config)
  @options.merge!(config.options)
  self
end

#prop_transformer(props:) ⇒ Object



108
109
110
# File 'lib/inertia_rails/configuration.rb', line 108

def prop_transformer(props:)
  @options[:prop_transformer].call(props: props)
end

#with_defaults(config) ⇒ Object

Internal: Finalizes the configuration for a specific controller.



99
100
101
102
# File 'lib/inertia_rails/configuration.rb', line 99

def with_defaults(config)
  @options = config.options.merge(@options)
  freeze
end