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
end end
def initialize! def initialize!(config = nil)
config_file = Compass.detect_configuration_file(root) config ||= Compass.detect_configuration_file(root)
Compass.add_project_configuration(config_file) Compass.add_project_configuration(config, :project_type => :rails)
Compass.discover_extensions! Compass.discover_extensions!
Compass.configure_sass_plugin! Compass.configure_sass_plugin!
Compass.handle_configuration_change! Compass.handle_configuration_change!

View File

@ -1,18 +1,46 @@
require 'compass' require 'compass'
require 'rails' require 'rails'
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| class Rails::Railtie::Configuration
# XXX How do I only do this if it's not done yet? # Adds compass configuration accessor to the application configuration.
# require 'sass/plugin/rack' #
# app.middleware.use Sass::Plugin::Rack # 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
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 end
end end

View File

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

View File

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

View File

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