Skip to content

Instantly share code, notes, and snippets.

@trvsdnn
Created December 17, 2013 00:43
Show Gist options
  • Save trvsdnn/7997972 to your computer and use it in GitHub Desktop.
Save trvsdnn/7997972 to your computer and use it in GitHub Desktop.
Rails static pages generation/compilation with no extra routing or controllers.
namespace :static do
desc "Compile static pages and save them in /public"
task compile: :environment do
class StaticController < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::AssetPaths
include Rails.application.routes.url_helpers
helper ApplicationHelper
def protect_against_forgery?; false; end
def logged_in?; false; end
helper_method :protect_against_forgery?, :logged_in?
self.view_paths = Rails.application.config.paths['app/views']
def show(template)
render template: "static/#{template}", layout: 'application'
end
end
Dir["#{Rails.root}/app/views/static/*.html.erb"].each do |path|
template_name = File.basename(path, '.html.erb')
static_path = Rails.public_path.join("#{template_name}.html")
File.open(static_path, 'w') do |f|
f.write(StaticController.new.show(template_name))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment