Permalink
Browse files

Make asset syncing on precompile optional.

  • Loading branch information...
1 parent 061f45b commit cfb2cb224e1f209152a9c4aebbc5868adf29f5d4 @freerobby freerobby committed Nov 1, 2012
Showing with 45 additions and 6 deletions.
  1. +22 −4 README.md
  2. +3 −0 lib/asset_sync/config.rb
  3. +11 −2 lib/tasks/asset_sync.rake
  4. +8 −0 spec/asset_sync_spec.rb
  5. +1 −0 spec/aws_with_yml/config/asset_sync.yml
View
@@ -306,21 +306,39 @@ With the new **user_env_compile** feature of Heroku (see above), this is no long
## Rake Task
-A rake task is included within the **asset_sync** gem to enhance the rails precompile task by automatically running after it.
+A rake task is included within the **asset_sync** gem to perform the sync:
+
+``` ruby
+ namespace :assets do
+ desc "Synchronize assets to S3"
+ task :sync => :environment do
+ AssetSync.sync
+ end
+ end
+```
+
+If `AssetSync.config.run_on_precompile` is `true` (default), then assets will be uploaded to S3 automatically after the `assets:precompile` rake task is invoked:
``` ruby
- # asset_sync/lib/tasks/asset_sync.rake
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
- AssetSync.sync
+ # Conditional execution needs to be inside the enhance block because the enhance block
+ # will get executed before yaml or Rails initializers.
+ Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
end
else
Rake::Task["assets:precompile"].enhance do
- AssetSync.sync
+ # rails 3.1.1 will clear out Rails.application.config if the env vars
+ # RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
+ # assets environment in this case.
+ # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
+ Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
end
end
```
+You can disable this behavior by setting `AssetSync.config.run_on_precompile = false`.
+
## Sinatra/Rack Support
You can use the gem with any Rack application, but you must specify two
@@ -14,6 +14,7 @@ class Invalid < StandardError; end
attr_accessor :prefix
attr_accessor :public_path
attr_accessor :enabled
+ attr_accessor :run_on_precompile
# FOG configuration
attr_accessor :fog_provider # Currently Supported ['AWS', 'Rackspace']
@@ -50,6 +51,7 @@ def initialize
self.always_upload = []
self.ignored_files = []
self.enabled = true
+ self.run_on_precompile = true
load_yml! if defined?(Rails) && yml_exists?
end
@@ -126,6 +128,7 @@ def load_yml!
self.fail_silently = yml["fail_silently"] if yml.has_key?("fail_silently")
self.always_upload = yml["always_upload"] if yml.has_key?("always_upload")
self.ignored_files = yml["ignored_files"] if yml.has_key?("ignored_files")
+ self.run_on_precompile = yml["run_on_precompile"] if yml.has_key?("run_on_precompile")
# TODO deprecate the other old style config settings. FML.
self.aws_access_key_id = yml["aws_access_key"] if yml.has_key?("aws_access_key")
@@ -1,13 +1,22 @@
+namespace :assets do
+ desc "Synchronize assets to S3"
+ task :sync => :environment do
+ AssetSync.sync
+ end
+end
+
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
- AssetSync.sync
+ # Conditional execution needs to be inside the enhance block because the enhance block
+ # will get executed before yaml or Rails initializers.
+ Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
end
else
Rake::Task["assets:precompile"].enhance do
# rails 3.1.1 will clear out Rails.application.config if the env vars
# RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
# assets environment in this case.
# Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
- AssetSync.sync
+ Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
end
end
@@ -16,6 +16,10 @@
end
end
+ it "should default to running on precompile" do
+ AssetSync.config.run_on_precompile.should be_true
+ end
+
it "should default AssetSync to enabled" do
AssetSync.config.enabled?.should be_true
AssetSync.enabled?.should be_true
@@ -70,6 +74,10 @@
AssetSync.enabled?.should be_true
end
+ it "should configure run_on_precompile" do
+ AssetSync.config.run_on_precompile.should be_false
+ end
+
it "should configure aws_access_key_id" do
AssetSync.config.aws_access_key_id.should == "xxxx"
end
@@ -3,6 +3,7 @@ defaults: &defaults
aws_access_key_id: "xxxx"
aws_secret_access_key: "zzzz"
region: "eu-west-1"
+ run_on_precompile: false
development:
<<: *defaults

0 comments on commit cfb2cb2

Please sign in to comment.