diff --git a/README.md b/README.md index 2cbe2f1..93c46f5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,49 @@ # Get at available assets in loaded gems in Sprockets -Either get the list of asset paths: +Who says you need Rails Engines to use the vendored assets in gems? All you need is +`sprockets-vendor_gems`. Use those cool vendored JavaScript & CSS gems in anything! + +The easiest way? Punch Sprockets right in the duck! + +``` ruby +require 'sprockets-vendor_gems/extend_all' +``` + +So, for instance, in Sinatra with [sinatra-sprockets](https://github.com/amarshall/sinatra-sprockets): + +``` ruby +# config.ru + +require 'sinatra/base' +require 'sprockets-vendor_gems/extend_all' +require 'sinatra/sprockets' + +class MyApp < Sinatra::Base + register Sinatra::Sprockets + + get '/' do + "hi" + end +end + +map "/assets" do + run Sinatra::Sprockets.environment +end + +run MyApp + +``` + +*Every* `Sprockets::Environment` instance now has every vendored gems asset path added! +By default, that's the `javascripts`, `stylesheets`, and `images` directories. Need more? + +``` ruby +Sprockets::VendorGems.default_types << "coolthings" +``` + +Do that before any `Sprockets::Environment`s get instantiated. + +Want more manual control? Either get the list of asset paths: ``` ruby require 'sprockets-vendor_gems' @@ -11,7 +54,7 @@ Sprockets.find_gem_vendor_paths(:for => :javascript).each do |path| end ``` -or get an Environment with those paths in there already: +or, instead of duck punching all of Sprockets, get an Environment with those paths in there already: ``` ruby require 'sprockets-vendor_gems' @@ -21,17 +64,5 @@ env = Sprockets::EnvironmentWithVendoredGems.new Yeah! -## Installation - -Add this line to your application's Gemfile: - - gem 'sprockets-vendor_gems' - -And then execute: - - $ bundle - -Or install it yourself as: - - $ gem install sprockets-vendor_gems - +You may have to futz a bit with `require` and fake classes for particular gems, especially if they really +rely on Rails to get their job done. diff --git a/lib/sprockets-vendor_gems.rb b/lib/sprockets-vendor_gems.rb index 9ef9faf..25e39ce 100644 --- a/lib/sprockets-vendor_gems.rb +++ b/lib/sprockets-vendor_gems.rb @@ -2,10 +2,18 @@ require 'sprockets' require 'rubygems' module ::Sprockets + module VendorGems + class << self + attr_accessor :default_types + end + + self.default_types = %w{javascripts stylesheets images} + end + def self.find_gem_vendor_paths(options = {}) options = { :paths => %w{vendor lib app} }.merge(options) - for_types = [ options[:for] || [ 'javascripts', 'stylesheets' ] ].flatten + for_types = [ options[:for] || ::Sprockets::VendorGems.default_types ].flatten paths = [] @@ -24,6 +32,12 @@ module ::Sprockets def initialize(*args) super(*args) + extend_with_vendored_gems + end + end + + class Environment + def extend_with_vendored_gems Sprockets.find_gem_vendor_paths.each { |path| append_path(path) } end end diff --git a/lib/sprockets-vendor_gems/extend_all.rb b/lib/sprockets-vendor_gems/extend_all.rb new file mode 100644 index 0000000..f0e6c79 --- /dev/null +++ b/lib/sprockets-vendor_gems/extend_all.rb @@ -0,0 +1,14 @@ +require 'sprockets-vendor_gems' + +module ::Sprockets + class Environment + alias :_initialize :initialize + + def initialize(*args) + _initialize(*args) + + self.extend_with_vendored_gems + end + end +end + diff --git a/sprockets-vendor_gems.gemspec b/sprockets-vendor_gems.gemspec index b5939e5..c7ef14d 100644 --- a/sprockets-vendor_gems.gemspec +++ b/sprockets-vendor_gems.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |gem| gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.name = "sprockets-vendor_gems" gem.require_paths = ["lib"] - gem.version = '0.1.2' + gem.version = '0.1.3' gem.add_dependency 'sprockets'