1namespace :errors do
 
2  desc "Precompile error pages into /public"
 
3  task :precompile => :environment do
 
4    require 'base64'
 
5
 
6    controller_class = Class.new(ActionController::Base) do
 
7      def url_options
 
8        Site.constraints_for_public
 
 9      end
 
10    end
 
 
12    context_class = Class.new(ActionView::Base) do
 
13      include Rails.application.routes.url_helpers
 
 
15      def data_uri(path)
 
16        "data:image/png;base64,#{Base64.strict_encode64(asset_data(path))}"
 
17      end
 
 
19      def asset_data(path)
 
20        File.read(Rails.root.join('app', 'assets', 'images', path))
 
21      end
 
 
23      def home_page?
 
24        false
 
25      end
 
26    end
 
 
28    %w[400 403 404 406 422 500 503].each do |status|
 
29      context = context_class.new('app/views', { status: status }, controller_class.new)
 
30      File.open(Rails.public_path.join("#{status}.html"), 'wb') do |f|
 
31        f.write context.render(template: "errors/#{status}", layout: 'errors/layout')
 
32      end
 
33    end
 
 
35    context = context_class.new('app/views', {}, controller_class.new)
 
36    File.open(Rails.public_path.join("error.css"), 'wb') do |f|
 
37      f.write context.render(template: "errors/error", layout: false)
 
38    end
 
39  end
 
40end
 
 
42task 'assets:precompile' => 'errors:precompile'