make it stupid-simple to use

This commit is contained in:
John Bintz 2012-08-02 12:50:20 -04:00
parent f64c6a8444
commit f81e0900d4
4 changed files with 77 additions and 18 deletions

View File

@ -1,6 +1,49 @@
# Get at available assets in loaded gems in Sprockets # 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 ``` ruby
require 'sprockets-vendor_gems' require 'sprockets-vendor_gems'
@ -11,7 +54,7 @@ Sprockets.find_gem_vendor_paths(:for => :javascript).each do |path|
end 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 ``` ruby
require 'sprockets-vendor_gems' require 'sprockets-vendor_gems'
@ -21,17 +64,5 @@ env = Sprockets::EnvironmentWithVendoredGems.new
Yeah! Yeah!
## Installation 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.
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

View File

@ -2,10 +2,18 @@ require 'sprockets'
require 'rubygems' require 'rubygems'
module ::Sprockets 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 = {}) def self.find_gem_vendor_paths(options = {})
options = { :paths => %w{vendor lib app} }.merge(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 = [] paths = []
@ -24,6 +32,12 @@ module ::Sprockets
def initialize(*args) def initialize(*args)
super(*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) } Sprockets.find_gem_vendor_paths.each { |path| append_path(path) }
end end
end end

View File

@ -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

View File

@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "sprockets-vendor_gems" gem.name = "sprockets-vendor_gems"
gem.require_paths = ["lib"] gem.require_paths = ["lib"]
gem.version = '0.1.2' gem.version = '0.1.3'
gem.add_dependency 'sprockets' gem.add_dependency 'sprockets'