Skip to content
Browse files

refactored support for multiple configurations

- always map plural routes for SprocketsController (eases transition from single- to multi-configuration, a common scenario);
- reverted default sprockets.yml to single-configuration;
- merged SprocketsMechanism into Sprockets class, which now encapsulates one configuration;
- updated rake and capistrano tasks to support multiple configurations (by default, the rake task installs the script or assets for all configurations);
  • Loading branch information...
1 parent b32d601 commit db8ccff331a5c84b2c4b38b268ffc3953ce98257 @neodude neodude committed Mar 20, 2009
View
10 README.markdown
@@ -52,15 +52,7 @@ Once `sprockets-rails` is installed, you can check out Sprockets plugins into th
:source_files:
- app/javascripts/special.js
-2. Edit your `config/routes.rb` file to use plural resources for `SprocketsController`:
-
- ActionController::Routing::Routes.draw do |map|
- # Add the following line:
- SprocketsApplication.routes(map, :resources)
- ...
- end
-
-3. Adjust your HTML templates to call `<%= sprockets_include_tag(:config_name) %>` with the name of the configuration you wish to use.
+2. Adjust your HTML templates to call `<%= sprockets_include_tag(:config_name) %>` with the name of the configuration you wish to use.
## License
View
17 config/sprockets.yml
@@ -1,9 +1,8 @@
-:default:
- :asset_root: public
- :load_path:
- - app/javascripts
- - vendor/sprockets/*/src
- - vendor/plugins/*/javascripts
- :source_files:
- - app/javascripts/application.js
- - app/javascripts/**/*.js
+:asset_root: public
+:load_path:
+ - app/javascripts
+ - vendor/sprockets/*/src
+ - vendor/plugins/*/javascripts
+:source_files:
+ - app/javascripts/application.js
+ - app/javascripts/**/*.js
View
72 lib/sprocket.rb
@@ -1,17 +1,67 @@
class Sprocket
- include SprocketsMechanism
-
- def initialize(configuration)
- if configuration.is_a? Symbol or configuration.is_a? String
- @configuration = load_configuration(configuration)
- else
- @configuration = configuration
- end
+ attr_reader :group
+
+ def initialize(group='default')
+ @group = group.to_sym
+ end
+
+ def source
+ concatenation.to_s
+ end
+
+ def install_script
+ concatenation.save_to(asset_path)
+ end
+
+ def install_assets
+ secretary.install_assets
+ end
+
+ def self.configurations
+ @conf ||= YAML.load(IO.read(configuration_path)) || {}
+ @conf.keys
end
protected
+ def secretary
+ @secretary ||= Sprockets::Secretary.new(configuration.merge(:root => Rails.root))
+ end
+
+ def configuration
+ load_configuration
+ end
+
+ def load_configuration
+ @conf ||= YAML.load(IO.read(configuration_path)) || {}
+ if @conf[group]
+ @conf[group]
+ else
+ {}
+ end
+ end
- def configuration
- @configuration || {}
- end
+ def concatenation
+ secretary.reset! unless source_is_unchanged?
+ secretary.concatenation
+ end
+
+ def configuration_path
+ File.join(Rails.root, "config", "sprockets.yml")
+ end
+
+ def asset_path
+ if group == :default
+ File.join(Rails.public_path, "sprockets.js")
+ else
+ File.join(Rails.public_path, "sprockets", "#{group}.js")
+ end
+ end
+
+ def source_is_unchanged?
+ previous_source_last_modified, @source_last_modified =
+ @source_last_modified, secretary.source_last_modified
+
+ previous_source_last_modified == @source_last_modified
+ end
+
end
View
12 lib/sprockets_application.rb
@@ -1,15 +1,9 @@
module SprocketsApplication
- extend SprocketsMechanism
-
- def routes(map, method = :resource)
- if method == :resource
- map.resource(:sprockets, :only => :show)
- elsif method == :resources
- map.resources(:sprockets, :only => [:index, :show])
- end
+ def routes(map)
+ map.resources(:sprockets, :only => [:index, :show])
end
- def sprocket(name)
+ def sprocket(name=nil)
Sprocket.new(name)
end
end
View
7 lib/sprockets_controller.rb
@@ -6,12 +6,7 @@ def index
end
def show
- sprocket = nil
- if params[:id]
- sprocket = SprocketsApplication.sprocket(params[:id])
- else
- sprocket = SprocketsApplication
- end
+ sprocket = SprocketsApplication.sprocket(params[:id])
render :text => sprocket.source, :content_type => "text/javascript"
end
end
View
51 lib/sprockets_mechanism.rb
@@ -1,51 +0,0 @@
-module SprocketsMechanism
- def source
- concatenation.to_s
- end
-
- def install_script
- concatenation.save_to(asset_path)
- end
-
- def install_assets
- secretary.install_assets
- end
-
- protected
- def secretary
- @secretary ||= Sprockets::Secretary.new(configuration.merge(:root => Rails.root))
- end
-
- def configuration
- load_configuration
- end
-
- def load_configuration(key = :default)
- conf = YAML.load(IO.read(File.join(Rails.root, "config", "sprockets.yml"))) || {}
- if conf[key]
- conf[key]
- elsif conf.any? { |k, v| k.to_s == key }
- conf[key.to_sym]
- elsif key == :default
- conf || {}
- else
- {}
- end
- end
-
- def concatenation
- secretary.reset! unless source_is_unchanged?
- secretary.concatenation
- end
-
- def asset_path
- File.join(Rails.public_path, "sprockets.js")
- end
-
- def source_is_unchanged?
- previous_source_last_modified, @source_last_modified =
- @source_last_modified, secretary.source_last_modified
-
- previous_source_last_modified == @source_last_modified
- end
-end
View
4 recipes/sprockets_tasks.rb
@@ -5,12 +5,12 @@ def run_sprockets_rake_task(task)
end
namespace :sprockets do
- desc "Generate and install the Sprockets concatenated JavaScript file"
+ desc "Generate and install the Sprockets concatenated JavaScript file for all configuration groups"
task :install_script, :roles => :app do
run_sprockets_rake_task 'install_script'
end
- desc "Install any assets provided by Sprockets scripts"
+ desc "Install any assets provided by Sprockets script, for all configuration groups"
task :install_assets, :roles => :app do
run_sprockets_rake_task 'install_assets'
end
View
26 tasks/sprockets_tasks.rake
@@ -1,13 +1,31 @@
+def process_config
+ if (config = ENV['CONFIG'] || config = ENV['CONFIGURATION']) && !Sprocket.configurations.include?(config)
+ raise "Configuration #{config} doesn't exist. Valid configurations: #{Sprocket.configurations.to_sentence}."
+ end
+
+ return config
+end
+
namespace :sprockets do
- desc "Generate and install the Sprockets concatenated JavaScript file"
+ desc "Generate and install the Sprockets concatenated JavaScript file for all configuration groups. Generate and install for a specific configuration by specifying CONFIG=<name>."
task :install_script do
require "config/environment"
- SprocketsApplication.install_script
+
+ if config = process_config
+ Sprocket.new(config).install_script
+ else
+ Sprocket.configurations.each { |configuration| Sprocket.new(configuration).install_script }
+ end
end
- desc "Install any assets provided by Sprockets scripts"
+ desc "Install any assets provided by Sprockets script, for all configuration groups. Install assets for a specific configuration by specifying CONFIG=<name>."
task :install_assets do
require "config/environment"
- SprocketsApplication.install_assets
+
+ if config = process_config
+ Sprocket.new(config).install_assets
+ else
+ Sprocket.configurations.each { |configuration| Sprocket.new(configuration).install_assets }
+ end
end
end

0 comments on commit db8ccff

Please sign in to comment.
Something went wrong with that request. Please try again.