ettu 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ettu.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'active_support/concern'
2
+ require 'active_support/ordered_options'
2
3
  require 'active_support/core_ext/object/blank'
3
4
  require 'active_support/core_ext/object/try'
4
5
 
@@ -14,13 +15,20 @@ class Ettu
14
15
  @@config.js = 'application.js'
15
16
  @@config.css = 'application.css'
16
17
  @@config.assets = []
18
+ @@config.template_digestor = if defined? ActionView::Digestor
19
+ ActionView::Digestor
20
+ else
21
+ nil
22
+ end
17
23
 
18
24
  def configure
19
25
  yield @@config
20
26
  end
21
27
  end
22
28
 
23
- def initialize(record_or_options = nil, additional_options = {})
29
+ def initialize(record_or_options = nil, additional_options = {}, controller = nil)
30
+ @controller = controller
31
+ @asset_etags = {}
24
32
  if record_or_options.is_a? Hash
25
33
  @record = nil
26
34
  @options = record_or_options
@@ -28,7 +36,6 @@ class Ettu
28
36
  @record = record_or_options
29
37
  @options = additional_options
30
38
  end
31
- @asset_etags = {}
32
39
  end
33
40
 
34
41
  def response_etag
@@ -39,8 +46,8 @@ class Ettu
39
46
  @options.fetch(:last_modified, @record.try(:updated_at))
40
47
  end
41
48
 
42
- def view_etag(view, format, lookup_context)
43
- @view_etag ||= view_digest(view, format, lookup_context)
49
+ def view_etag
50
+ @view_etag ||= view_digest
44
51
  end
45
52
 
46
53
  def js_etag
@@ -54,7 +61,7 @@ class Ettu
54
61
  end
55
62
 
56
63
  def asset_etags
57
- assets = @config.fetch(:assets, @@config.assets)
64
+ assets = @options.fetch(:assets, @@config.assets)
58
65
  [*assets].map { |asset| asset_etag(asset) }
59
66
  end
60
67
 
@@ -66,11 +73,11 @@ class Ettu
66
73
 
67
74
  # Jeremy Kemper
68
75
  # 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
76
+ def view_digest
77
+ @@config.template_digestor.digest(
78
+ "#{@controller.controller_name}/#{@controller.action_name}",
79
+ @controller.request.format.try(:to_sym),
80
+ @controller.lookup_context
74
81
  )
75
82
  end
76
83
 
@@ -6,13 +6,10 @@ class Ettu
6
6
  alias_method :old_fresh_when, :fresh_when
7
7
 
8
8
  def fresh_when(record_or_options, additional_options = {})
9
- ettu = Ettu.new(record_or_options, additional_options)
9
+ ettu = Ettu.new(record_or_options, additional_options, self)
10
10
 
11
11
  etags = [*ettu.response_etag]
12
- if view = ettu.options.fetch(:view, "#{controller_name}/#{action_name}")
13
- # TODO: This is ugly. How do we clean it up?
14
- etags << ettu.view_etag(view, request.format.try(:to_sym), lookup_context)
15
- end
12
+ etags << ettu.view_etag
16
13
  if request.format.try(:html?)
17
14
  etags << ettu.js_etag
18
15
  etags << ettu.css_etag
data/lib/ettu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Ettu
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/spec/ettu_spec.rb CHANGED
@@ -17,7 +17,7 @@ module Rails
17
17
  end
18
18
  end
19
19
 
20
- describe Ettu::Ettu do
20
+ describe Ettu do
21
21
  let(:record) { Record.new(DateTime.now) }
22
22
  let(:hash) { { etag: record, last_modified: DateTime.now, public: false, random: true } }
23
23
 
@@ -31,13 +31,23 @@ describe Ettu::Ettu do
31
31
 
32
32
  xit '#view_etag'
33
33
 
34
+
35
+ context '.configure' do
36
+
37
+ end
38
+
39
+
34
40
  context 'when given only a record' do
35
- subject(:ettu) { Ettu::Ettu.new(record) }
41
+ subject(:ettu) { Ettu.new(record) }
36
42
 
37
- it 'makes #options an empty hash' do
43
+ it 'makes #options an hash' do
38
44
  expect(ettu.options).to be_a(Hash)
39
45
  end
40
46
 
47
+ it 'makes #options an empty hash' do
48
+ expect(ettu.options).to be_empty
49
+ end
50
+
41
51
  it 'uses record as #response_etag' do
42
52
  expect(ettu.response_etag).to eq(record)
43
53
  end
@@ -48,7 +58,7 @@ describe Ettu::Ettu do
48
58
  end
49
59
 
50
60
  context 'when given only a hash' do
51
- subject(:ettu) { Ettu::Ettu.new(hash) }
61
+ subject(:ettu) { Ettu.new(hash) }
52
62
 
53
63
  it 'sets #options to that hash' do
54
64
  expect(ettu.options).to eq(hash)
@@ -65,7 +75,11 @@ describe Ettu::Ettu do
65
75
 
66
76
  context 'when given a record and hash' do
67
77
  let(:hash) { { public: true } }
68
- subject(:ettu) { Ettu::Ettu.new(record, hash) }
78
+ subject(:ettu) { Ettu.new(record, hash) }
79
+
80
+ it 'sets #options to the hash' do
81
+ expect(ettu.options).to eq(hash)
82
+ end
69
83
 
70
84
  describe '#response_etag' do
71
85
  it 'uses record as #response_etag' do
@@ -0,0 +1,8 @@
1
+ describe Ettu::FreshWhen do
2
+ context 'when given extra options' do
3
+ subject(:ettu) { Ettu.new(record) }
4
+
5
+ it "passes those options to Rails' fresh_when"
6
+ it "calls #old_fresh_when"
7
+ end
8
+ 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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -95,6 +95,7 @@ files:
95
95
  - lib/ettu/railtie.rb
96
96
  - lib/ettu/version.rb
97
97
  - spec/ettu_spec.rb
98
+ - spec/fresh_when_spec.rb
98
99
  - spec/spec_helper.rb
99
100
  homepage: http://github.com/cloudspace/ettu
100
101
  licenses:
@@ -123,4 +124,5 @@ specification_version: 3
123
124
  summary: Account for view code when using ETags.
124
125
  test_files:
125
126
  - spec/ettu_spec.rb
127
+ - spec/fresh_when_spec.rb
126
128
  - spec/spec_helper.rb