Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_page_renderer.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65bd09dbcaa7379c9af3195e63105fc4a7986936 (plain)
1
2
3
4
5
6
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
44
45
46
47
48
49

# frozen_string_literal: true

# Inspired by https://github.com/route/errgent/blob/master/lib/errgent/renderer.rb
class ErrorPageRenderer
  def initialize options={}
    @codes    = options.fetch :codes, [404, 500]
    @output   = options.fetch :output, "public/%s.html"
    @vars     = options.fetch :vars, {}
    @template = options.fetch :template, "errors/error_%s"
    @layout   = options.fetch :layout, "layouts/error_page"
  end

  def render
    @codes.each do |code|
      view = build_action_view
      view.assign @vars.merge(code: code)
      path = Rails.root.join(@output % code)
      File.write path, view.render(template: @template % code, layout: @layout)
    end
  end

  def helpers(&block)
    @helpers = block
  end

  private

  def build_action_view
    paths = ::ActionController::Base.view_paths
    ::ActionView::Base.new(paths).tap do |view|
      view.class_eval do
        include Rails.application.helpers
        include Rails.application.routes.url_helpers
      end
      view.assets_manifest = build_manifest(Rails.application)
      view.class_eval(&@helpers) if @helpers
    end
  end

  # Internal API from the sprocket-rails railtie, if somebody finds a way to
  # call it, please replace it. Might need to be updated on sprocket-rails
  # updates.
  def build_manifest(app)
    config = app.config
    path = File.join(config.paths['public'].first, config.assets.prefix)
    Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
  end
end