ettu 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,15 +33,15 @@ Rails 4 ETags can be used in the following way:
33
33
 
34
34
  ```ruby
35
35
  class ProductsController < ApplicationController
36
- def show
37
- @product = Product.find(params[:id])
36
+ def show
37
+ @product = Product.find(params[:id])
38
38
 
39
- # Sugar syntax
40
- fresh_when @product
39
+ # Sugar syntax
40
+ fresh_when @product
41
41
 
42
- # Hash syntax
43
- fresh_when etag: @product, last_modified: @product.updated_at, public: true
44
- end
42
+ # Hash syntax
43
+ fresh_when etag: @product, last_modified: @product.updated_at, public: true
44
+ end
45
45
  end
46
46
  ```
47
47
 
@@ -53,23 +53,44 @@ browser.
53
53
 
54
54
  ### Configuring
55
55
 
56
- Of course, you can override Ettu's behavior. Using a custom js file?
57
- Just pass in like so:
56
+ Ettu can be disabled for any environment using the `ettu.disabled`
57
+ config option.
58
58
 
59
- fresh_when @product, js: 'custom.js'
59
+ ```ruby
60
+ # config/environments/*.rb
61
+ My::Application.configure do
62
+ config.ettu.disabled = true
63
+ end
64
+ ```
60
65
 
61
- The same things goes for custom css files:
66
+ Of course, you can override Ettu's default behavior:
62
67
 
63
- fresh_when @product, css: 'custom.css'
68
+ ```ruby
69
+ # config/initializers/ettu.rb
70
+ Ettu.configure do |config|
71
+ # Set the default js file
72
+ config.js = 'app.js'
73
+ # Or don't account for javascript
74
+ # config.js = false
75
+
76
+ # Set the default css file
77
+ config.css = 'style.css'
78
+ # Or don't account for css
79
+ # config.css = false
80
+
81
+ # Add in extra assets to account for
82
+ config.assets = ['first.js', 'second.css']
83
+ end
84
+ ```
64
85
 
65
- Have several other assets you want to account for? Pass them in as an
66
- array with the `assets` key:
86
+ Or each can be passed on an individual basis:
67
87
 
68
- fresh_when @product, assets: ['first.css', 'second.js']
88
+ fresh_when @product, js: 'app.js', css: 'style.css', assets: 'super.css'
69
89
 
70
- You can specify a template with the `view` key:
90
+ Additionally, you can specify a different template to calculate with the
91
+ `view` option:
71
92
 
72
- fresh_when @product, "products/index"
93
+ fresh_when @product, view: "products/index"
73
94
 
74
95
  You can even stop Ettu from accounting for any of them by setting the
75
96
  value to `false`:
@@ -91,17 +112,12 @@ while in the **development** environment. This is not an issue that
91
112
  affects staging or production, because the template cache will be
92
113
  recreated after each deploy.
93
114
 
94
- In the mean time, you can monkey-patch around this:
115
+ In the mean time, you can enable a monkey-patch with:
95
116
 
96
117
  ```ruby
97
118
  # config/environments/development.rb
98
- class BlackHole < Hash
99
- def []=(k, v); end
100
- end
101
- module ActionView
102
- class Digestor
103
- @@cache = BlackHole.new
104
- end
119
+ My::Application.configure do
120
+ config.ettu.development_hack = true
105
121
  end
106
122
  ```
107
123
 
@@ -1,4 +1,4 @@
1
- module Ettu
1
+ class Ettu
2
2
  module FreshWhen
3
3
  extend ActiveSupport::Concern
4
4
 
@@ -17,8 +17,7 @@ module Ettu
17
17
  etags << ettu.js_etag
18
18
  etags << ettu.css_etag
19
19
  end
20
- assets = ettu.options.fetch(:assets, []).map { |asset| ettu.asset_etag(asset) }
21
- etags.concat assets
20
+ etags.concat ettu.asset_etags
22
21
 
23
22
  ettu_params = {etag: etags, last_modified: ettu.last_modified}
24
23
 
data/lib/ettu/railtie.rb CHANGED
@@ -1,10 +1,26 @@
1
- module Ettu
1
+ class Ettu
2
2
  class Railtie < Rails::Railtie
3
- initializer 'install' do
4
- require 'ettu'
3
+ config.ettu = ActiveSupport::OrderedOptions.new
4
+
5
+ initializer 'load' do |app|
6
+
7
+ unless app.config.ettu.disabled
8
+ require 'ettu'
9
+ ActiveSupport.on_load :action_controller do
10
+ ActionController::Base.send :include, FreshWhen
11
+ end
12
+
13
+ if app.config.ettu.development_hack
14
+ class BlackHole < Hash
15
+ def []=(k, v); end
16
+ end
17
+ module ::ActionView
18
+ class Digestor
19
+ @@cache = BlackHole.new
20
+ end
21
+ end
22
+ end
5
23
 
6
- ActiveSupport.on_load :action_controller do
7
- ActionController::Base.send :include, FreshWhen
8
24
  end
9
25
  end
10
26
  end
