Class: SolidObjects::ComponentsController

Inherits:
ActionController::Base show all
Defined in:
app/controllers/solid_objects/components_controller.rb,
sig/generated/controllers/solid_objects/components_controller.rbs

Instance Method Summary collapse

Instance Method Details

#component_frame(registration, snapshot, rendered) ⇒ String

RBS:

  • (ComponentRegistration, ActorSnapshot, untyped) -> String

Parameters:

Returns:

  • (String)


69
70
71
72
73
74
75
76
# File 'app/controllers/solid_objects/components_controller.rb', line 69

def component_frame(registration, snapshot, rendered)
  target = DomIdentity.component(
    registration.reference,
    registration.component_name
  )
  revision = "#{snapshot.instance_id}:#{snapshot.revision}"
  %(<turbo-frame id="#{target}" data-solid-objects-revision="#{revision}">#{rendered}</turbo-frame>).html_safe
end

#component_view_contextObject

RBS:

  • () -> untyped

Returns:

  • (Object)


58
59
60
61
62
63
64
65
66
# File 'app/controllers/solid_objects/components_controller.rb', line 58

def component_view_context
  if defined?(Rails) && Rails.application
    prepend_view_path(*Rails.application.paths["app/views"].existent)
  end

  view_context.tap do |context|
    context.extend(Rails.application.helpers) if defined?(Rails) && Rails.application
  end
end

#newer_than_snapshot?(requested_revision, snapshot) ⇒ Boolean

RBS:

  • (Array[Integer], ActorSnapshot) -> bool

Parameters:

Returns:

  • (Boolean)


53
54
55
# File 'app/controllers/solid_objects/components_controller.rb', line 53

def newer_than_snapshot?(requested_revision, snapshot)
  (requested_revision <=> [ snapshot.instance_id, snapshot.revision ]) == 1
end

#requested_revision_keyArray[Integer]

RBS:

  • () -> Array[Integer]

Returns:

  • (Array[Integer])


44
45
46
47
48
49
50
# File 'app/controllers/solid_objects/components_controller.rb', line 44

def requested_revision_key
  instance_id = Integer(params.fetch(:instance_id), 10)
  revision = Integer(params.fetch(:revision), 10)
  raise ArgumentError if instance_id.negative? || revision.negative?

  [ instance_id, revision ]
end

#showvoid

This method returns an undefined value.

RBS:

  • () -> void



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/solid_objects/components_controller.rb', line 10

def show
  registration = ComponentRegistration.from_token(
    params.require(:token)
  )
  requested_revision = requested_revision_key
  snapshot = ActorSnapshot.new(registration.reference)
  return head :conflict if newer_than_snapshot?(requested_revision, snapshot)

  authorization_context = SolidObjects
    .configuration
    .component_authorization_context
    .call(controller: self)
  rendered = ComponentRenderer.new(
    snapshot:,
    component_name: registration.component_name,
    dependencies: registration.dependencies,
    view_context: component_view_context,
    authorization_context:
  ).call
  response.headers["Cache-Control"] = "private, no-store"
  render html: component_frame(registration, snapshot, rendered)
rescue Unauthorized
  head :forbidden
rescue UnknownComponent
  head :not_found
rescue ActionController::ParameterMissing,
  ArgumentError,
  InvalidComponentToken
  head :bad_request
end