cloud_assets 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
+ ## cloud_assets
2
+
1
3
  cloud_assets enables a Rails app to make transparent use of
2
4
  assets on a remote server in an alternative technology.
3
5
 
4
- Principles and modes of operation
5
- ---------------------------------
6
+ ## Principles and modes of operation
6
7
 
7
8
  You can avoid incorporating static assets in your Rails
8
9
  app directly, if they exist somewhere else on the internet.
@@ -20,67 +21,128 @@ remote system, and rewrites URIs in such templates to
20
21
  point either to the local Rails system or a CDN, depending
21
22
  on configuration and utility.
22
23
 
23
- Installation
24
- ------------
24
+ ## Installation
25
+
26
+ Add cloud_assets to your Gemfile and bundle install, then
27
+ define the necessary configuration in an initializer,
28
+ e.g. config/initializers/cloud_assets.rb.
29
+
30
+ If you don't define the initializer, all the important
31
+ values will be read from the environment, which is extra
32
+ handy if you are running on Heroku.
33
+
34
+ This initializer will behave the same as no initializer;
35
+ customize to your taste.
25
36
 
26
- 1.
37
+ ```ruby
38
+ CloudAssets.setup do |config|
27
39
 
28
- Add cloud_assets to your Gemfile and bundle install, then
29
- define the following in your environment.
40
+ # Required: origin URI for remote assets, e.g. http://yourhost.com
41
+ config.origin = ENV['CLOUD_ASSET_ORIGIN']
30
42
 
31
- Required:
32
- CLOUD_ASSET_ORIGIN - Origin URI for remote assets
43
+ # If needed: HTTP Basic user for remote assets
44
+ config.user = ENV['CLOUD_ASSET_USER']
33
45
 
34
- Optional:
35
- CLOUD_ASSET_CDN - Content delivery network, if any. This
36
- is used to rewrite URIs for images and other large static
37
- assets to the CDN instead of Rails.
46
+ # If needed: HTTP Basic user for remote password
47
+ config.password = ENV['CLOUD_ASSET_PASSWORD']
38
48
 
39
- CLOUD_ASSET_USER, CLOUD_ASSET_PASSWORD - HTTP Basic
40
- credentials which will be used when retrieving assets from
41
- the cloud asset source.
49
+ # Rewrite URIs to use a CDN
50
+ config.cdn = ENV['CLOUD_ASSET_CDN']
42
51
 
43
- 2.
52
+ # Activate verbose logging
53
+ config.verbose = false
44
54
 
45
- Put a route at the bottom of your routes.rb:
55
+ end
56
+ ```
46
57
 
47
- match '*url' => 'cloud_assets#content'
58
+ Put a route at the bottom of your routes.rb:
48
59
 
49
- This will allow cloud_assets to handle anything Rails
50
- doesn't recognize.
60
+ ```ruby
61
+ match '*url' => 'cloud_assets#content'
62
+ ```
51
63
 
52
- 3.
64
+ This will allow cloud_assets to handle anything Rails
65
+ doesn't recognize.
53
66
 
54
- In application_controller.rb:
67
+ In application_controller.rb:
55
68
 
56
- include CloudAssets
69
+ ```ruby
70
+ include CloudAssets
71
+ ```
57
72
 
58
- Additional Configuration
59
- ------------------------
73
+ ## Additional Configuration
60
74
 
61
75
  If you are serving any non-trivial amount of remotely
62
76
  sourced assets out of your Rails system, you'll want a cache.
63
- cloud_assets likes memcached via dalli.
77
+ FIXME: The cache currently uses dalli only. This should be
78
+ and can be configured more flexibly in the initializer.
64
79
 
65
- To enable it, just define $hydra_cache somewhere in one of
80
+ To enable it, just define $dalli_cache somewhere in one of
66
81
  your initializers.
67
82
 
68
- Usage
69
- -----
83
+ ## Usage
70
84
 
71
85
  Set a remote layout for an HTML view by defining the URI to
72
86
  the layout on the remote system, e.g.
73
87
 
74
- set_remote_layout '/templates/foo.html'
88
+ ```ruby
89
+ set_remote_layout '/templates/foo.html'
90
+ ```
75
91
 
76
92
  You can inject additional content into the elements of that
77
93
  template, or override the interior of its elements, using CSS
78
94
  selectors:
79
95
 
80
- inject_into_remote_layout '#notice' => flash[:notice]
81
- override_remote_layout 'body' => yield
96
+ ```ruby
97
+ inject_into_remote_layout '#notice' => flash[:notice]
98
+ override_remote_layout 'body' => yield
99
+ replace_remote_layout '#unwanted_stuff' => 'wanted stuff'
100
+ remove_remote_layout '.unwanted_classes'
101
+ ```
82
102
 
