Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Declare what goes in to your ETags: asset versions, account ID, etc.

  • Loading branch information...
commit d7b62c2fb1de5cc9f7e398cc9e6c05dee6da2d84 0 parents
@jeremy jeremy authored
1  .rbenv-version
@@ -0,0 +1 @@
+1.9.3-p125
2  Gemfile
@@ -0,0 +1,2 @@
+source 'https://rubygems.org'
+gemspec
52 Gemfile.lock
@@ -0,0 +1,52 @@
+PATH
+ remote: .
+ specs:
+ etagger (1.0.0)
+ actionpack (>= 3.2)
+ activesupport (>= 3.2)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ actionpack (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ activesupport (3.2.3)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.3)
+ minitest (2.12.1)
+ multi_json (1.2.0)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rake (0.9.2.2)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ etagger!
+ minitest
+ rake
10 Rakefile
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'rake'
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.pattern = 'test/*_test.rb'
+end
17 etagger.gemspec
@@ -0,0 +1,17 @@
+Gem::Specification.new do |s|
+ s.name = 'etagger'
+ s.version = '1.0.0'
+ s.author = '37signals'
+ s.email = 'jeremy@37signals.com'
+ s.summary = 'Declare what goes in to your ETags: asset versions, account ID, etc.'
+
+ s.required_ruby_version = '>= 1.9'
+
+ s.add_dependency 'actionpack', '>= 3.2'
+ s.add_dependency 'activesupport', '>= 3.2'
+
+ s.add_development_dependency 'rake'
+ s.add_development_dependency 'minitest'
+
+ s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
+end
33 lib/etagger.rb
@@ -0,0 +1,33 @@
+require 'active_support/concern'
+require 'active_support/core_ext/class/attribute'
+
+module Etagger
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :etaggers
+ self.etaggers = []
+ end
+
+ module ClassMethods
+ def etag(&etagger)
+ self.etaggers += [etagger]
+ end
+ end
+
+ def fresh_when(record_or_options, additional_options = {})
+ super revise_etag(record_or_options, additional_options)
+ end
+
+ private
+ def revise_etag(options, additional_options = nil)
+ if !options.is_a?(Hash)
+ revise_etag({ etag: options, last_modified: options.try(:updated_at) }.merge(additional_options))
+ elsif etag = options[:etag]
+ etags = etaggers.map { |etagger| instance_exec &etagger }.compact
+ options.merge etag: [etag, *etags]
+ else
+ options
+ end
+ end
+end
11 lib/etagger/railtie.rb
@@ -0,0 +1,11 @@
+module Etagger
+ class Railtie < ::Rails::Railtie
+ initializer 'install' do
+ require 'etagger'
+
+ ActiveSupport.on_load :action_controller do
+ ActionController::Base.send :include, Etagger
+ end
+ end
+ end
+end
32 test/etagger_test.rb
@@ -0,0 +1,32 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'minitest/unit'
+MiniTest::Unit.autorun
+
+require 'etagger'
+
+class EtaggerTest < MiniTest::Unit::TestCase
+ class FakeController
+ module Freshness
+ def fresh_when(record_or_options, additional_options = nil)
+ record_or_options[:etag]
+ end
+ end
+ include Freshness
+
+ include Etagger
+ etag { nil }
+ etag { 'ab' }
+ etag { :cde }
+ etag { [:f] }
+ etag { nil }
+ end
+
+ def setup
+ @controller = FakeController.new
+ end
+
+ def test_revises_etag
+ assert_equal ['123', 'ab', :cde, [:f]], @controller.fresh_when(etag: '123')
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.