Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removing automatic appending of paths to Environments

  • Loading branch information...
commit bb891790ae39eee8f31718a65367ae1db7fcfb8b 1 parent 3fe2299
Pete Browne authored
83 README.md
View
@@ -1,7 +1,7 @@
sprockets-plugin
================
-Package assets into gems for non-Rails applications.
+Package assets into gems for non-Rails Sprockets 2.x applications.
Installation
@@ -12,61 +12,68 @@ $ gem install sprockets-plugin
```
-Usage
------
+Usage in Applications
+---------------------
-Sprockets::Plugin is meant to be used within gems, to package assets for reuse. So the first step is to add it as a dependency in your gemspec:
+To use Sprockets plugins, you only need to do 3 things:
+1. Require "sprockets-plugin" to hook everything up.
+ _This may be required by the plugins themselves, but it's best practice to also require this in your application._
+2. Require any plugins you want to use.
+3. Call Sprockets::Environment#append_plugin_paths after setting up your application paths.
+ _Sprockets::Plugin **does not** automatically append paths to the environment. This is because the plugin paths would take precedence over your application's paths._
+
+Here's an example:
+
``` ruby
-Gem::Specification.new do |s|
- # ...
- s.add_runtime_dependency "sprockets-plugin"
+require "sprockets"
+require "sprockets-plugin" # 1.
+require "my_plugin" # 2.
+
+map "/assets" do
+ env = Sprockets::Environment.new
+ env.append_path "assets/images"
+ env.append_path "assets/javascripts"
+ env.append_path "assets/stylesheets"
+ env.append_plugin_paths # 3.
+ run env
end
```
-And then extend Sprockets::Plugin and add the necessary asset paths:
+Usage in Gems
+-------------
-``` ruby
-require "sprockets-plugin"
+Sprockets::Plugin is meant to be used within gems, to package assets for reuse. Again, there's only 3 things to do to set this up.
-class MyPlugin < Sprockets::Plugin
- # Set the root path to use relative paths in `.append_path`
- root File.expand_path("../..", __FILE__)
- append_path "lib/assets/images"
- append_path "lib/assets/javascripts"
- append_path "lib/assets/stylesheets"
-end
-```
+1. Add it as a dependency in your gemspec.
+2. Extend Sprockets::Plugin.
+3. Add the necessary paths.
-Now any assets in the "lib/assets" directory will be available to applications that require this gem:
+`my_plugin.gemspec`:
``` ruby
-require "sprockets"
-require "my_plugin"
-
-map "/assets" do
- environment = Sprockets::Environment.new
- # The assets from MyPlugin will be automatically appended.
- environment.append_path "assets/images"
- environment.append_path "assets/javascripts"
- environment.append_path "assets/stylesheets"
- run environment
+Gem::Specification.new do |s|
+ # ...
+ s.add_runtime_dependency "sprockets-plugin" # 1.
end
```
-Advanced Usage
---------------
-
-You can package assets for Rails and non-Rails applications in the following way:
+`my_plugin.rb`:
``` ruby
-if defined? Rails
- require "my_assets/engine"
-elsif defined? Sprockets::Plugin
- require "my_assets/plugin"
-end
+require "sprockets-plugin"
+
+class MyPlugin < Sprockets::Plugin # 2.
+ # Set the root path to use relative paths in `.append_path`
+ root File.expand_path("../..", __FILE__)
+
+ append_path "lib/assets/images" # 3.
+ append_path "lib/assets/javascripts" # 3.
+ append_path "lib/assets/stylesheets" # 3.
+end
```
+
Copyright
---------
16 lib/sprockets/plugin/aware.rb 100644 → 100755
View
@@ -3,22 +3,6 @@
module Sprockets
class Plugin
module Aware
- def self.included(base)
- base.extend ClassMethods
- end
-
- module ClassMethods
- # Overrides .new to append Plugin paths after
- # initialization.
- #
- # Is there a better way to do this?
- def new(*args)
- super(*args) do |env|
- env.append_plugin_paths
- end
- end
- end
-
# Appends the paths from each Sprockets::Plugin
# to the Sprockets::Environment.
def append_plugin_paths
70 spec/sprockets-plugin_spec.rb
View
@@ -5,40 +5,44 @@
Sprockets::Plugin.send :class_variable_set, :@@plugins, nil
end
- it "adds paths from plugins to newly created environments" do
- dir_1 = @sandbox.directory "plugin_1/assets"
- dir_2 = @sandbox.directory "plugin_2/assets"
- dir_3 = @sandbox.directory "plugin_3/assets"
-
- plugin_1 = Class.new Sprockets::Plugin
- plugin_1.append_path dir_1
- plugin_2 = Class.new Sprockets::Plugin
- plugin_2.append_path dir_2
- plugin_3 = Class.new Sprockets::Plugin
- plugin_3.append_path dir_3
-
- env = Sprockets::Environment.new
- env.paths.should == [dir_1, dir_2, dir_3].map(&:to_s)
- end
+ describe "#append_plugin_paths" do
+ it "adds paths from plugins to environments" do
+ dir_1 = @sandbox.directory "plugin_1/assets"
+ dir_2 = @sandbox.directory "plugin_2/assets"
+ dir_3 = @sandbox.directory "plugin_3/assets"
+
+ plugin_1 = Class.new Sprockets::Plugin
+ plugin_1.append_path dir_1
+ plugin_2 = Class.new Sprockets::Plugin
+ plugin_2.append_path dir_2
+ plugin_3 = Class.new Sprockets::Plugin
+ plugin_3.append_path dir_3
+
+ env = Sprockets::Environment.new
+ env.append_plugin_paths
+ env.paths.should == [dir_1, dir_2, dir_3].map(&:to_s)
+ end
- it "adds a #append_plugin_paths method for adding paths from plugins" do
- dir_1 = @sandbox.directory "plugin_1/assets"
- dir_2 = @sandbox.directory "plugin_2/assets"
- dir_3 = @sandbox.directory "plugin_3/assets"
-
- plugin_1 = Class.new Sprockets::Plugin
- plugin_1.append_path dir_1
-
- env = Sprockets::Environment.new
- env.paths.should == [dir_1].map(&:to_s)
-
- plugin_2 = Class.new Sprockets::Plugin
- plugin_2.append_path dir_2
- plugin_3 = Class.new Sprockets::Plugin
- plugin_3.append_path dir_3
-
- env.append_plugin_paths
- env.paths.should == [dir_1, dir_2, dir_3].map(&:to_s)
+ it "does not add duplicate paths" do
+ dir_1 = @sandbox.directory "plugin_1/assets"
+ dir_2 = @sandbox.directory "plugin_2/assets"
+ dir_3 = @sandbox.directory "plugin_3/assets"
+
+ plugin_1 = Class.new Sprockets::Plugin
+ plugin_1.append_path dir_1
+
+ env = Sprockets::Environment.new
+ env.append_plugin_paths
+ env.paths.should == [dir_1].map(&:to_s)
+
+ plugin_2 = Class.new Sprockets::Plugin
+ plugin_2.append_path dir_2
+ plugin_3 = Class.new Sprockets::Plugin
+ plugin_3.append_path dir_3
+
+ env.append_plugin_paths
+ env.paths.should == [dir_1, dir_2, dir_3].map(&:to_s)
+ end
end
describe ".append_path" do
4 sprockets-plugin.gemspec 100644 → 100755
View
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
s.authors = ["Pete Browne"]
s.email = ["me@petebrowne.com"]
s.homepage = "https://github.com/petebrowne/sprockets-plugin"
- s.summary = %q{Package assets into gems for non-Rails applications.}
- s.description = %q{Package assets into gems for non-Rails applications.}
+ s.summary = %q{Package assets into gems for non-Rails Sprockets 2.x applications.}
+ s.description = %q{Package assets into gems for non-Rails Sprockets 2.x applications.}
s.rubyforge_project = "sprockets-plugin"
Please sign in to comment.
Something went wrong with that request. Please try again.