Class: SolidObjects::ComponentRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/component_renderer.rb,
sig/generated/lib/solid_objects/component_renderer.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snapshot:, registration:, view_context:, authorization_context:) ⇒ ComponentRenderer

Returns a new instance of ComponentRenderer.

RBS:

  • (snapshot: ActorSnapshot, registration: ComponentRegistration, view_context: untyped, authorization_context: untyped) -> void

Parameters:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/solid_objects/component_renderer.rb', line 11

def initialize(
  snapshot:,
  registration:,
  view_context:,
  authorization_context:
)
  @snapshot = snapshot
  @registration = registration
  @view_context = view_context
  @authorization_context = authorization_context
end

Instance Attribute Details

#authorization_contextObject (readonly)

Returns the value of attribute authorization_context.

Returns:

  • (Object)


52
53
54
# File 'lib/solid_objects/component_renderer.rb', line 52

def authorization_context
  @authorization_context
end

#registrationObject (readonly)

Returns the value of attribute registration.

Returns:

  • (Object)


52
53
54
# File 'lib/solid_objects/component_renderer.rb', line 52

def registration
  @registration
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.

Returns:

  • (Object)


52
53
54
# File 'lib/solid_objects/component_renderer.rb', line 52

def snapshot
  @snapshot
end

#view_contextObject (readonly)

Returns the value of attribute view_context.

Returns:

  • (Object)


52
53
54
# File 'lib/solid_objects/component_renderer.rb', line 52

def view_context
  @view_context
end

Instance Method Details

#authorize_read!(observable_name) ⇒ void

This method returns an undefined value.

RBS:

  • (String) -> void

Parameters:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solid_objects/component_renderer.rb', line 58

def authorize_read!(observable_name)
  authorized = SolidObjects.configuration.authorize_query.call(
    actor_type: snapshot.reference.actor_type,
    actor_id: snapshot.reference.actor_id,
    message_name: observable_name,
    arguments: registration.authorization_arguments,
    authorization_context:
  )
  return if authorized

  raise Unauthorized, "actor component query is not authorized"
end

#callObject

RBS:

  • () -> untyped

Returns:

  • (Object)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solid_objects/component_renderer.rb', line 24

def call
  [ registration.component_name, *registration.dependencies ].uniq.each do |authorization_name|
    authorize_read!(authorization_name)
  end
  actor = ComponentView.new(
    snapshot:,
    dependencies: registration.dependencies,
    authorization_context:
  )
  view_context.render(
    partial: default_partial,
    locals: registration.locals.transform_keys(&:to_sym).merge(
      actor:,
      authorization_context:,
      component_key: registration.component_key
    )
  )
rescue ActionView::MissingTemplate
  raise UnknownComponent,
    "unknown component #{registration.component_name.inspect} for #{snapshot.reference.actor_type}"
rescue ActionView::Template::Error => error
  raise error.cause if error.cause.is_a?(SolidObjects::Error)

  raise
end

#default_partialString

RBS:

  • () -> String

Returns:

  • (String)


72
73
74
75
76
77
# File 'lib/solid_objects/component_renderer.rb', line 72

def default_partial
  actor_name = snapshot.actor_class.name
  raise InvalidActor, "anonymous actors cannot render reactive components" unless actor_name

  "actors/#{actor_name.underscore}/#{registration.component_name}"
end