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:, component_name:, dependencies:, view_context:, authorization_context:) ⇒ ComponentRenderer

Returns a new instance of ComponentRenderer.

RBS:

  • (snapshot: ActorSnapshot, component_name: String, dependencies: Array[String], view_context: untyped, authorization_context: untyped) -> void

Parameters:

  • snapshot: (ActorSnapshot)
  • component_name: (String)
  • dependencies: (Array[String])
  • view_context: (Object)
  • authorization_context: (Object)


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

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

Instance Attribute Details

#authorization_contextObject (readonly)

Returns the value of attribute authorization_context.

Returns:

  • (Object)


54
55
56
# File 'lib/solid_objects/component_renderer.rb', line 54

def authorization_context
  @authorization_context
end

#component_nameObject (readonly)

Returns the value of attribute component_name.

Returns:

  • (Object)


54
55
56
# File 'lib/solid_objects/component_renderer.rb', line 54

def component_name
  @component_name
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.

Returns:

  • (Object)


54
55
56
# File 'lib/solid_objects/component_renderer.rb', line 54

def dependencies
  @dependencies
end

#snapshotObject (readonly)

Returns the value of attribute snapshot.

Returns:

  • (Object)


54
55
56
# File 'lib/solid_objects/component_renderer.rb', line 54

def snapshot
  @snapshot
end

#view_contextObject (readonly)

Returns the value of attribute view_context.

Returns:

  • (Object)


54
55
56
# File 'lib/solid_objects/component_renderer.rb', line 54

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)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/solid_objects/component_renderer.rb', line 61

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: {},
    authorization_context:
  )
  return if authorized

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

#callObject

RBS:

  • () -> untyped

Returns:

  • (Object)


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

def call
  [ component_name, *dependencies ].uniq.each do |authorization_name|
    authorize_read!(authorization_name)
  end
  actor = ComponentView.new(
    snapshot:,
    dependencies:,
    authorization_context:
  )
  view_context.render(
    partial: default_partial,
    locals: {
      actor:,
      authorization_context:
    }
  )
rescue ActionView::MissingTemplate
  raise UnknownComponent,
    "unknown component #{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)


75
76
77
78
79
80
# File 'lib/solid_objects/component_renderer.rb', line 75

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

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