Module: Sinatra::Inertia

Defined in:
lib/sinatra/inertia.rb,
lib/sinatra/inertia/errors.rb,
lib/sinatra/inertia/helpers.rb,
lib/sinatra/inertia/version.rb,
lib/sinatra/inertia/deferred.rb,
lib/sinatra/inertia/response.rb,
lib/sinatra/inertia/middleware.rb,
lib/sinatra/inertia/csrf_middleware.rb

Overview

Inertia.js v2 protocol adapter for Sinatra.

class App < Sinatra::Base
  register Sinatra::Inertia

  set :page_version, -> { ASSETS_VERSION }
  set :page_layout,  :layout

  share_props do
    { auth: { user: current_user }, flash: flash_payload }
  end

  get '/' do
    render 'Todos/Index', todos: -> { Todo.all }
  end
end

See README.md for the full feature matrix.

Defined Under Namespace

Modules: Helpers Classes: AlwaysProp, CSRFMiddleware, DeferredProp, LazyProp, MergeProp, Middleware, OptionalProp, Prop, Response

Constant Summary collapse

Error =
Class.new(StandardError)
InvalidProtocol =
Class.new(Error)
VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.always(value = nil, &block) ⇒ Object



72
# File 'lib/sinatra/inertia/deferred.rb', line 72

def always(value = nil, &block) = AlwaysProp.new(block: block, value: value)

.defer(group: "default", &block) ⇒ Object



73
# File 'lib/sinatra/inertia/deferred.rb', line 73

def defer(group: "default", &block) = DeferredProp.new(block: block, group: group)

.lazy(&block) ⇒ Object



75
# File 'lib/sinatra/inertia/deferred.rb', line 75

def lazy(&block) = LazyProp.new(block: block)

.merge(value = nil, &block) ⇒ Object



76
# File 'lib/sinatra/inertia/deferred.rb', line 76

def merge(value = nil, &block) = MergeProp.new(block: block, value: value)

.optional(&block) ⇒ Object



74
# File 'lib/sinatra/inertia/deferred.rb', line 74

def optional(&block) = OptionalProp.new(block: block)

.registered(app) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sinatra/inertia.rb', line 34

def self.registered(app)
  # Default settings. `page_*` is the recommended application-facing API;
  # `inertia_*` remains supported for existing apps and lower-level tuning.
  app.set(:inertia_version, "1") unless app.respond_to?(:inertia_version)
  app.set(:inertia_layout, :layout) unless app.respond_to?(:inertia_layout)
  unless app.respond_to?(:inertia_encrypt_history)
    app.set(:inertia_encrypt_history, false)
  end

  unless app.respond_to?(:inertia_csrf_protection)
    app.set(:inertia_csrf_protection, true)
  end

  app.set(:inertia_share_blocks, [])

  # Mount CSRF middleware (double-submit XSRF-TOKEN cookie that
  # @inertiajs/* clients honour). Opt-out via
  # `set :inertia_csrf_protection, false` when the consumer ships
  # its own (e.g. Rack::Protection::AuthenticityToken).
  if app.settings.inertia_csrf_protection
    app.use(Sinatra::Inertia::CSRFMiddleware)
  end

  # Mount protocol middleware (version mismatch + 303 redirect promotion).
  app.use(
    Sinatra::Inertia::Middleware,
    version: lambda {
      version = if app.settings.respond_to?(:page_version)
        app.settings.page_version
      else
        app.settings.inertia_version
      end

      version.respond_to?(:call) ? version.call.to_s : version.to_s
    }
  )

  # Class-level DSL: `share_props { ... }` registers a block whose return
  # value is merged into every page's props.
  app.define_singleton_method(:inertia_share) do |&block|
    raise ArgumentError, "inertia_share requires a block" unless block
    settings.inertia_share_blocks = settings.inertia_share_blocks + [block]
  end

  app.define_singleton_method(:share_props) do |&block|
    inertia_share(&block)
  end

  app.helpers(Sinatra::Inertia::Helpers)
end