From bc99a928abc3256c2d93139733490dac5f7c53d0 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Sun, 4 Sep 2011 18:09:45 +1000 Subject: [PATCH] apply rails_env option to both runners, shorter readme. --- README.md | 25 +++++----------------- lib/guard/rails-assets/cli_runner.rb | 5 +++-- spec/guard/rails-assets/cli_runner_spec.rb | 13 ++++++++--- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1d23b4c..85bc0da 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Guard::RailsAssets -Guard::RailsAssets compiles the assets in Rails 3.1 application automatically when files are modified. +Guard::RailsAssets compiles the assets within Rails 3.1 application whenever those change. -Tested on MRI Ruby 1.9.2 (please report if it works on your platform). +Tested on MRI 1.9.2 (please report if it works on your platform). If you have any questions please contact me [@dnagir](http://www.ApproachE.com). @@ -22,25 +22,10 @@ gem 'guard-rails-assets' Add guard definition to your `Guardfile` by running: ```bash -$ guard init rails-assets +$ bundle exec guard init rails-assets ``` -## Rails 3.1 - -The assets can be compiled using following "runners": - -1. rake command (CLI); -2. loading the actual Rails environment. - -In the 1st case - this Guard is not actually using Rails directly while in the 2nd - it loads it explicitly. - -Good thing about the 1st approach is that assets will always be same as produced by Rails. -Bad thing is that it is pretty slow (~10 seconds) because it starts Rails from ground zero. - -The 2nd approach is good because it is much faster, but does not reload Rails environment (so you have to restart guard). -Additionally it relies on a single instance of your app to be loaded, so you can't have multiple guards with different reails configurations. - -## Guardfile and Options +## Options In addition to the guard configuration, `guard-rails-assets` has options to specify when exacly to precompile assets. @@ -52,7 +37,7 @@ In addition to the guard configuration, `guard-rails-assets` has options to spec Also you can set the `:runner` option: - `:cli` - compile assets using the rake task - the most correct method, but slow. -- `:rails` - compile assets by loading rails environment (default) - fast, but does not pick up changes. +- `:rails` - compile assets by loading rails environment (default) - fast, but does not pick up changes. Additionally it relies on a single instance of your app to be loaded, so you can't have multiple guards with different rails configurations. You can also use `:rails_env` option to specify what Rails environment to use (defaults to 'test'). diff --git a/lib/guard/rails-assets/cli_runner.rb b/lib/guard/rails-assets/cli_runner.rb index 593d092..b3ba25e 100644 --- a/lib/guard/rails-assets/cli_runner.rb +++ b/lib/guard/rails-assets/cli_runner.rb @@ -1,10 +1,11 @@ module Guard class RailsAssets::CliRunner - def initialize(options) + def initialize(options={}) + @rails_env = (options[:rails_env] || 'test').to_s end def compile_assets - system "bundle exec rake assets:clean assets:precompile" + system "RAILS_ENV=#{@rails_env} bundle exec rake assets:clean assets:precompile" end end end diff --git a/spec/guard/rails-assets/cli_runner_spec.rb b/spec/guard/rails-assets/cli_runner_spec.rb index 9c317d0..c3fecd8 100644 --- a/spec/guard/rails-assets/cli_runner_spec.rb +++ b/spec/guard/rails-assets/cli_runner_spec.rb @@ -2,11 +2,18 @@ require 'spec_helper' describe Guard::RailsAssets::CliRunner do - subject { Guard::RailsAssets::CliRunner.new({}) } - it 'should run the command' do subject.stub(:system) - subject.should_receive(:system).with("bundle exec rake assets:clean assets:precompile") + subject.should_receive(:system).with("RAILS_ENV=test bundle exec rake assets:clean assets:precompile") subject.compile_assets end + + context 'with production environment' do + subject { Guard::RailsAssets::CliRunner.new(:rails_env => :production) } + it 'should run the command' do + subject.stub(:system) + subject.should_receive(:system).with("RAILS_ENV=production bundle exec rake assets:clean assets:precompile") + subject.compile_assets + end + end end