data/lib/ettu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Ettu
2
- VERSION = '0.0.3'
1
+ class Ettu
2
+ VERSION = '0.0.4'
3
3
  end
data/lib/ettu.rb CHANGED
@@ -3,6 +3,86 @@ require 'active_support/core_ext/object/blank'
3
3
  require 'active_support/core_ext/object/try'
4
4
 
5
5
  require 'ettu/version'
6
- require 'ettu/ettu'
7
6
  require 'ettu/fresh_when'
8
7
  require 'ettu/railtie' if defined? Rails
8
+
9
+ class Ettu
10
+ attr_reader :options
11
+
12
+ class << self
13
+ @@config = ActiveSupport::OrderedOptions.new
14
+ @@config.js = 'application.js'
15
+ @@config.css = 'application.css'
16
+ @@config.assets = []
17
+
18
+ def configure
19
+ yield @@config
20
+ end
21
+ end
22
+
23
+ def initialize(record_or_options = nil, additional_options = {})
24
+ if record_or_options.is_a? Hash
25
+ @record = nil
26
+ @options = record_or_options
27
+ else
28
+ @record = record_or_options
29
+ @options = additional_options
30
+ end
31
+ @asset_etags = {}
32
+ end
33
+
34
+ def response_etag
35
+ @options.fetch(:etag, @record)
36
+ end
37
+
38
+ def last_modified
39
+ @options.fetch(:last_modified, @record.try(:updated_at))
40
+ end
41
+
42
+ def view_etag(view, format, lookup_context)
43
+ @view_etag ||= view_digest(view, format, lookup_context)
44
+ end
45
+
46
+ def js_etag
47
+ js = @options.fetch(:js, @@config.js)
48
+ asset_etag js
49
+ end
50
+
51
+ def css_etag
52
+ css = @options.fetch(:css, @@config.css)
53
+ asset_etag css
54
+ end
55
+
56
+ def asset_etags
57
+ assets = @config.fetch(:assets, @@config.assets)
58
+ [*assets].map { |asset| asset_etag(asset) }
59
+ end
60
+
61
+ private
62
+
63
+ def asset_etag(asset)
64
+ @asset_etags[asset] ||= asset_digest(asset)
65
+ end
66
+
67
+ # Jeremy Kemper
68
+ # https://gist.github.com/jeremy/4211803
69
+ def view_digest(view, format, lookup_context)
70
+ ActionView::Digestor.digest(
71
+ view,
72
+ format,
73
+ lookup_context
74
+ )
75
+ end
76
+
77
+ # Jeremy Kemper
78
+ # https://gist.github.com/jeremy/4211803
79
+ # Check precompiled asset manifest (production) or compute the digest (dev).
80
+ def asset_digest(asset)
81
+ return nil unless asset.present?
82
+ if manifest = Rails.application.config.assets.digests
83
+ manifest[asset]
84
+ else
85
+ Rails.application.assets[asset].digest
86
+ end
87
+ end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ettu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -91,7 +91,6 @@ files:
91
91
  - Rakefile
92
92
  - ettu.gemspec
93
93
  - lib/ettu.rb
94
- - lib/ettu/ettu.rb
95
94
  - lib/ettu/fresh_when.rb
96
95
  - lib/ettu/railtie.rb
97
96
  - lib/ettu/version.rb
data/lib/ettu/ettu.rb DELETED
@@ -1,66 +0,0 @@
1
- module Ettu
2
- class Ettu
3
- attr_reader :options
4
-
5
- def initialize(record_or_options = nil, additional_options = {})
6
- if record_or_options.is_a? Hash
7
- @record = nil
8
- @options = record_or_options
9
- else
10
- @record = record_or_options
11
- @options = additional_options
12
- end
13
- @asset_etags = {}
14
- end
15
-
16
- def response_etag
17
- @options.fetch(:etag, @record)
18
- end
19
-
20
- def last_modified
21
- @options.fetch(:last_modified, @record.try(:updated_at))
22
- end
23
-
24
- def view_etag(view, format, lookup_context)
25
- @view_etag ||= view_digest(view, format, lookup_context)
26
- end
27
-
28
- def asset_etag(asset)
29
- @asset_etags[asset] ||= asset_digest(asset)
30
- end
31
-
32
- def js_etag
33
- js = @options.fetch(:js, 'application.js')
34
- asset_etag js
35
- end
36
-
37
- def css_etag
38
- css = @options.fetch(:css, 'application.css')
39
- asset_etag css
40
- end
41
-
42
- private
43
-
44
- # Jeremy Kemper
45
- # https://gist.github.com/jeremy/4211803
46
- def view_digest(view, format, lookup_context)
47
- ActionView::Digestor.digest(
48
- view,
49
- format,
50
- lookup_context
51
- )
52
- end
53
-
54
- # Jeremy Kemper
55
- # https://gist.github.com/jeremy/4211803
56
- # Check precompiled asset manifest (production) or compute the digest (dev).
57
- def asset_digest(asset)
58
- return nil unless asset.present?
59
- if manifest = Rails.application.config.assets.digests
60
- manifest[asset]
61
- else
62
- Rails.application.assets[asset].digest
63
- end
64
- end
65
- end
66
- end