Permalink
Browse files

Add support to active expansions. Configurable will do on ActionContr…

…oller::Base or can extend ActiveAssets::ActiveExpansions::Reload in any controller
  • Loading branch information...
1 parent 7de3328 commit 595c833abe84073465c99bd94851854df0c69cd3 @shwoodard committed Apr 22, 2011
View
@@ -5,6 +5,7 @@ Bundler::GemHelper.install_tasks
Rake::TestTask.new(:test) do |t|
t.libs << "test"
+ t.libs << "test/fixtures/rails_root/test"
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
@@ -13,5 +13,38 @@ module ActiveExpansions
autoload :Javascripts
autoload :Stylesheets
autoload :Expansions
+
+ def self.load_active_expansions(root)
+ if File.exists?(File.join(root, 'config/assets.rb'))
+ load File.join(root, 'config/assets.rb')
+ elsif File.directory?(File.join(root, 'config/assets'))
+ Dir[File.join(root, 'config/assets/*.rb')].each do |f|
+ load f
+ end
+ end
+ end
+
+ def self.load_expansions_and_register
+ ActiveExpansions.load_active_expansions(Rails.root)
+ Rails.application.railties.engines.each {|e| ActiveExpansions.load_active_expansions(e.root) }
+ Rails.application.expansions.javascripts.register!
+ Rails.application.expansions.stylesheets.register!
+ end
+
+ def self.remove_active_expansions
+ Rails.application.expansions.javascripts.expansion_names.each do |expansion|
+ Rails.application.expansions.javascripts.remove(expansion)
+ if ActionView::Helpers::AssetTagHelper.javascript_expansions.has_key?(expansion)
+ ActionView::Helpers::AssetTagHelper.javascript_expansions.delete(expansion)
+ end
+ end
+
+ Rails.application.expansions.stylesheets.expansion_names.each do |expansion|
+ Rails.application.expansions.stylesheets.remove(expansion)
+ if ActionView::Helpers::AssetTagHelper.stylesheet_expansions.has_key?(expansion)
+ ActionView::Helpers::AssetTagHelper.stylesheet_expansions.delete(expansion)
+ end
+ end
+ end
end
end
@@ -14,7 +14,7 @@ def has_expansion?(name)
end
def expansion_names
- @expansion.keys
+ @expansions.keys
end
def [](expansion_name)
@@ -30,6 +30,10 @@ def paths(expansion_name)
self[expansion_name].assets.map(&:path)
end
+ def remove(expansion_name)
+ @expansions.delete(expansion_name)
+ end
+
def asset_type
raise NoMethodError
end
@@ -6,8 +6,9 @@ module Configurable
extend ActiveSupport::Concern
included do
- config_accessor :precache_assets
+ config_accessor :precache_assets, :reload_expansions
self.precache_assets = false if precache_assets.nil?
+ self.reload_expansions = false if reload_expansions.nil?
end
end
end
@@ -1,6 +1,7 @@
require "rails"
require "rails/active_expansions"
require 'active_support/ordered_options'
+require 'active_assets/active_expansions/reload'
module ActiveAssets
module ActiveExpansions
@@ -15,39 +16,27 @@ class Railtie < Rails::Railtie
Rails.application.extend(Rails::ActiveExpansions)
end
- initializer 'active_expansions-load-definitons' do
- load_active_assets(Rails.root)
- Rails.application.railties.engines.each {|e| load_active_assets(e.root) }
- end
-
- initializer 'active_expansions-register' do
- Rails.application.expansions.javascripts.register!
- Rails.application.expansions.stylesheets.register!
- end
-
initializer 'active_expansions-set-configs' do
options = config.active_expansions
ActiveSupport.on_load(:active_expansions) do
options.each { |k,v| send("#{k}=", v) }
end
end
- initializer 'active_expansions-cache' do
- if Expansions.precache_assets
- Rails.application.expansions.javascripts.cache! and Rails.application.expansions.stylesheets.cache!
+ initializer 'active_expansions-load-definitions-and-register' do
+ ActiveExpansions.load_expansions_and_register
+
+ if ActiveAssets::ActiveExpansions::Expansions.reload_expansions
+ ActionController::Base.extend(Reload)
end
end
- private
- def load_active_assets(root)
- if File.exists?(File.join(root, 'config/assets.rb'))
- load File.join(root, 'config/assets.rb')
- elsif File.directory?(File.join(root, 'config/assets'))
- Dir[File.join(root, 'config/assets/*.rb')].each do |f|
- load f
- end
- end
+ initializer 'active_expansions-cache' do
+ if Expansions.precache_assets
+ Rails.application.expansions.javascripts.cache!
+ Rails.application.expansions.stylesheets.cache!
end
+ end
end
end
end
@@ -0,0 +1,14 @@
+module ActiveAssets
+ module ActiveExpansions
+ module Reload
+
+ def self.extended(controller)
+ controller.before_filter do
+ ActiveAssets::ActiveExpansions.remove_active_expansions
+ ActiveAssets::ActiveExpansions.load_expansions_and_register
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,7 @@
+class ApplicationController < ActionController::Base
+ extend ActiveAssets::ActiveExpansions::Reload
+
+ def index
+ head :ok
+ end
+end
@@ -1,9 +1,18 @@
-require 'rails/all'
+# Pick the frameworks you want:
+# require 'rails/all'
+# require "active_record/railtie"
+require "action_controller/railtie"
+# require "action_mailer/railtie"
+# require "active_resource/railtie"
+# require "active_model/railtie"
+require "rails/test_unit/railtie"
require 'active_assets/railtie'
module ActiveAssetsTest
class Application < Rails::Application
config.root = File.expand_path('../..', __FILE__)
+ config.cache_classes = false
+ # config.active_expansions.reload_expansions = true
end
end
@@ -0,0 +1,5 @@
+ Rails.application.expansions do
+ expansion :new do
+ _'foo.js'
+ end
+ end
@@ -0,0 +1,3 @@
+Rails.application.routes.draw do
+ root :to => "application#index"
+end
@@ -0,0 +1,52 @@
+require 'test_helper'
+require 'fileutils'
+
+class ApplicationControllerTest < ActionController::TestCase
+ include FileUtils
+
+ def new_assets
+ File.expand_path('../../../config/assets/new.rb', __FILE__)
+ end
+
+ def write_new_assets
+ File.open(new_assets, 'w+') do |io|
+ io.puts <<-EOF
+ Rails.application.expansions do
+ expansion :new do
+ _'foo.js'
+ end
+ end
+ EOF
+ end
+ end
+
+ def teardown
+ rm_rf new_assets
+ ActionView::Helpers::AssetTagHelper.javascript_expansions.delete(:new)
+ end
+
+ def test_index
+ get :index
+ assert response.success?
+ end
+
+ def test_expansions
+ assert ActionView::Helpers::AssetTagHelper.javascript_expansions.any? {|k, v| k == :basfoo}
+ end
+
+ def test_can_add_expansion
+ assert !ActionView::Helpers::AssetTagHelper.javascript_expansions.any? {|k, v| k == :new}
+ get :index
+ write_new_assets
+ get :index
+ assert ActionView::Helpers::AssetTagHelper.javascript_expansions.any? {|k, v| k == :new}
+ end
+
+ def test_can_remove_expansion
+ test_can_add_expansion
+ rm_rf new_assets
+ get :index
+
+ assert !ActionView::Helpers::AssetTagHelper.javascript_expansions.any? {|k, v| k == :new}
+ end
+end
@@ -0,0 +1,6 @@
+ENV['RAILS_ENV'] ||= 'test'
+
+require File.expand_path('../../config/environment', __FILE__)
+require 'rails/test_help'
+
+
View
@@ -1,9 +1,7 @@
ENV['RAILS_ENV'] ||= 'test'
-require 'rubygems'
require 'test/unit'
-require 'rails/all'
require 'active_assets'
require 'socket'

0 comments on commit 595c833

Please sign in to comment.