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}" },

  # 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,
}.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.



35
36
37
38
39
40
41
42
# File 'lib/inertia_rails/configuration.rb', line 35

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

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

Class Method Details

.defaultObject



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

def self.default
  new(**DEFAULTS)
end

Instance Method Details

#bind_controller(controller) ⇒ Object



44
45
46
# File 'lib/inertia_rails/configuration.rb', line 44

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

#component_path_resolver(path:, action:) ⇒ Object



68
69
70
# File 'lib/inertia_rails/configuration.rb', line 68

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

#freezeObject



48
49
50
51
# File 'lib/inertia_rails/configuration.rb', line 48

def freeze
  @options.freeze
  super
end

#merge(config) ⇒ Object



58
59
60
# File 'lib/inertia_rails/configuration.rb', line 58

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

#merge!(config) ⇒ Object



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

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

#with_defaults(config) ⇒ Object

Internal: Finalizes the configuration for a specific controller.



63
64
65
66
# File 'lib/inertia_rails/configuration.rb', line 63

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