Skip to content

Instantly share code, notes, and snippets.

@bradleypriest
Created September 21, 2011 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradleypriest/1231607 to your computer and use it in GitHub Desktop.
Save bradleypriest/1231607 to your computer and use it in GitHub Desktop.
Monkey-Patch rake assets:precompile to get Devise to work with Heroku
Rake::Task['assets:precompile'].clear
namespace :assets do
desc "Compile all the assets named in config.assets.precompile"
task :precompile do
# We need to do this dance because RAILS_GROUPS is used
# too early in the boot process and changing here is already too late.
if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty?
ENV["RAILS_GROUPS"] ||= "assets"
ENV["RAILS_ENV"] ||= "production"
Kernel.exec $0, *ARGV
else
# MonkeyPatch Devise
module ActionDispatch::Routing
class Mapper
def devise_for(*resources)
true
end
end
end
Rake::Task["environment"].invoke
# UnMonkeyPatch
module ActionDispatch::Routing
class Mapper
def devise_for(*resources)
@devise_finalized = false
options = resources.extract_options!
options[:as] ||= @scope[:as] if @scope[:as].present?
options[:module] ||= @scope[:module] if @scope[:module].present?
options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
@scope[:options] = (@scope[:options] || {}).merge({:format => false}) if options[:format] == false
resources.map!(&:to_sym)
resources.each do |resource|
mapping = Devise.add_mapping(resource, options)
begin
raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
rescue NameError => e
raise unless mapping.class_name == resource.to_s.classify
warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
"no model #{mapping.class_name} defined in your application"
next
rescue NoMethodError => e
raise unless e.message.include?("undefined method `devise'")
raise_no_devise_method_error!(mapping.class_name)
end
routes = mapping.used_routes
devise_scope mapping.name do
yield if block_given?
with_devise_exclusive_scope mapping.fullpath, mapping.name, mapping.constraints, mapping.defaults do
routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
end
end
end
end
end
end
Rake::Task["tmp:cache:clear"].invoke
# Ensure that action view is loaded and the appropriate sprockets hooks get executed
ActionView::Base
# Always compile files
Rails.application.config.assets.compile = true
config = Rails.application.config
env = Rails.application.assets
target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
manifest = {}
manifest_path = config.assets.manifest || target
config.assets.precompile.each do |path|
env.each_logical_path do |logical_path|
if path.is_a?(Regexp)
next unless path.match(logical_path)
elsif path.is_a?(Proc)
next unless path.call(logical_path)
else
next unless File.fnmatch(path.to_s, logical_path)
end
if asset = env.find_asset(logical_path)
asset_path = config.assets.digest ? asset.digest_path : logical_path
manifest[logical_path] = asset_path
filename = target.join(asset_path)
mkdir_p filename.dirname
asset.write_to(filename)
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
end
end
end
File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
YAML.dump(manifest, f)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment