├── .gitignore ├── lib ├── sprockets_helper.rb ├── sprockets_controller.rb ├── tasks │ └── sprockets_tasks.rake └── sprockets_application.rb ├── init.rb ├── test └── sprockets_test.rb ├── config └── sprockets.yml ├── install.rb ├── Rakefile ├── MIT-LICENSE └── README.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /lib/sprockets_helper.rb: -------------------------------------------------------------------------------- 1 | module SprocketsHelper 2 | def sprockets_include_tag 3 | javascript_include_tag("/sprockets.js") 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require "sprockets" 2 | require "sprockets_helper" 3 | require "sprockets_application" 4 | 5 | class ActionController::Base 6 | helper :sprockets 7 | end 8 | -------------------------------------------------------------------------------- /test/sprockets_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | 3 | class SprocketsTest < Test::Unit::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /config/sprockets.yml: -------------------------------------------------------------------------------- 1 | :asset_root: public 2 | :load_path: 3 | - app/javascripts 4 | - vendor/sprockets/*/src 5 | - vendor/plugins/*/javascripts 6 | - vendor/plugins/*/app/javascripts 7 | :source_files: 8 | - app/javascripts/application.js 9 | - app/javascripts/**/*.js 10 | -------------------------------------------------------------------------------- /lib/sprockets_controller.rb: -------------------------------------------------------------------------------- 1 | class SprocketsController < ActionController::Base 2 | caches_page :show, :if => Proc.new { SprocketsApplication.use_page_caching } 3 | 4 | def show 5 | render :text => SprocketsApplication.source, :content_type => "text/javascript" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/tasks/sprockets_tasks.rake: -------------------------------------------------------------------------------- 1 | namespace :sprockets do 2 | desc "Generate and install the Sprockets concatenated JavaScript file" 3 | task :install_script => :environment do 4 | SprocketsApplication.install_script 5 | end 6 | 7 | desc "Install any assets provided by Sprockets scripts" 8 | task :install_assets => :environment do 9 | SprocketsApplication.install_assets 10 | end 11 | end -------------------------------------------------------------------------------- /install.rb: -------------------------------------------------------------------------------- 1 | require "fileutils" 2 | include FileUtils::Verbose 3 | 4 | RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..")) unless defined?(RAILS_ROOT) 5 | 6 | mkdir_p File.join(RAILS_ROOT, "vendor", "sprockets") 7 | mkdir_p File.join(RAILS_ROOT, "app", "javascripts") 8 | touch File.join(RAILS_ROOT, "app", "javascripts", "application.js") 9 | cp File.join(File.dirname(__FILE__), "config", "sprockets.yml"), 10 | File.join(RAILS_ROOT, "config") 11 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the sprockets plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/*_test.rb' 12 | t.verbose = true 13 | end 14 | 15 | desc 'Generate documentation for the sprockets plugin.' 16 | Rake::RDocTask.new(:rdoc) do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'Sprockets' 19 | rdoc.options << '--line-numbers' << '--inline-source' 20 | rdoc.rdoc_files.include('README') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Sam Stephenson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /lib/sprockets_application.rb: -------------------------------------------------------------------------------- 1 | module SprocketsApplication 2 | mattr_accessor :use_page_caching 3 | self.use_page_caching = true 4 | 5 | class << self 6 | def routes(map) 7 | map.resource(:sprockets) 8 | end 9 | 10 | def source 11 | concatenation.to_s 12 | end 13 | 14 | def install_script 15 | concatenation.save_to(asset_path) 16 | end 17 | 18 | def install_assets 19 | secretary.install_assets 20 | end 21 | 22 | protected 23 | def secretary 24 | @secretary ||= Sprockets::Secretary.new(configuration.merge(:root => RAILS_ROOT)) 25 | end 26 | 27 | def configuration 28 | YAML.load(IO.read(File.join(RAILS_ROOT, "config", "sprockets.yml"))) || {} 29 | end 30 | 31 | def concatenation 32 | secretary.reset! unless source_is_unchanged? 33 | secretary.concatenation 34 | end 35 | 36 | def asset_path 37 | File.join(Rails.public_path, "sprockets.js") 38 | end 39 | 40 | def source_is_unchanged? 41 | previous_source_last_modified, @source_last_modified = 42 | @source_last_modified, secretary.source_last_modified 43 | 44 | previous_source_last_modified == @source_last_modified 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | `sprockets-rails` 2 | ================= 3 | 4 | The `sprockets-rails` plugin sets up your Rails application for use with [Sprockets](http://github.com/sstephenson/sprockets). To install it, first install the `sprockets` RubyGem, then check out a copy of the `sprockets-rails` repository into your `vendor/plugins/` directory. When you run the bundled `install.rb` script, `sprockets-rails` will create two new directories in your application and copy a configuration file into your `config/` directory. 5 | 6 | `sprockets-rails` includes a controller named `SprocketsController` that renders your application's Sprockets concatenation. When caching is enabled, e.g. in production mode, `SprocketsController` uses Rails page caching to save the concatenated output to `public/sprockets.js` the first time it is requested. When caching is disabled, e.g. in development mode, `SprocketsController` will render a fresh concatenation any time a source file changes. 7 | 8 | To source Sprockets' JavaScript concatenation from your HTML templates, use the provided `sprockets_include_tag` helper. 9 | 10 | `sprockets-rails` also includes a set of Rake tasks for generating the concatenation (`rake sprockets:install_script`) and installing provided assets (`rake sprockets:install_assets`). Run `sprockets:install_assets` any time you add or update a Sprockets plugin in your application. Add `sprockets:install_script` as a [Capistrano](http://www.capify.org/) post-deploy hook to generate the Sprockets concatenation on your servers automatically at deploy time. 11 | 12 | Here's a walkthrough of the installation process: 13 | 14 | 1. `gem install --remote sprockets` 15 | 16 | 2. `script/plugin install git://github.com/sstephenson/sprockets-rails.git` 17 | 18 | You now have `app/javascripts/` and `vendor/sprockets/` directories in your application, as well as a `config/sprockets.yml` file. 19 | 20 | 3. Edit your `config/routes.rb` file to add routes for `SprocketsController`: 21 | 22 | ActionController::Routing::Routes.draw do |map| 23 | # Add the following line: 24 | SprocketsApplication.routes(map) 25 | ... 26 | end 27 | 28 | 4. Move your JavaScript source files from `public/javascripts/` into `app/javascripts/`. All files in all subdirectories of `app/javascripts/` will be required by Sprockets in alphabetical order, with the exception of `app/javascripts/application.js`, which is required _before any other file_. (You can change this behavior by editing the `source_files` line of `config/sprockets.yml`.) 29 | 30 | 5. Adjust your HTML templates to call `<%= sprockets_include_tag %>` instead of `<%= javascript_include_tag ... %>`. 31 | 32 | Once `sprockets-rails` is installed, you can check out Sprockets plugins into the `vendor/sprockets/` directory. By default, `sprockets-rails` configures Sprockets' load path to search `vendor/sprockets/*/src/`, as well as `vendor/plugins/*/javascripts/`. This means that the `javascripts/` directories of Rails plugins are automatically installed into your Sprockets load path. 33 | 34 | ## License 35 | 36 | Copyright © 2009 Sam Stephenson. 37 | 38 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 39 | 40 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 41 | 42 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 43 | --------------------------------------------------------------------------------