From 8d3b2d92dfa98f948e3259647982d766234eb448 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 17 Dec 2010 17:55:54 -0800 Subject: [PATCH] Make compass configuration accessible within a rails app's configuration. --- lib/compass/app_integration/rails.rb | 6 +-- .../rails/actionpack3/railtie.rb | 50 +++++++++++++++---- .../rails/configuration_defaults.rb | 22 +++++--- lib/compass/configuration/helpers.rb | 9 ++-- lib/compass/configuration/inheritance.rb | 4 ++ 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/lib/compass/app_integration/rails.rb b/lib/compass/app_integration/rails.rb index 87ba573f..cbd48b99 100644 --- a/lib/compass/app_integration/rails.rb +++ b/lib/compass/app_integration/rails.rb @@ -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! diff --git a/lib/compass/app_integration/rails/actionpack3/railtie.rb b/lib/compass/app_integration/rails/actionpack3/railtie.rb index 5fc3a2bf..a3fc5498 100644 --- a/lib/compass/app_integration/rails/actionpack3/railtie.rb +++ b/lib/compass/app_integration/rails/actionpack3/railtie.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/app_integration/rails/configuration_defaults.rb b/lib/compass/app_integration/rails/configuration_defaults.rb index 076444b9..fbab17e2 100644 --- a/lib/compass/app_integration/rails/configuration_defaults.rb +++ b/lib/compass/app_integration/rails/configuration_defaults.rb @@ -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 diff --git a/lib/compass/configuration/helpers.rb b/lib/compass/configuration/helpers.rb index 00f31b75..0dd06de5 100644 --- a/lib/compass/configuration/helpers.rb +++ b/lib/compass/configuration/helpers.rb @@ -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] diff --git a/lib/compass/configuration/inheritance.rb b/lib/compass/configuration/inheritance.rb index 18884d3d..e792923b 100644 --- a/lib/compass/configuration/inheritance.rb +++ b/lib/compass/configuration/inheritance.rb @@ -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)