Basic Rails 3.1 integration.

This commit is contained in:
Chris Eppstein 2011-06-03 10:29:42 -07:00
parent 4852e7803c
commit 1b689ce966
12 changed files with 127 additions and 31 deletions

View File

@ -24,8 +24,10 @@ module Compass
end
def configuration
Compass::Configuration::Data.new('rails').
extend(ConfigurationDefaults)
config = Compass::Configuration::Data.new('rails')
config.extend(ConfigurationDefaults)
config.extend(ConfigurationDefaultsWithAssetPipeline) if Sass::Util.ap_geq?('3.1.0.beta')
config
end
def env
@ -48,12 +50,17 @@ module Compass
end
end
def initialize!(config = nil)
def check_for_double_boot!
if booted?
Compass::Util.compass_warn("Warning: Compass was booted twice. Compass has a Railtie now; please remove your intializer.")
Compass::Util.compass_warn("Warning: Compass was booted twice. If you're using Rails 3, Compass has a Railtie now; please remove your compass intializer.")
else
booted!
end
end
# Rails 2.x projects use this in their compass initializer.
def initialize!(config = nil)
check_for_double_boot!
config ||= Compass.detect_configuration_file(root)
Compass.add_project_configuration(config, :project_type => :rails)
Compass.discover_extensions!

View File

@ -0,0 +1,10 @@
%w(action_controller sass_plugin urls).each do |lib|
require "compass/app_integration/rails/actionpack2x/#{lib}"
end
# Wierd that this has to be re-included to pick up sub-modules. Ruby bug?
class Sass::Script::Functions::EvaluationContext
include Sass::Script::Functions
private
include ActionView::Helpers::AssetTagHelper
end

View File

@ -0,0 +1,11 @@
# TODO figure something out so image_path works with rails integration
%w(railtie).each do |lib|
require "compass/app_integration/rails/actionpack30/#{lib}"
end
# Wierd that this has to be re-included to pick up sub-modules. Ruby bug?
class Sass::Script::Functions::EvaluationContext
include Sass::Script::Functions
private
include ActionView::Helpers::AssetTagHelper
end

View File

@ -40,7 +40,10 @@ module Compass
initializer "compass.initialize_rails" do |app|
# Configure compass for use within rails, and provide the project configuration
# that came via the rails boot process.
Compass::AppIntegration::Rails.initialize!(app.config.compass)
Compass::AppIntegration::Rails.check_for_double_boot!
Compass.discover_extensions!
Compass.configure_sass_plugin!
Compass.handle_configuration_change!
end
end
end

View File

@ -0,0 +1,5 @@
# TODO figure something out so image_path works with rails integration
%w(railtie).each do |lib|
require "compass/app_integration/rails/actionpack31/#{lib}"
end

View File

@ -0,0 +1,50 @@
require 'compass'
require 'rails'
class Rails::Railtie::Configuration
# Adds compass configuration accessor to the application configuration.
#
# If a configuration file for compass exists, it will be read in and
# the project's configuration values will already be set on the config
# object.
#
# For example:
#
# module MyApp
# class Application < Rails::Application
# config.compass.line_comments = !Rails.env.production?
# config.compass.fonts_dir = "app/assets/fonts"
# end
# end
#
# It is suggested that you create a compass configuration file if you
# want a quicker boot time when using the compass command line tool.
#
# For more information on available configuration options see:
# http://compass-style.org/help/tutorials/configuration-reference/
def compass
@compass ||= begin
data = if (config_file = Compass.detect_configuration_file) && (config_data = Compass.configuration_for(config_file))
config_data
else
Compass::Configuration::Data.new("rails_config")
end
data.project_type = :rails # Forcing this makes sure all the rails defaults will be loaded.
Compass.add_project_configuration(data)
data
end
@compass
end
end
module Compass
class Railtie < Rails::Railtie
initializer "compass.initialize_rails" do |app|
# Configure compass for use within rails, and provide the project configuration
# that came via the rails boot process.
Compass::AppIntegration::Rails.check_for_double_boot!
Compass.discover_extensions!
Compass.configure_rails!(app)
end
end
end

View File

@ -1,6 +1,28 @@
module Compass
module AppIntegration
module Rails
module ConfigurationDefaultsWithAssetPipeline
# These methods overwrite the old rails defaults
# when rails 3.1 is detected.
def default_sass_dir
File.join("app", "assets", "stylesheets")
end
def default_images_dir
File.join("app", "assets", "images")
end
def default_fonts_dir
File.join("app", "assets", "fonts")
end
def default_javascripts_dir
File.join("app", "assets", "javascripts")
end
end
module ConfigurationDefaults
def project_type_without_default
@ -8,11 +30,7 @@ module Compass
end
def default_sass_dir
if Sass::Util.ap_geq?('3.1.0.beta')
File.join("app", "assets", "stylesheets")
else
File.join("app", "stylesheets")
end
File.join("app", "stylesheets")
end
def default_css_dir
@ -28,11 +46,7 @@ module Compass
end
def default_javascripts_dir
if Sass::Util.ap_geq?('3.1.0.beta')
File.join("app", "assets", "javascripts")
else
File.join("public", "javascripts")
end
File.join("public", "javascripts")
end
def default_http_images_path

View File

@ -3,25 +3,15 @@ unless defined?(Compass::RAILS_LOADED)
begin
require 'action_pack/version'
if ActionPack::VERSION::MAJOR >= 3
# TODO figure something out so image_path works with rails integration
%w(railtie).each do |lib|
require "compass/app_integration/rails/actionpack3/#{lib}"
if ActionPack::VERSION::MINOR < 1
require 'compass/app_integration/rails/actionpack30'
else
require 'compass/app_integration/rails/actionpack31'
end
else
%w(action_controller sass_plugin urls).each do |lib|
require "compass/app_integration/rails/actionpack2/#{lib}"
end
require 'compass/app_integration/rails/actionpack2x'
end
rescue LoadError => e
rescue LoadError, NameError
$stderr.puts "Compass could not access the rails environment."
rescue NameError => e
$stderr.puts "Compass could not access the rails environment."
end
# Wierd that this has to be re-included to pick up sub-modules. Ruby bug?
class Sass::Script::Functions::EvaluationContext
include Sass::Script::Functions
private
include ActionView::Helpers::AssetTagHelper
end
end

View File

@ -77,6 +77,12 @@ module Compass
end
end
def configure_rails!(app)
app.config.compass.to_sass_engine_options.each do |key, value|
app.config.sass.send(:"#{key}=", value)
end
end
def sass_engine_options
configuration.to_sass_engine_options
end