diff --git a/lib/compass/app_integration/rails.rb b/lib/compass/app_integration/rails.rb index 06bc7764..b734d1af 100644 --- a/lib/compass/app_integration/rails.rb +++ b/lib/compass/app_integration/rails.rb @@ -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! diff --git a/lib/compass/app_integration/rails/actionpack2x.rb b/lib/compass/app_integration/rails/actionpack2x.rb new file mode 100644 index 00000000..f48299bb --- /dev/null +++ b/lib/compass/app_integration/rails/actionpack2x.rb @@ -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 diff --git a/lib/compass/app_integration/rails/actionpack2/action_controller.rb b/lib/compass/app_integration/rails/actionpack2x/action_controller.rb similarity index 100% rename from lib/compass/app_integration/rails/actionpack2/action_controller.rb rename to lib/compass/app_integration/rails/actionpack2x/action_controller.rb diff --git a/lib/compass/app_integration/rails/actionpack2/sass_plugin.rb b/lib/compass/app_integration/rails/actionpack2x/sass_plugin.rb similarity index 100% rename from lib/compass/app_integration/rails/actionpack2/sass_plugin.rb rename to lib/compass/app_integration/rails/actionpack2x/sass_plugin.rb diff --git a/lib/compass/app_integration/rails/actionpack2/urls.rb b/lib/compass/app_integration/rails/actionpack2x/urls.rb similarity index 100% rename from lib/compass/app_integration/rails/actionpack2/urls.rb rename to lib/compass/app_integration/rails/actionpack2x/urls.rb diff --git a/lib/compass/app_integration/rails/actionpack30.rb b/lib/compass/app_integration/rails/actionpack30.rb new file mode 100644 index 00000000..05e24df8 --- /dev/null +++ b/lib/compass/app_integration/rails/actionpack30.rb @@ -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 diff --git a/lib/compass/app_integration/rails/actionpack3/railtie.rb b/lib/compass/app_integration/rails/actionpack30/railtie.rb similarity index 89% rename from lib/compass/app_integration/rails/actionpack3/railtie.rb rename to lib/compass/app_integration/rails/actionpack30/railtie.rb index a3fc5498..2e52ae3b 100644 --- a/lib/compass/app_integration/rails/actionpack3/railtie.rb +++ b/lib/compass/app_integration/rails/actionpack30/railtie.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/app_integration/rails/actionpack31.rb b/lib/compass/app_integration/rails/actionpack31.rb new file mode 100644 index 00000000..bbd2d531 --- /dev/null +++ b/lib/compass/app_integration/rails/actionpack31.rb @@ -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 + diff --git a/lib/compass/app_integration/rails/actionpack31/railtie.rb b/lib/compass/app_integration/rails/actionpack31/railtie.rb new file mode 100644 index 00000000..6604e836 --- /dev/null +++ b/lib/compass/app_integration/rails/actionpack31/railtie.rb @@ -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 \ 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 fbab17e2..06aaf311 100644 --- a/lib/compass/app_integration/rails/configuration_defaults.rb +++ b/lib/compass/app_integration/rails/configuration_defaults.rb @@ -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 diff --git a/lib/compass/app_integration/rails/runtime.rb b/lib/compass/app_integration/rails/runtime.rb index d2765ef0..815fd29f 100644 --- a/lib/compass/app_integration/rails/runtime.rb +++ b/lib/compass/app_integration/rails/runtime.rb @@ -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 diff --git a/lib/compass/configuration/helpers.rb b/lib/compass/configuration/helpers.rb index db6f8af6..9017f735 100644 --- a/lib/compass/configuration/helpers.rb +++ b/lib/compass/configuration/helpers.rb @@ -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