From 998168160b11c8702ded0a32820ea15b70d51e83 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 23 May 2009 08:52:23 -0700 Subject: [PATCH] [Rails] image_url() now integrates with the rails asset handling code when stylesheets are generated within the rails container. This causes your rails configuration for cache busting and asset hosts to be picked up when generating your stylesheets. Unfortunately, all that code runs within the context of a controller, so the stylesheets have to be generated during first request to use this functionality. --- compass.gemspec | 33 +++++++++++++++---- lib/compass/app_integration.rb | 2 ++ lib/compass/app_integration/rails.rb | 12 +++++++ .../rails/action_controller.rb | 15 +++++++++ .../app_integration/rails/image_url.rb | 14 ++++++++ .../app_integration/rails/sass_plugin.rb | 5 +++ test/test_rails_helper.rb | 20 +++++++++++ 7 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 lib/compass/app_integration/rails.rb create mode 100644 lib/compass/app_integration/rails/action_controller.rb create mode 100644 lib/compass/app_integration/rails/image_url.rb create mode 100644 lib/compass/app_integration/rails/sass_plugin.rb create mode 100644 test/test_rails_helper.rb diff --git a/compass.gemspec b/compass.gemspec index 6f9023bf76..512abbe374 100644 --- a/compass.gemspec +++ b/compass.gemspec @@ -2,11 +2,11 @@ Gem::Specification.new do |s| s.name = %q{compass} - s.version = "0.6.15" + s.version = "0.6.16" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Eppstein"] - s.date = %q{2009-06-05} + s.date = %q{2009-06-11} s.default_executable = %q{compass} s.description = %q{Compass is a Sass-based Stylesheet Framework that streamlines the creation and maintainance of CSS.} s.email = %q{chris@eppsteins.net} @@ -30,6 +30,7 @@ Gem::Specification.new do |s| "examples/blueprint_default/parts/test.jpg", "examples/blueprint_default/parts/valid.png", "examples/blueprint_default/src/ie.sass", + "examples/blueprint_default/src/images/grid.png", "examples/blueprint_default/src/print.sass", "examples/blueprint_default/src/screen.sass", "examples/blueprint_plugins/config.rb", @@ -55,6 +56,7 @@ Gem::Specification.new do |s| "examples/blueprint_plugins/plugins/rtl.html.haml", "examples/blueprint_plugins/src/buttons.sass", "examples/blueprint_plugins/src/ie.sass", + "examples/blueprint_plugins/src/images/grid.png", "examples/blueprint_plugins/src/link_icons.sass", "examples/blueprint_plugins/src/print.sass", "examples/blueprint_plugins/src/rtl_screen.sass", @@ -74,6 +76,7 @@ Gem::Specification.new do |s| "examples/blueprint_semantic/parts/test.jpg", "examples/blueprint_semantic/parts/valid.png", "examples/blueprint_semantic/src/ie.sass", + "examples/blueprint_semantic/src/images/grid.png", "examples/blueprint_semantic/src/liquid.sass", "examples/blueprint_semantic/src/print.sass", "examples/blueprint_semantic/src/screen.sass", @@ -81,6 +84,7 @@ Gem::Specification.new do |s| "examples/compass/config.rb", "examples/compass/images/blue_arrow.gif", "examples/compass/src/compass.sass", + "examples/compass/src/images/blue_arrow.gif", "examples/compass/src/sticky_footer.sass", "examples/compass/src/utilities.sass", "examples/compass/sticky_footer.html.haml", @@ -179,6 +183,12 @@ Gem::Specification.new do |s| "frameworks/yui/templates/project/screen.sass", "lib/compass.rb", "lib/compass/actions.rb", + "lib/compass/app_integration.rb", + "lib/compass/app_integration/merb.rb", + "lib/compass/app_integration/rails.rb", + "lib/compass/app_integration/rails/action_controller.rb", + "lib/compass/app_integration/rails/image_url.rb", + "lib/compass/app_integration/rails/sass_plugin.rb", "lib/compass/commands/base.rb", "lib/compass/commands/create_project.rb", "lib/compass/commands/generate_grid_background.rb", @@ -205,16 +215,22 @@ Gem::Specification.new do |s| "lib/compass/installers/rails.rb", "lib/compass/installers/stand_alone.rb", "lib/compass/logger.rb", - "lib/compass/merb.rb", + "lib/compass/sass_extensions.rb", + "lib/compass/sass_extensions/functions.rb", + "lib/compass/sass_extensions/functions/enumerate.rb", + "lib/compass/sass_extensions/functions/image_url.rb", + "lib/compass/sass_extensions/functions/nest.rb", + "lib/compass/sass_extensions/monkey_patches.rb", + "lib/compass/sass_extensions/monkey_patches/stylesheet_updating.rb", "lib/compass/test_case.rb", "lib/compass/validator.rb", "lib/compass/version.rb", - "lib/sass_extensions.rb", "test/command_line_test.rb", "test/compass_test.rb", "test/configuration_test.rb", "test/fixtures/stylesheets/blueprint/config.rb", "test/fixtures/stylesheets/blueprint/css/typography.css", + "test/fixtures/stylesheets/blueprint/images/grid.png", "test/fixtures/stylesheets/blueprint/sass/ie.sass", "test/fixtures/stylesheets/blueprint/sass/print.sass", "test/fixtures/stylesheets/blueprint/sass/screen.sass", @@ -235,7 +251,9 @@ Gem::Specification.new do |s| "test/fixtures/stylesheets/yui/sass/grids.sass", "test/fixtures/stylesheets/yui/sass/mixins.sass", "test/sass_extensions_test.rb", - "test/test_helper.rb" + "test/test_case_helper.rb", + "test/test_helper.rb", + "test/test_rails_helper.rb" ] s.homepage = %q{http://compass-style.org} s.rdoc_options = ["--charset=UTF-8"] @@ -249,6 +267,7 @@ Gem::Specification.new do |s| "test/configuration_test.rb", "test/fixtures/stylesheets/blueprint/config.rb", "test/fixtures/stylesheets/blueprint/css/typography.css", + "test/fixtures/stylesheets/blueprint/images/grid.png", "test/fixtures/stylesheets/blueprint/sass/ie.sass", "test/fixtures/stylesheets/blueprint/sass/print.sass", "test/fixtures/stylesheets/blueprint/sass/screen.sass", @@ -269,7 +288,9 @@ Gem::Specification.new do |s| "test/fixtures/stylesheets/yui/sass/grids.sass", "test/fixtures/stylesheets/yui/sass/mixins.sass", "test/sass_extensions_test.rb", - "test/test_helper.rb" + "test/test_case_helper.rb", + "test/test_helper.rb", + "test/test_rails_helper.rb" ] if s.respond_to? :specification_version then diff --git a/lib/compass/app_integration.rb b/lib/compass/app_integration.rb index c6b00f7b8c..58e5591324 100644 --- a/lib/compass/app_integration.rb +++ b/lib/compass/app_integration.rb @@ -1,3 +1,5 @@ +# If we're running inside Rails +require File.join(File.dirname(__FILE__), 'app_integration', 'rails') if defined?(ActionController::Base) # If we're running inside Merb require File.join(File.dirname(__FILE__), 'app_integration', 'merb') if defined?(Merb::Plugins) diff --git a/lib/compass/app_integration/rails.rb b/lib/compass/app_integration/rails.rb new file mode 100644 index 0000000000..c29cbb2433 --- /dev/null +++ b/lib/compass/app_integration/rails.rb @@ -0,0 +1,12 @@ +unless defined?(Compass::RAILS_LOADED) + Compass::RAILS_LOADED = true + require File.join(File.dirname(__FILE__), 'rails', 'action_controller') + require File.join(File.dirname(__FILE__), 'rails', 'sass_plugin') + require File.join(File.dirname(__FILE__), 'rails', 'image_url') + # 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/app_integration/rails/action_controller.rb b/lib/compass/app_integration/rails/action_controller.rb new file mode 100644 index 0000000000..d3a4b9e933 --- /dev/null +++ b/lib/compass/app_integration/rails/action_controller.rb @@ -0,0 +1,15 @@ +# :stopdoc: +module ActionController + class Base + def process_with_compass(*args) + Sass::Plugin.rails_controller = self + begin + process_without_compass(*args) + ensure + Sass::Plugin.rails_controller = nil + end + end + alias_method_chain :process, :compass + end +end +# :startdoc: \ No newline at end of file diff --git a/lib/compass/app_integration/rails/image_url.rb b/lib/compass/app_integration/rails/image_url.rb new file mode 100644 index 0000000000..f59f8056d3 --- /dev/null +++ b/lib/compass/app_integration/rails/image_url.rb @@ -0,0 +1,14 @@ +module Compass::SassExtensions::Functions::ImageUrl + def image_url_with_rails_integration(path) + if @controller = Sass::Plugin.rails_controller + begin + Sass::Script::String.new "url(#{image_path(path.value)})" + ensure + @controller = nil + end + else + image_url_without_rails_integration(path) + end + end + alias_method_chain :image_url, :rails_integration +end \ No newline at end of file diff --git a/lib/compass/app_integration/rails/sass_plugin.rb b/lib/compass/app_integration/rails/sass_plugin.rb new file mode 100644 index 0000000000..8e0cba78ca --- /dev/null +++ b/lib/compass/app_integration/rails/sass_plugin.rb @@ -0,0 +1,5 @@ +module Sass::Plugin + class << self + attr_accessor :rails_controller + end +end \ No newline at end of file diff --git a/test/test_rails_helper.rb b/test/test_rails_helper.rb new file mode 100644 index 0000000000..6cbb80f2d8 --- /dev/null +++ b/test/test_rails_helper.rb @@ -0,0 +1,20 @@ +# XXX This file isn't in use at the moment, but will be used to help test +# XXX deep rails integration of compass features. +need_gems = false + +# allows testing with edge Rails by creating a test/rails symlink +RAILS_ROOT = linked_rails = File.dirname(__FILE__) + '/rails' +RAILS_ENV = 'test' + +if File.exists?(linked_rails) && !$:.include?(linked_rails + '/activesupport/lib') + puts "[ using linked Rails ]" + $:.unshift linked_rails + '/activesupport/lib' + $:.unshift linked_rails + '/actionpack/lib' +else + need_gems = true +end + +require 'rubygems' if need_gems + +require 'action_controller' +require 'action_view'