83
103
  Finally, obtain and show the result (complete with rewrites
84
104
  of CDN URLs and so on) with:
85
105
 
86
- apply_remote_layout
106
+ ```ruby
107
+ apply_remote_layout
108
+ ```
109
+
110
+ So a complete application layout would look something like this (Haml):
111
+
112
+ ```
113
+ - set_default_remote_layout '/about/'
114
+ - inject_into_remote_layout 'head' => (render :partial => 'layouts/headers')
115
+ - unless yield.empty?
116
+ - override_remote_layout '#content' => yield
117
+ != apply_remote_layout
118
+ ```
119
+
120
+ This loads the HTML of the remote "about" page, rewrites references to
121
+ localhost or the CDN as appropriate, injects Rails headers form the
122
+ headers partial into the HTML head element, and replaces the interior
123
+ of the element whose id is "content" -- say it's a div -- with the
124
+ yield of the Rails view that uses this layout.
125
+
126
+ ## Fixups
127
+
128
+ The remote site likely was not designed to have its HTML repurposed and
129
+ rewritten on other sites. Sites following best practices *should* not
130
+ need any manual fixups. However, any number of hacks might produce HTML
131
+ that cloud_assets does not know how to rewrite. You have an opportunity
132
+ to fix these by adding a monkey-patch to your initializer:
133
+
134
+ ```ruby
135
+ module CloudAssets
136
+ def self.fixup_html(html)
137
+ html.gsub 'something bad', 'something good'
138
+ end
139
+ end
140
+ ```
141
+
142
+ This method is applied immediately before the HTML is delivered to the
143
+ browser; you can do anything here that you like, but it should be
144
+ regarded as a hack. If the uncorrectable code is standards-compliant
145
+ and best practice, it would be good to submit a pull request so
146
+ cloud_assets can handle it. If not, it would be ideal to fix the
147
+ remote asset source -- e.g. remove hard-coded references or eliminate
148
+ unwarranted assumptions.
data/lib/cloud_assets.rb CHANGED
@@ -153,6 +153,17 @@ module CloudAssets
153
153
  @overrides.merge! hash
154
154
  end
155
155
 
156
+ def replace_remote_layout(hash)
157
+ if @replacements.nil?
158
+ @replacements = {}
159
+ end
160
+ @replacements.merge! hash
161
+ end
162
+
163
+ def remove_remote_layout(selector)
164
+ replace_remote_layout(selector => '')
165
+ end
166
+
156
167
  def set_remote_layout(layout)
157
168
  @remote_layout = layout
158
169
  end
@@ -177,6 +188,15 @@ module CloudAssets
177
188
  else
178
189
  doc = optimized_html_for @remote_layout
179
190
  end
191
+ unless @replacements.nil?
192
+ @replacements.each do |key, value|
193
+ begin
194
+ doc.at_css(key).replace(value)
195
+ rescue
196
+ puts "Failed to replace template element: #{key}"
197
+ end
198
+ end
199
+ end
180
200
  unless @overrides.nil?
181
201
  @overrides.each do |key, value|
182
202
  begin
@@ -213,6 +233,8 @@ module CloudAssets
213
233
  include ControllerMethods
214
234
  helper_method :inject_into_remote_layout
215
235
  helper_method :override_remote_layout
236
+ helper_method :replace_remote_layout
237
+ helper_method :remove_remote_layout
216
238
  helper_method :set_remote_layout
217
239
  helper_method :set_default_remote_layout
218
240
  helper_method :apply_remote_layout
@@ -1,3 +1,3 @@
1
1
  module CloudAssets
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-26 00:00:00.000000000 Z
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
16
- requirement: &70144157060340 !ruby/object:Gem::Requirement
16
+ requirement: &70170673604560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70144157060340
24
+ version_requirements: *70170673604560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &70144157059580 !ruby/object:Gem::Requirement
27
+ requirement: &70170673603960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70144157059580
35
+ version_requirements: *70170673603960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: dalli
38
- requirement: &70144157058760 !ruby/object:Gem::Requirement
38
+ requirement: &70170673602980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70144157058760
46
+ version_requirements: *70170673602980
47
47
  description: ! "\n This gem is in use in some production sites to provide\n backing
48
48
  for a Rails app using content from WordPress and\n PostLaunch (a Java based CMS),
49
49
  and has specific\n dependencies on Typhoeus, Nokogiri, and dalli, favorites\n