Make compass configuration accessible within a rails app's configuration.

This commit is contained in:
Chris Eppstein 2010-12-17 17:55:54 -08:00
parent 4abe386847
commit 8d3b2d92df
5 changed files with 64 additions and 27 deletions

View File

@ -34,9 +34,9 @@ module Compass
end
end
def initialize!
config_file = Compass.detect_configuration_file(root)
Compass.add_project_configuration(config_file)
def initialize!(config = nil)
config ||= Compass.detect_configuration_file(root)
Compass.add_project_configuration(config, :project_type => :rails)
Compass.discover_extensions!
Compass.configure_sass_plugin!
Compass.handle_configuration_change!

View File

@ -1,18 +1,46 @@
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("project")
end
data.project_type = :rails # Forcing this makes sure all the rails defaults will be loaded.
data
end
end
end
module Compass
class Railtie < Rails::Railtie
config.to_prepare do
# putting this here allows compass to detect
# and adjust to changes to the project configuration
Compass.reset_configuration!
Compass::AppIntegration::Rails.initialize!
end
initializer "compass/railtie.configure_rails_initialization" do |app|
# XXX How do I only do this if it's not done yet?
# require 'sass/plugin/rack'
# app.middleware.use Sass::Plugin::Rack
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)
end
end
end

View File

@ -8,9 +8,11 @@ module Compass
end
def default_sass_dir
# XXX Maybe this should be: app/views/stylesheets
# or maybe layouts should be moved up a level.
File.join("app", "stylesheets")
if Sass::Util.ap_geq?('3.1.0.beta')
File.join("app", "assets", "stylesheets")
else
File.join("app", "stylesheets")
end
end
def default_css_dir
@ -26,23 +28,27 @@ module Compass
end
def default_javascripts_dir
File.join("public", "javascripts")
if Sass::Util.ap_geq?('3.1.0.beta')
File.join("app", "assets", "javascripts")
else
File.join("public", "javascripts")
end
end
def default_http_images_path
"/images"
"#{top_level.http_path}images"
end
def default_http_javascripts_path
"/javascripts"
"#{top_level.http_path}javascripts"
end
def default_http_fonts_path
"/fonts"
"#{top_level.http_path}fonts"
end
def default_http_stylesheets_path
"/stylesheets"
"#{top_level.http_path}stylesheets"
end
def default_extensions_dir

View File

@ -28,7 +28,9 @@ module Compass
end
def configuration_for(config, filename = nil)
if config.is_a?(Compass::Configuration::Data)
if config.nil?
nil
elsif config.is_a?(Compass::Configuration::Data)
config
elsif config.respond_to?(:read)
filename ||= config.to_s if config.is_a?(Pathname)
@ -74,10 +76,7 @@ module Compass
options = args.last.is_a?(Hash) ? args.pop : {}
configuration_file_path = args.shift || detect_configuration_file
raise ArgumentError, "Too many arguments" if args.any?
if configuration_file_path && File.exists?(configuration_file_path)
data = configuration_for(configuration_file_path)
if data = configuration_for(configuration_file_path)
if data.raw_project_type
add_configuration(data.raw_project_type.to_sym)
elsif options[:project_type]

View File

@ -99,6 +99,10 @@ module Compass
@set_attributes[attribute]
end
def any_attributes_set?
@set_attributes && @set_attributes.size > 0
end
def default_for(attribute)
method = "default_#{attribute}".to_sym
if respond_to?(method)