Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make asset syncing on precompile optional.

  • Loading branch information...
commit cfb2cb224e1f209152a9c4aebbc5868adf29f5d4 1 parent 061f45b
Robby Grossman authored November 01, 2012
26  README.md
Source Rendered
@@ -306,21 +306,39 @@ With the new **user_env_compile** feature of Heroku (see above), this is no long
306 306
 
307 307
 ## Rake Task
308 308
 
309  
-A rake task is included within the **asset_sync** gem to enhance the rails precompile task by automatically running after it.
  309
+A rake task is included within the **asset_sync** gem to perform the sync:
  310
+
  311
+``` ruby
  312
+  namespace :assets do
  313
+    desc "Synchronize assets to S3"
  314
+    task :sync => :environment do
  315
+      AssetSync.sync
  316
+    end
  317
+  end
  318
+```
  319
+
  320
+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:
310 321
 
311 322
 ``` ruby
312  
-  # asset_sync/lib/tasks/asset_sync.rake
313 323
   if Rake::Task.task_defined?("assets:precompile:nondigest")
314 324
     Rake::Task["assets:precompile:nondigest"].enhance do
315  
-      AssetSync.sync
  325
+      # Conditional execution needs to be inside the enhance block because the enhance block
  326
+      # will get executed before yaml or Rails initializers.
  327
+      Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
316 328
     end
317 329
   else
318 330
     Rake::Task["assets:precompile"].enhance do
319  
-      AssetSync.sync
  331
+      # rails 3.1.1 will clear out Rails.application.config if the env vars
  332
+      # RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
  333
+      # assets environment in this case.
  334
+      # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
  335
+      Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
320 336
     end
321 337
   end
322 338
 ```
323 339
 
  340
+You can disable this behavior by setting `AssetSync.config.run_on_precompile = false`.
  341
+
324 342
 ## Sinatra/Rack Support
325 343
 
326 344
 You can use the gem with any Rack application, but you must specify two
3  lib/asset_sync/config.rb
@@ -14,6 +14,7 @@ class Invalid < StandardError; end
14 14
     attr_accessor :prefix
15 15
     attr_accessor :public_path
16 16
     attr_accessor :enabled
  17
+    attr_accessor :run_on_precompile
17 18
 
18 19
     # FOG configuration
19 20
     attr_accessor :fog_provider          # Currently Supported ['AWS', 'Rackspace']
@@ -50,6 +51,7 @@ def initialize
50 51
       self.always_upload = []
51 52
       self.ignored_files = []
52 53
       self.enabled = true
  54
+      self.run_on_precompile = true
53 55
       load_yml! if defined?(Rails) && yml_exists?
54 56
     end
55 57
 
@@ -126,6 +128,7 @@ def load_yml!
126 128
       self.fail_silently          = yml["fail_silently"] if yml.has_key?("fail_silently")
127 129
       self.always_upload          = yml["always_upload"] if yml.has_key?("always_upload")
128 130
       self.ignored_files          = yml["ignored_files"] if yml.has_key?("ignored_files")
  131
+      self.run_on_precompile      = yml["run_on_precompile"] if yml.has_key?("run_on_precompile")
129 132
 
130 133
       # TODO deprecate the other old style config settings. FML.
131 134
       self.aws_access_key_id      = yml["aws_access_key"] if yml.has_key?("aws_access_key")
13  lib/tasks/asset_sync.rake
... ...
@@ -1,6 +1,15 @@
  1
+namespace :assets do
  2
+  desc "Synchronize assets to S3"
  3
+  task :sync => :environment do
  4
+    AssetSync.sync
  5
+  end
  6
+end
  7
+
1 8
 if Rake::Task.task_defined?("assets:precompile:nondigest")
2 9
   Rake::Task["assets:precompile:nondigest"].enhance do
3  
-    AssetSync.sync
  10
+    # Conditional execution needs to be inside the enhance block because the enhance block
  11
+    # will get executed before yaml or Rails initializers.
  12
+    Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
4 13
   end
5 14
 else
6 15
   Rake::Task["assets:precompile"].enhance do
@@ -8,6 +17,6 @@ else
8 17
     # RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
9 18
     # assets environment in this case.
10 19
     # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
11  
-    AssetSync.sync
  20
+    Rake::Task["assets:sync"].invoke if AssetSync.config.run_on_precompile
12 21
   end
13 22
 end
8  spec/asset_sync_spec.rb
@@ -16,6 +16,10 @@
16 16
       end
17 17
     end
18 18
 
  19
+    it "should default to running on precompile" do
  20
+      AssetSync.config.run_on_precompile.should be_true
  21
+    end
  22
+
19 23
     it "should default AssetSync to enabled" do
20 24
       AssetSync.config.enabled?.should be_true
21 25
       AssetSync.enabled?.should be_true
@@ -70,6 +74,10 @@
70 74
       AssetSync.enabled?.should be_true
71 75
     end
72 76
 
  77
+    it "should configure run_on_precompile" do
  78
+      AssetSync.config.run_on_precompile.should be_false
  79
+    end
  80
+
73 81
     it "should configure aws_access_key_id" do
74 82
       AssetSync.config.aws_access_key_id.should == "xxxx"
75 83
     end
1  spec/aws_with_yml/config/asset_sync.yml
@@ -3,6 +3,7 @@ defaults: &defaults
3 3
   aws_access_key_id: "xxxx"
4 4
   aws_secret_access_key: "zzzz"
5 5
   region: "eu-west-1"
  6
+  run_on_precompile: false
6 7
 
7 8
 development:
8 9
   <<: *defaults

0 notes on commit cfb2cb2

Please sign in to comment.
Something went wrong with that request. Please try again.