Module: Authtown::ViewMixin

Defined in:
lib/authtown/view_mixin.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



45
# File 'lib/authtown/view_mixin.rb', line 45

def current_user = Authtown::Current.user

#localsObject



4
# File 'lib/authtown/view_mixin.rb', line 4

def locals = @_route_locals

#view(*args, view_class: Bridgetown::ERBView, **kwargs) ⇒ Object

TODO: this is super hacky



7
8
9
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
40
41
42
43
# File 'lib/authtown/view_mixin.rb', line 7

def view(*args, view_class: Bridgetown::ERBView, **kwargs) # rubocop:disable Metrics
  kwargs = args.first if args.first.is_a?(Hash)

  # If we're farming out to another view, let's go!
  unless kwargs.empty?
    response["X-Bridgetown-SSR"] = "1"

    # UGH, hate special casing this
    if kwargs.dig(:locals, :rodauth)&.prefix
      kwargs[:template] =
        "#{kwargs.dig(:locals, :rodauth).prefix.delete_prefix("/")}/#{kwargs[:template]}"
    end

    Bridgetown::Routes::Manifest.generate_manifest(bridgetown_site).each do |route|
      file, localized_file_slugs = route

      file_slug = localized_file_slugs.first

      next unless file_slug == kwargs[:template]

      Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, self
      route_block = Bridgetown::Routes::CodeBlocks.route_block(file_slug)
      response.instance_variable_set(
        :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
      ) # could be nil
      @_route_locals = kwargs[:locals]
      return instance_exec(request, &route_block)
    end

    Bridgetown.logger.warn("Template not found: #{kwargs[:template]}")
    return nil # couldn't find template, 404
  end

  response._fake_resource_view(
    view_class:, roda_app: self, bridgetown_site:
  )
end