diff -Nru ruby-actionpack-page-caching-1.0.2/actionpack-page_caching.gemspec ruby-actionpack-page-caching-1.1.0/actionpack-page_caching.gemspec --- ruby-actionpack-page-caching-1.0.2/actionpack-page_caching.gemspec 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/actionpack-page_caching.gemspec 2018-06-26 18:41:59.000000000 +0000 @@ -1,20 +1,21 @@ -# -*- encoding: utf-8 -*- - Gem::Specification.new do |gem| - gem.name = 'actionpack-page_caching' - gem.version = '1.0.2' - gem.author = 'David Heinemeier Hansson' - gem.email = 'david@loudthinking.com' - gem.description = 'Static page caching for Action Pack (removed from core in Rails 4.0)' - gem.summary = 'Static page caching for Action Pack (removed from core in Rails 4.0)' - gem.homepage = 'https://github.com/rails/actionpack-page_caching' + gem.name = "actionpack-page_caching" + gem.version = "1.1.0" + gem.author = "David Heinemeier Hansson" + gem.email = "david@loudthinking.com" + gem.description = "Static page caching for Action Pack (removed from core in Rails 4.0)" + gem.summary = "Static page caching for Action Pack (removed from core in Rails 4.0)" + gem.homepage = "https://github.com/rails/actionpack-page_caching" + gem.license = "MIT" + gem.required_ruby_version = '>= 1.9.3' gem.files = `git ls-files`.split($/) - gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.require_paths = ['lib'] + gem.require_paths = ["lib"] + gem.license = 'MIT' - gem.add_dependency 'actionpack', '>= 4.0.0', '< 5' + gem.add_dependency "actionpack", ">= 4.0.0", "< 6" - gem.add_development_dependency 'mocha' + gem.add_development_dependency "mocha" end diff -Nru ruby-actionpack-page-caching-1.0.2/CHANGELOG.md ruby-actionpack-page-caching-1.1.0/CHANGELOG.md --- ruby-actionpack-page-caching-1.0.2/CHANGELOG.md 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/CHANGELOG.md 2018-06-26 18:41:59.000000000 +0000 @@ -1,11 +1,36 @@ -# 1.0.2 +## 1.1.0 (January 23, 2017) -* Fix load order problem with other gems. +* Support dynamic `page_cache_directory` using a Proc, Symbol or callable -# 1.0.1 + *Andrew White* -* Add Railtie to set `page_cache_directory` by default to `public` folder. (Fixes #5) +* Support instance level setting of `page_cache_directory` -# 1.0.0 + *Andrew White* -* First Release +* Add support for Rails 5.0 and master + + *Andrew White* + + +## 1.0.2 (November 15, 2013) + +* Fix load order problem with other gems. + + *Rafael Mendonça França* + + +## 1.0.1 (October 24, 2013) + +* Add Railtie to set `page_cache_directory` by default to `public` folder. + + Fixes #5. + + *Žiga Vidic* + + +## 1.0.0 (February 27, 2013) + +* Extract Action Pack - Action Caching from Rails core. + + *Francesco Rodriguez*, *Rafael Mendonça França*, *Michiel Sikkes* diff -Nru ruby-actionpack-page-caching-1.0.2/.codeclimate.yml ruby-actionpack-page-caching-1.1.0/.codeclimate.yml --- ruby-actionpack-page-caching-1.0.2/.codeclimate.yml 1970-01-01 00:00:00.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/.codeclimate.yml 2018-06-26 18:41:59.000000000 +0000 @@ -0,0 +1,7 @@ +engines: + rubocop: + enabled: true + +ratings: + paths: + - "**.rb" diff -Nru ruby-actionpack-page-caching-1.0.2/debian/changelog ruby-actionpack-page-caching-1.1.0/debian/changelog --- ruby-actionpack-page-caching-1.0.2/debian/changelog 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/changelog 2018-10-15 17:50:12.000000000 +0000 @@ -1,6 +1,21 @@ +ruby-actionpack-page-caching (1.1.0-1) unstable; urgency=medium + + * Team upload + + [ Manas Kashyap ] + * watch file directed to gemwatch.debian.net + * VCS directed to salsa.debian.org + * standard version updated + * debhelper bumped + + [ Sruthi Chandran ] + * Refresh Bundler-Dependency patch and remove Fix-Failing-Tests.patch already + applied upstream + + -- Sruthi Chandran Mon, 15 Oct 2018 23:20:12 +0530 + ruby-actionpack-page-caching (1.0.2-4) unstable; urgency=medium - * Team upload. * Add debian/patches/Fix-Failing-Tests.patch from upstream to fix running tests against Rails 4.2 diff -Nru ruby-actionpack-page-caching-1.0.2/debian/compat ruby-actionpack-page-caching-1.1.0/debian/compat --- ruby-actionpack-page-caching-1.0.2/debian/compat 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/compat 2018-10-15 17:50:12.000000000 +0000 @@ -1 +1 @@ -7 +11 diff -Nru ruby-actionpack-page-caching-1.0.2/debian/control ruby-actionpack-page-caching-1.1.0/debian/control --- ruby-actionpack-page-caching-1.0.2/debian/control 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/control 2018-10-15 17:50:12.000000000 +0000 @@ -3,10 +3,15 @@ Priority: optional Maintainer: Debian Ruby Extras Maintainers Uploaders: Balasankar C -Build-Depends: debhelper (>= 7.0.50~), gem2deb, ruby-actionpack (>= 4.0.0), ruby-mocha, ruby-minitest, rake -Standards-Version: 3.9.6 -Vcs-Git: git://anonscm.debian.org/pkg-ruby-extras/ruby-actionpack-page-caching.git -Vcs-Browser: http://anonscm.debian.org/gitweb/?p=pkg-ruby-extras/ruby-actionpack-page-caching.git;a=summary +Build-Depends: debhelper (>= 11), + gem2deb, + rake, + ruby-actionpack (>= 4.0.0), + ruby-minitest, + ruby-mocha +Standards-Version: 4.2.1 +Vcs-Git: https://salsa.debian.org/ruby-team/ruby-actionpack-page-caching.git +Vcs-Browser: https://salsa.debian.org/ruby-team/ruby-actionpack-page-caching Homepage: https://github.com/rails/actionpack-page_caching Testsuite: autopkgtest-pkg-ruby XS-Ruby-Versions: all @@ -14,18 +19,21 @@ Package: ruby-actionpack-page-caching Architecture: all XB-Ruby-Versions: ${ruby:Versions} -Depends: ${shlibs:Depends}, ${misc:Depends}, ruby | ruby-interpreter, ruby-actionpack (>= 4.0.0) +Depends: ruby | ruby-interpreter, + ruby-actionpack (>= 4.0.0), + ${misc:Depends}, + ${shlibs:Depends} Breaks: ruby-actionpack-2.3 Replaces: ruby-actionpack-2.3 Description: static page caching for Action Pack (removed from core in Rails 4.0) - Page caching is an approach to caching where the entire action output of is - stored as a HTML file that the web server can serve without going through + Page caching is an approach to caching where the entire action output of is + stored as a HTML file that the web server can serve without going through Action Pack. . - This is the fastest way to cache your content as opposed to going dynamically - through the process of generating the content. Unfortunately, this incredible - speed-up is only available to stateless pages where all visitors are treated - the same. Content management systems -- including weblogs and wikis -- have - many pages that are a great fit for this approach, but account-based systems - where people log in and manipulate their own data are often less likely + This is the fastest way to cache your content as opposed to going dynamically + through the process of generating the content. Unfortunately, this incredible + speed-up is only available to stateless pages where all visitors are treated + the same. Content management systems -- including weblogs and wikis -- have + many pages that are a great fit for this approach, but account-based systems + where people log in and manipulate their own data are often less likely candidates. diff -Nru ruby-actionpack-page-caching-1.0.2/debian/patches/Bundler-Dependency ruby-actionpack-page-caching-1.1.0/debian/patches/Bundler-Dependency --- ruby-actionpack-page-caching-1.0.2/debian/patches/Bundler-Dependency 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/patches/Bundler-Dependency 2018-10-15 17:50:12.000000000 +0000 @@ -1,5 +1,5 @@ Description: Bundler Dependency - Removing bundler dependecy from test file. + Removing bundler dependency from test file. . ruby-actionpack-page-caching (1.0.2-1) UNRELEASED; urgency=medium . @@ -9,8 +9,8 @@ --- a/test/abstract_unit.rb +++ b/test/abstract_unit.rb @@ -1,4 +1,4 @@ --require 'bundler/setup' -+#require 'bundler/setup' - require 'minitest/autorun' - require 'action_controller' - require 'action_controller/page_caching' +-require "bundler/setup" ++#require "bundler/setup" + require "minitest/autorun" + require "action_controller" + require "action_controller/page_caching" diff -Nru ruby-actionpack-page-caching-1.0.2/debian/patches/Fix-Failing-Tests.patch ruby-actionpack-page-caching-1.1.0/debian/patches/Fix-Failing-Tests.patch --- ruby-actionpack-page-caching-1.0.2/debian/patches/Fix-Failing-Tests.patch 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/patches/Fix-Failing-Tests.patch 1970-01-01 00:00:00.000000000 +0000 @@ -1,22 +0,0 @@ -From 160aabc7f6138e6bef4779f9b555174fea5a487b Mon Sep 17 00:00:00 2001 -From: claudiob -Date: Tue, 6 Jan 2015 08:40:03 -0800 -Subject: [PATCH] Fix failing tests by requiring mocha - -Now that the rails gem points to 4.2, we need to require mocha in order -to use the `expects` syntax, otherwise tests will fail. See: -https://travis-ci.org/rails/actionpack-action_caching/jobs/46021642 ---- - test/caching_test.rb | 1 + - 1 file changed, 1 insertion(+) - -diff --git a/test/caching_test.rb b/test/caching_test.rb -index a25845a..7b20895 100644 ---- a/test/caching_test.rb -+++ b/test/caching_test.rb -@@ -1,4 +1,5 @@ - require 'abstract_unit' -+require 'mocha/setup' - - CACHE_DIR = 'test_cache' - # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed diff -Nru ruby-actionpack-page-caching-1.0.2/debian/patches/series ruby-actionpack-page-caching-1.1.0/debian/patches/series --- ruby-actionpack-page-caching-1.0.2/debian/patches/series 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/patches/series 2018-10-15 17:50:12.000000000 +0000 @@ -1,2 +1 @@ Bundler-Dependency -Fix-Failing-Tests.patch diff -Nru ruby-actionpack-page-caching-1.0.2/debian/watch ruby-actionpack-page-caching-1.1.0/debian/watch --- ruby-actionpack-page-caching-1.0.2/debian/watch 2015-12-14 11:24:28.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/debian/watch 2018-10-15 17:50:12.000000000 +0000 @@ -1,2 +1,2 @@ version=3 -http://pkg-ruby-extras.alioth.debian.org/cgi-bin/gemwatch/actionpack-page_caching .*/actionpack-page_caching-(.*).tar.gz +https://gemwatch.debian.net/actionpack-page_caching .*/actionpack-page_caching-(.*).tar.gz diff -Nru ruby-actionpack-page-caching-1.0.2/Gemfile ruby-actionpack-page-caching-1.1.0/Gemfile --- ruby-actionpack-page-caching-1.0.2/Gemfile 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/Gemfile 2018-06-26 18:41:59.000000000 +0000 @@ -1,5 +1,5 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec -gem 'rails' +gem "rails" diff -Nru ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-0-stable ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-0-stable --- ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-0-stable 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-0-stable 2018-06-26 18:41:59.000000000 +0000 @@ -1,5 +1,6 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '..' +gemspec path: ".." -gem 'rails', github: 'rails/rails', branch: '4-0-stable' +gem "rails", github: "rails/rails", branch: "4-0-stable" +gem "mime-types", "< 3" diff -Nru ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-1-stable ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-1-stable --- ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-1-stable 1970-01-01 00:00:00.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-1-stable 2018-06-26 18:41:59.000000000 +0000 @@ -0,0 +1,6 @@ +source "https://rubygems.org" + +gemspec path: ".." + +gem "rails", github: "rails/rails", branch: "4-1-stable" +gem "mime-types", "< 3" diff -Nru ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-2-stable ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-2-stable --- ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-4-2-stable 1970-01-01 00:00:00.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-4-2-stable 2018-06-26 18:41:59.000000000 +0000 @@ -0,0 +1,10 @@ +source "https://rubygems.org" + +gemspec path: ".." + +gem "rails", github: "rails/rails", branch: "4-2-stable" +gem "mime-types", "< 3" + +if RUBY_VERSION < "2.1" + gem "nokogiri", "< 1.7" +end diff -Nru ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-5-0-stable ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-5-0-stable --- ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-5-0-stable 1970-01-01 00:00:00.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-5-0-stable 2018-06-26 18:41:59.000000000 +0000 @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gemspec path: ".." + +gem "rails", github: "rails/rails", branch: "5-0-stable" diff -Nru ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-edge ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-edge --- ruby-actionpack-page-caching-1.0.2/gemfiles/Gemfile-edge 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/gemfiles/Gemfile-edge 2018-06-26 18:41:59.000000000 +0000 @@ -1,5 +1,6 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '..' +gemspec path: ".." -gem 'rails', github: 'rails/rails', branch: 'master' +gem "rails", github: "rails/rails", branch: "master" +gem "arel", github: "rails/arel", branch: "master" diff -Nru ruby-actionpack-page-caching-1.0.2/.gitignore ruby-actionpack-page-caching-1.1.0/.gitignore --- ruby-actionpack-page-caching-1.0.2/.gitignore 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/.gitignore 2018-06-26 18:41:59.000000000 +0000 @@ -1,17 +1,5 @@ -*.gem -*.rbc -.bundle -.config -.yardoc +.ruby-version Gemfile.lock -InstalledFiles -_yardoc -coverage -doc/ -lib/bundler/man -pkg -rdoc -spec/reports +gemfiles/*.lock +pkg/* test/tmp -test/version_tmp -tmp diff -Nru ruby-actionpack-page-caching-1.0.2/lib/action_controller/caching/pages.rb ruby-actionpack-page-caching-1.1.0/lib/action_controller/caching/pages.rb --- ruby-actionpack-page-caching-1.0.2/lib/action_controller/caching/pages.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/lib/action_controller/caching/pages.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,5 +1,7 @@ -require 'fileutils' -require 'active_support/core_ext/class/attribute_accessors' +require "fileutils" +require "uri" +require "active_support/core_ext/class/attribute_accessors" +require "active_support/core_ext/string/strip" module ActionController module Caching @@ -32,8 +34,8 @@ # class WeblogController < ActionController::Base # def update # List.update(params[:list][:id], params[:list]) - # expire_page action: 'show', id: params[:list][:id] - # redirect_to action: 'show', id: params[:list][:id] + # expire_page action: "show", id: params[:list][:id] + # redirect_to action: "show", id: params[:list][:id] # end # end # @@ -51,7 +53,7 @@ # likely require configuring your web server to look in the new location for # cached files. class_attribute :page_cache_directory - self.page_cache_directory ||= '' + self.page_cache_directory ||= "" # The compression used for gzip. If +false+ (default), the page is not compressed. # If can be a symbol showing the ZLib compression method, for example, :best_compression @@ -60,33 +62,137 @@ self.page_cache_compression ||= false end + class PageCache #:nodoc: + def initialize(cache_directory, default_extension, controller = nil) + @cache_directory = cache_directory + @default_extension = default_extension + @controller = controller + end + + def expire(path) + instrument :expire_page, path do + delete(cache_path(path)) + end + end + + def cache(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION) + instrument :write_page, path do + write(content, cache_path(path, extension), gzip) + end + end + + private + def cache_directory + case @cache_directory + when Proc + handle_proc_cache_directory + when Symbol + handle_symbol_cache_directory + else + handle_default_cache_directory + end + end + + def handle_proc_cache_directory + if @controller + @controller.instance_exec(&@cache_directory) + else + raise_runtime_error + end + end + + def handle_symbol_cache_directory + if @controller + @controller.send(@cache_directory) + else + raise_runtime_error + end + end + + def handle_callable_cache_directory + if @controller + @cache_directory.call(@controller.request) + else + raise_runtime_error + end + end + + def handle_default_cache_directory + if @cache_directory.respond_to?(:call) + handle_callable_cache_directory + else + @cache_directory.to_s + end + end + + def raise_runtime_error + raise RuntimeError, <<-MSG.strip_heredoc + Dynamic page_cache_directory used with class-level cache_page method + + You have specified either a Proc, Symbol or callable object for page_cache_directory + which needs to be executed within the context of a request. If you need to call the + cache_page method from a class-level context then set the page_cache_directory to a + static value and override the setting at the instance-level using before_action. + MSG + end + + def default_extension + @default_extension + end + + def cache_file(path, extension) + if path.empty? || path == "/" + name = "/index" + else + name = URI.parser.unescape(path.chomp("/")) + end + + if File.extname(name).empty? + name + (extension || default_extension) + else + name + end + end + + def cache_path(path, extension = nil) + File.join(cache_directory, cache_file(path, extension)) + end + + def delete(path) + File.delete(path) if File.exist?(path) + File.delete(path + ".gz") if File.exist?(path + ".gz") + end + + def write(content, path, gzip) + FileUtils.makedirs(File.dirname(path)) + File.open(path, "wb+") { |f| f.write(content) } + + if gzip + Zlib::GzipWriter.open(path + ".gz", gzip) { |f| f.write(content) } + end + end + + def instrument(name, path) + ActiveSupport::Notifications.instrument("#{name}.action_controller", path: path) { yield } + end + end + module ClassMethods # Expires the page that was cached with the +path+ as a key. # - # expire_page '/lists/show' + # expire_page "/lists/show" def expire_page(path) - return unless perform_caching - path = page_cache_path(path) - - instrument_page_cache :expire_page, path do - File.delete(path) if File.exist?(path) - File.delete(path + '.gz') if File.exist?(path + '.gz') + if perform_caching + page_cache.expire(path) end end # Manually cache the +content+ in the key determined by +path+. # - # cache_page "I'm the cached content", '/lists/show' + # cache_page "I'm the cached content", "/lists/show" def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION) - return unless perform_caching - path = page_cache_path(path, extension) - - instrument_page_cache :write_page, path do - FileUtils.makedirs(File.dirname(path)) - File.open(path, 'wb+') { |f| f.write(content) } - if gzip - Zlib::GzipWriter.open(path + '.gz', gzip) { |f| f.write(content) } - end + if perform_caching + page_cache.cache(content, path, extension, gzip) end end @@ -105,60 +211,50 @@ # # don't gzip images # caches_page :image, gzip: false def caches_page(*actions) - return unless perform_caching - options = actions.extract_options! + if perform_caching + options = actions.extract_options! - gzip_level = options.fetch(:gzip, page_cache_compression) - gzip_level = case gzip_level - when Symbol - Zlib.const_get(gzip_level.upcase) - when Fixnum - gzip_level - when false - nil - else - Zlib::BEST_COMPRESSION - end + gzip_level = options.fetch(:gzip, page_cache_compression) + gzip_level = \ + case gzip_level + when Symbol + Zlib.const_get(gzip_level.upcase) + when Integer + gzip_level + when false + nil + else + Zlib::BEST_COMPRESSION + end - after_filter({only: actions}.merge(options)) do |c| - c.cache_page(nil, nil, gzip_level) + after_action({ only: actions }.merge(options)) do |c| + c.cache_page(nil, nil, gzip_level) + end end end private - def page_cache_file(path, extension) - name = (path.empty? || path == '/') ? '/index' : URI.parser.unescape(path.chomp('/')) - unless (name.split('/').last || name).include? '.' - name << (extension || self.default_static_extension) - end - return name - end - - def page_cache_path(path, extension = nil) - page_cache_directory.to_s + page_cache_file(path, extension) - end - - def instrument_page_cache(name, path) - ActiveSupport::Notifications.instrument("#{name}.action_controller", path: path){ yield } + def page_cache + PageCache.new(page_cache_directory, default_static_extension) end end # Expires the page that was cached with the +options+ as a key. # - # expire_page controller: 'lists', action: 'show' + # expire_page controller: "lists", action: "show" def expire_page(options = {}) - return unless self.class.perform_caching - - if options.is_a?(Hash) - if options[:action].is_a?(Array) - options[:action].each do |action| - self.class.expire_page(url_for(options.merge(only_path: true, action: action))) + if perform_caching? + case options + when Hash + case options[:action] + when Array + options[:action].each { |action| expire_page(options.merge(action: action)) } + else + page_cache.expire(url_for(options.merge(only_path: true))) end else - self.class.expire_page(url_for(options.merge(only_path: true))) + page_cache.expire(options) end - else - self.class.expire_page(options) end end @@ -166,29 +262,39 @@ # the contents of response.body is used. If no options are provided, the url of the current # request being handled is used. # - # cache_page "I'm the cached content", controller: 'lists', action: 'show' + # cache_page "I'm the cached content", controller: "lists", action: "show" def cache_page(content = nil, options = nil, gzip = Zlib::BEST_COMPRESSION) - return unless self.class.perform_caching && caching_allowed? + if perform_caching? && caching_allowed? + path = \ + case options + when Hash + url_for(options.merge(only_path: true, format: params[:format])) + when String + options + else + request.path + end - path = case options - when Hash - url_for(options.merge(only_path: true, format: params[:format])) - when String - options - else - request.path - end + if (type = Mime::LOOKUP[self.content_type]) && (type_symbol = type.symbol).present? + extension = ".#{type_symbol}" + end - if (type = Mime::LOOKUP[self.content_type]) && (type_symbol = type.symbol).present? - extension = ".#{type_symbol}" + page_cache.cache(content || response.body, path, extension, gzip) end - - self.class.cache_page(content || response.body, path, extension, gzip) end def caching_allowed? (request.get? || request.head?) && response.status == 200 end + + def perform_caching? + self.class.perform_caching + end + + private + def page_cache + PageCache.new(page_cache_directory, default_static_extension, self) + end end end end diff -Nru ruby-actionpack-page-caching-1.0.2/lib/action_controller/page_caching.rb ruby-actionpack-page-caching-1.1.0/lib/action_controller/page_caching.rb --- ruby-actionpack-page-caching-1.0.2/lib/action_controller/page_caching.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/lib/action_controller/page_caching.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,4 +1,4 @@ -require 'action_controller/caching/pages' +require "action_controller/caching/pages" module ActionController module Caching diff -Nru ruby-actionpack-page-caching-1.0.2/lib/actionpack/page_caching/railtie.rb ruby-actionpack-page-caching-1.1.0/lib/actionpack/page_caching/railtie.rb --- ruby-actionpack-page-caching-1.0.2/lib/actionpack/page_caching/railtie.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/lib/actionpack/page_caching/railtie.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,16 +1,16 @@ -require 'rails/railtie' +require "rails/railtie" module ActionPack module PageCaching class Railtie < Rails::Railtie - initializer 'action_pack.page_caching' do + initializer "action_pack.page_caching" do ActiveSupport.on_load(:action_controller) do - require 'action_controller/page_caching' + require "action_controller/page_caching" end end - initializer 'action_pack.page_caching.set_config', before: 'action_controller.set_configs' do |app| - app.config.action_controller.page_cache_directory ||= app.config.paths['public'].first + initializer "action_pack.page_caching.set_config", before: "action_controller.set_configs" do |app| + app.config.action_controller.page_cache_directory ||= app.config.paths["public"].first end end end diff -Nru ruby-actionpack-page-caching-1.0.2/lib/actionpack/page_caching.rb ruby-actionpack-page-caching-1.1.0/lib/actionpack/page_caching.rb --- ruby-actionpack-page-caching-1.0.2/lib/actionpack/page_caching.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/lib/actionpack/page_caching.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1 +1 @@ -require 'actionpack/page_caching/railtie' +require "actionpack/page_caching/railtie" diff -Nru ruby-actionpack-page-caching-1.0.2/metadata.yml ruby-actionpack-page-caching-1.1.0/metadata.yml --- ruby-actionpack-page-caching-1.0.2/metadata.yml 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/metadata.yml 1970-01-01 00:00:00.000000000 +0000 @@ -1,96 +0,0 @@ ---- !ruby/object:Gem::Specification -name: actionpack-page_caching -version: !ruby/object:Gem::Version - version: 1.0.2 -platform: ruby -authors: -- David Heinemeier Hansson -autorequire: -bindir: bin -cert_chain: [] -date: 2013-11-15 00:00:00.000000000 Z -dependencies: -- !ruby/object:Gem::Dependency - name: actionpack - requirement: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: 4.0.0 - - - < - - !ruby/object:Gem::Version - version: '5' - type: :runtime - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: 4.0.0 - - - < - - !ruby/object:Gem::Version - version: '5' -- !ruby/object:Gem::Dependency - name: mocha - requirement: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: '0' - type: :development - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: '0' -description: Static page caching for Action Pack (removed from core in Rails 4.0) -email: david@loudthinking.com -executables: [] -extensions: [] -extra_rdoc_files: [] -files: -- .gitignore -- .travis.yml -- CHANGELOG.md -- Gemfile -- LICENSE.txt -- README.md -- Rakefile -- actionpack-page_caching.gemspec -- gemfiles/Gemfile-4-0-stable -- gemfiles/Gemfile-edge -- lib/action_controller/caching/pages.rb -- lib/action_controller/page_caching.rb -- lib/actionpack/page_caching.rb -- lib/actionpack/page_caching/railtie.rb -- test/abstract_unit.rb -- test/caching_test.rb -- test/log_subscriber_test.rb -homepage: https://github.com/rails/actionpack-page_caching -licenses: [] -metadata: {} -post_install_message: -rdoc_options: [] -require_paths: -- lib -required_ruby_version: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: '0' -required_rubygems_version: !ruby/object:Gem::Requirement - requirements: - - - '>=' - - !ruby/object:Gem::Version - version: '0' -requirements: [] -rubyforge_project: -rubygems_version: 2.0.3 -signing_key: -specification_version: 4 -summary: Static page caching for Action Pack (removed from core in Rails 4.0) -test_files: -- test/abstract_unit.rb -- test/caching_test.rb -- test/log_subscriber_test.rb diff -Nru ruby-actionpack-page-caching-1.0.2/Rakefile ruby-actionpack-page-caching-1.1.0/Rakefile --- ruby-actionpack-page-caching-1.0.2/Rakefile 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/Rakefile 2018-06-26 18:41:59.000000000 +0000 @@ -1,11 +1,11 @@ #!/usr/bin/env rake -require 'bundler/gem_tasks' -require 'rake/testtask' +require "bundler/gem_tasks" +require "rake/testtask" Rake::TestTask.new do |t| - t.libs = ['test'] - t.pattern = 'test/**/*_test.rb' - t.ruby_opts = ['-w'] + t.libs = ["test"] + t.pattern = "test/**/*_test.rb" + t.ruby_opts = ["-w"] end task default: :test diff -Nru ruby-actionpack-page-caching-1.0.2/README.md ruby-actionpack-page-caching-1.1.0/README.md --- ruby-actionpack-page-caching-1.0.2/README.md 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/README.md 2018-06-26 18:41:59.000000000 +0000 @@ -3,14 +3,14 @@ Static page caching for Action Pack (removed from core in Rails 4.0). -**NOTE:** It will continue to be officially maintained until Rails 4.1. - Installation ------------ Add this line to your application's Gemfile: - gem 'actionpack-page_caching' +``` ruby +gem "actionpack-page_caching" +``` And then execute: @@ -23,7 +23,7 @@ Usage ----- -Page caching is an approach to caching where the entire action output of is +Page caching is an approach to caching where the entire action output is stored as a HTML file that the web server can serve without going through Action Pack. This is the fastest way to cache your content as opposed to going dynamically through the process of generating the content. Unfortunately, this @@ -34,13 +34,52 @@ First you need to set `page_cache_directory` in your configuration file: - config.action_controller.page_cache_directory = "#{Rails.root.to_s}/public/deploy" +``` ruby +config.action_controller.page_cache_directory = "#{Rails.root}/public/cached_pages" +``` + +The `page_cache_directory` setting can be used with a Proc: + +``` ruby +class WeblogController < ApplicationController + self.page_cache_directory = -> { Rails.root.join("public", request.domain) } +end +``` + +a Symbol: + +``` ruby +class WeblogController < ApplicationController + self.page_cache_directory = :domain_cache_directory + + private + def domain_cache_directory + Rails.root.join("public", request.domain) + end +end +``` + +or a callable object: + +``` ruby +class DomainCacheDirectory + def self.call(request) + Rails.root.join("public", request.domain) + end +end + +class WeblogController < ApplicationController + self.page_cache_directory = DomainCacheDirectory +end +``` Specifying which actions to cache is done through the `caches_page` class method: - class WeblogController < ActionController::Base - caches_page :show, :new - end +``` ruby +class WeblogController < ActionController::Base + caches_page :show, :new +end +``` This will generate cache files such as `weblog/show/5.html` and `weblog/new.html`, which match the URLs used that would normally trigger @@ -54,17 +93,23 @@ hit is made against it. The API for doing so mimics the options from `url_for` and friends: - class WeblogController < ActionController::Base - def update - List.update(params[:list][:id], params[:list]) - expire_page action: 'show', id: params[:list][:id] - redirect_to action: 'show', id: params[:list][:id] - end - end +``` ruby +class WeblogController < ActionController::Base + def update + List.update(params[:list][:id], params[:list]) + expire_page action: "show", id: params[:list][:id] + redirect_to action: "show", id: params[:list][:id] + end +end +``` Additionally, you can expire caches using [Sweepers](https://github.com/rails/rails-observers#action-controller-sweeper) that act on changes in the model to determine when a cache is supposed to be expired. +Finally, configure your web server to serve these static pages when they are present +rather than the original files. See the [project wiki][1] for example configurations. +[1]: https://github.com/rails/actionpack-page_caching/wiki + Contributing ------------ @@ -77,5 +122,5 @@ Code Status ----------- -* [![Build Status](https://travis-ci.org/rails/actionpack-page_caching.png?branch=master)](https://travis-ci.org/rails/page_caching) -* [![Dependency Status](https://gemnasium.com/rails/actionpack-page_caching.png)](https://gemnasium.com/rails/actionpack-page_caching) +* [![Build Status](https://travis-ci.org/rails/actionpack-page_caching.svg?branch=master)](https://travis-ci.org/rails/actionpack-page_caching) +* [![Dependency Status](https://gemnasium.com/rails/actionpack-page_caching.svg)](https://gemnasium.com/rails/actionpack-page_caching) diff -Nru ruby-actionpack-page-caching-1.0.2/.rubocop.yml ruby-actionpack-page-caching-1.1.0/.rubocop.yml --- ruby-actionpack-page-caching-1.0.2/.rubocop.yml 1970-01-01 00:00:00.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/.rubocop.yml 2018-06-26 18:41:59.000000000 +0000 @@ -0,0 +1,116 @@ +AllCops: + TargetRubyVersion: 2.2 + # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop + # to ignore them, so only the ones explicitly set in this file are enabled. + DisabledByDefault: true + +# Prefer &&/|| over and/or. +Style/AndOr: + Enabled: true + +# Do not use braces for hash literals when they are the last argument of a +# method call. +Style/BracesAroundHashParameters: + Enabled: true + +# Align `when` with `case`. +Style/CaseIndentation: + Enabled: true + +# Align comments with method definitions. +Style/CommentIndentation: + Enabled: true + +# No extra empty lines. +Style/EmptyLines: + Enabled: true + +# In a regular class definition, no empty lines around the body. +Style/EmptyLinesAroundClassBody: + Enabled: true + +# In a regular module definition, no empty lines around the body. +Style/EmptyLinesAroundModuleBody: + Enabled: true + +# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }. +Style/HashSyntax: + Enabled: true + +# Method definitions after `private` or `protected` isolated calls need one +# extra level of indentation. +Style/IndentationConsistency: + Enabled: true + EnforcedStyle: rails + +# Two spaces, no tabs (for indentation). +Style/IndentationWidth: + Enabled: true + +Style/SpaceAfterColon: + Enabled: true + +Style/SpaceAfterComma: + Enabled: true + +Style/SpaceAroundEqualsInParameterDefault: + Enabled: true + +Style/SpaceAroundKeyword: + Enabled: true + +Style/SpaceAroundOperators: + Enabled: true + +Style/SpaceBeforeFirstArg: + Enabled: true + +# Defining a method with parameters needs parentheses. +Style/MethodDefParentheses: + Enabled: true + +# Use `foo {}` not `foo{}`. +Style/SpaceBeforeBlockBraces: + Enabled: true + +# Use `foo { bar }` not `foo {bar}`. +Style/SpaceInsideBlockBraces: + Enabled: true + +# Use `{ a: 1 }` not `{a:1}`. +Style/SpaceInsideHashLiteralBraces: + Enabled: true + +Style/SpaceInsideParens: + Enabled: true + +# Check quotes usage according to lint rule below. +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +# Detect hard tabs, no hard tabs. +Style/Tab: + Enabled: true + +# Blank lines should not have any spaces. +Style/TrailingBlankLines: + Enabled: true + +# No trailing whitespace. +Style/TrailingWhitespace: + Enabled: true + +# Use quotes for string literals when they are enough. +Style/UnneededPercentQ: + Enabled: true + +# Align `end` with the matching keyword or starting expression except for +# assignments, where it should be aligned with the LHS. +Lint/EndAlignment: + Enabled: true + AlignWith: variable + +# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg. +Lint/RequireParentheses: + Enabled: true diff -Nru ruby-actionpack-page-caching-1.0.2/test/abstract_unit.rb ruby-actionpack-page-caching-1.1.0/test/abstract_unit.rb --- ruby-actionpack-page-caching-1.0.2/test/abstract_unit.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/test/abstract_unit.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,22 +1,12 @@ -require 'bundler/setup' -require 'minitest/autorun' -require 'action_controller' -require 'action_controller/page_caching' +require "bundler/setup" +require "minitest/autorun" +require "action_controller" +require "action_controller/page_caching" -SharedTestRoutes = ActionDispatch::Routing::RouteSet.new - -module ActionController - class Base - include SharedTestRoutes.url_helpers - end - - class TestCase - def setup - @routes = SharedTestRoutes +if ActiveSupport.respond_to?(:test_order) + ActiveSupport.test_order = :random +end - @routes.draw do - get ':controller(/:action)' - end - end - end +if ActionController::Base.respond_to?(:enable_fragment_cache_logging=) + ActionController::Base.enable_fragment_cache_logging = true end diff -Nru ruby-actionpack-page-caching-1.0.2/test/caching_test.rb ruby-actionpack-page-caching-1.1.0/test/caching_test.rb --- ruby-actionpack-page-caching-1.0.2/test/caching_test.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/test/caching_test.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,12 +1,69 @@ -require 'abstract_unit' +require "abstract_unit" +require "mocha/setup" -CACHE_DIR = 'test_cache' -# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed -FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) +CACHE_DIR = "test_cache" +# Don't change "../tmp" cavalierly or you might hose something you don't want hosed +TEST_TMP_DIR = File.expand_path("../tmp", __FILE__) +FILE_STORE_PATH = File.join(TEST_TMP_DIR, CACHE_DIR) + +module PageCachingTestHelpers + def setup + super + + @routes = ActionDispatch::Routing::RouteSet.new + + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) + FileUtils.mkdir_p(FILE_STORE_PATH) + end + + def teardown + super + + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) + @controller.perform_caching = false + end + + private + + def assert_page_cached(action, options = {}) + expected = options[:content] || action.to_s + path = cache_file(action, options) + + assert File.exist?(path), "The cache file #{path} doesn't exist" + + if File.extname(path) == ".gz" + actual = Zlib::GzipReader.open(path) { |f| f.read } + else + actual = File.read(path) + end + + assert_equal expected, actual, "The cached content doesn't match the expected value" + end + + def assert_page_not_cached(action, options = {}) + path = cache_file(action, options) + assert !File.exist?(path), "The cache file #{path} still exists" + end + + def cache_file(action, options = {}) + path = options[:path] || FILE_STORE_PATH + controller = options[:controller] || self.class.name.underscore + format = options[:format] || "html" + + "#{path}/#{controller}/#{action}.#{format}" + end + + def draw(&block) + @routes = ActionDispatch::Routing::RouteSet.new + @routes.draw(&block) + @controller.extend(@routes.url_helpers) + end +end class CachingMetalController < ActionController::Metal abstract! + include AbstractController::Callbacks include ActionController::Caching self.page_cache_directory = FILE_STORE_PATH @@ -17,28 +74,22 @@ caches_page :ok def ok - self.response_body = 'ok' + self.response_body = "ok" end end class PageCachingMetalTest < ActionController::TestCase + include PageCachingTestHelpers tests PageCachingMetalTestController - def setup - super - - FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) - FileUtils.mkdir_p(FILE_STORE_PATH) - end - - def teardown - FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) - end - def test_should_cache_get_with_ok_status + draw do + get "/page_caching_metal_test/ok", to: "page_caching_metal_test#ok" + end + get :ok assert_response :ok - assert File.exist?("#{FILE_STORE_PATH}/page_caching_metal_test/ok.html"), 'get with ok status should have been cached' + assert_page_cached :ok end end @@ -48,6 +99,17 @@ abstract! self.cache_store = :file_store, FILE_STORE_PATH + + protected + if ActionPack::VERSION::STRING < "4.1" + def render(options) + if options.key?(:html) + super({ text: options.delete(:html) }.merge(options)) + else + super + end + end + end end class PageCachingTestController < CachingController @@ -61,7 +123,7 @@ caches_page :gzip_level, gzip: :best_speed def ok - head :ok + render html: "ok" end def no_content @@ -69,7 +131,7 @@ end def found - redirect_to action: 'ok' + redirect_to action: "ok" end def not_found @@ -77,170 +139,204 @@ end def custom_path - render text: 'Super soaker' - cache_page('Super soaker', '/index.html') + render html: "custom_path" + cache_page(nil, "/index.html") end def default_gzip - render text: 'Text' + render html: "default_gzip" end def no_gzip - render text: 'PNG' + render html: "no_gzip" end def gzip_level - render text: 'Big text' + render html: "gzip_level" end def expire_custom_path - expire_page('/index.html') + expire_page("/index.html") head :ok end def trailing_slash - render text: 'Sneak attack' + render html: "trailing_slash" end def about_me respond_to do |format| - format.html { render text: 'I am html' } - format.xml { render text: 'I am xml' } + format.html { render html: "I am html" } + format.xml { render xml: "I am xml" } end end end class PageCachingTest < ActionController::TestCase - def setup - super - - @request = ActionController::TestRequest.new - @request.host = 'hostname.com' - @request.env.delete('PATH_INFO') + include PageCachingTestHelpers + tests PageCachingTestController - @controller = PageCachingTestController.new - @controller.perform_caching = true - @controller.cache_store = :file_store, FILE_STORE_PATH - - @response = ActionController::TestResponse.new - - @params = { controller: 'posts', action: 'index', only_path: true } + def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route + draw do + get "posts.:format", to: "posts#index", as: :formatted_posts + get "/", to: "posts#index", as: :main + end - FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) - FileUtils.mkdir_p(FILE_STORE_PATH) - end + defaults = { controller: "posts", action: "index", only_path: true } - def teardown - FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) - @controller.perform_caching = false + assert_equal "/posts.rss", @routes.url_for(defaults.merge(format: "rss")) + assert_equal "/", @routes.url_for(defaults.merge(format: nil)) end - def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route - with_routing do |set| - set.draw do - get 'posts.:format', to: 'posts#index', as: :formatted_posts - get '/', to: 'posts#index', as: :main - end - @params[:format] = 'rss' - assert_equal '/posts.rss', @routes.url_for(@params) - @params[:format] = nil - assert_equal '/', @routes.url_for(@params) + def test_should_cache_head_with_ok_status + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" end - end - def test_should_cache_head_with_ok_status head :ok assert_response :ok - assert_page_cached :ok, 'head with ok status should have been cached' + assert_page_cached :ok end def test_should_cache_get_with_ok_status + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + end + get :ok assert_response :ok - assert_page_cached :ok, 'get with ok status should have been cached' + assert_page_cached :ok end def test_should_cache_with_custom_path + draw do + get "/page_caching_test/custom_path", to: "page_caching_test#custom_path" + end + get :custom_path - assert File.exist?("#{FILE_STORE_PATH}/index.html") + assert_page_cached :index, controller: ".", content: "custom_path" end def test_should_expire_cache_with_custom_path + draw do + get "/page_caching_test/custom_path", to: "page_caching_test#custom_path" + get "/page_caching_test/expire_custom_path", to: "page_caching_test#expire_custom_path" + end + get :custom_path - assert File.exist?("#{FILE_STORE_PATH}/index.html") + assert_page_cached :index, controller: ".", content: "custom_path" get :expire_custom_path - assert !File.exist?("#{FILE_STORE_PATH}/index.html") + assert_page_not_cached :index, controller: ".", content: "custom_path" end def test_should_gzip_cache + draw do + get "/page_caching_test/custom_path", to: "page_caching_test#custom_path" + get "/page_caching_test/expire_custom_path", to: "page_caching_test#expire_custom_path" + end + get :custom_path - assert File.exist?("#{FILE_STORE_PATH}/index.html.gz") + assert_page_cached :index, controller: ".", format: "html.gz", content: "custom_path" get :expire_custom_path - assert !File.exist?("#{FILE_STORE_PATH}/index.html.gz") + assert_page_not_cached :index, controller: ".", format: "html.gz" end def test_should_allow_to_disable_gzip + draw do + get "/page_caching_test/no_gzip", to: "page_caching_test#no_gzip" + end + get :no_gzip - assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/no_gzip.html") - assert !File.exist?("#{FILE_STORE_PATH}/page_caching_test/no_gzip.html.gz") + assert_page_cached :no_gzip, format: "html" + assert_page_not_cached :no_gzip, format: "html.gz" end def test_should_use_config_gzip_by_default + draw do + get "/page_caching_test/default_gzip", to: "page_caching_test#default_gzip" + end + @controller.expects(:cache_page).with(nil, nil, Zlib::BEST_COMPRESSION) get :default_gzip end def test_should_set_gzip_level + draw do + get "/page_caching_test/gzip_level", to: "page_caching_test#gzip_level" + end + @controller.expects(:cache_page).with(nil, nil, Zlib::BEST_SPEED) get :gzip_level end def test_should_cache_without_trailing_slash_on_url - @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash' - assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html") + @controller.class.cache_page "cached content", "/page_caching_test/trailing_slash" + assert_page_cached :trailing_slash, content: "cached content" end def test_should_obey_http_accept_attribute - @request.env['HTTP_ACCEPT'] = 'text/xml' + draw do + get "/page_caching_test/about_me", to: "page_caching_test#about_me" + end + + @request.env["HTTP_ACCEPT"] = "text/xml" get :about_me - assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/about_me.xml") - assert_equal 'I am xml', @response.body + assert_equal "I am xml", @response.body + assert_page_cached :about_me, format: "xml", content: "I am xml" end def test_cached_page_should_not_have_trailing_slash_even_if_url_has_trailing_slash - @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/' - assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html") + @controller.class.cache_page "cached content", "/page_caching_test/trailing_slash/" + assert_page_cached :trailing_slash, content: "cached content" end def test_should_cache_ok_at_custom_path - @request.env['PATH_INFO'] = '/index.html' + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + end + + @request.env["PATH_INFO"] = "/index.html" get :ok assert_response :ok - assert File.exist?("#{FILE_STORE_PATH}/index.html") + assert_page_cached :index, controller: ".", content: "ok" end [:ok, :no_content, :found, :not_found].each do |status| [:get, :post, :patch, :put, :delete].each do |method| unless method == :get && status == :ok define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + match "/page_caching_test/#{status}", to: "page_caching_test##{status}", via: method + end + send(method, status) assert_response status - assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached" + assert_page_not_cached status end end end end def test_page_caching_conditional_options - get :ok, format: 'json' + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + end + + get :ok, format: "json" assert_page_not_cached :ok end def test_page_caching_directory_set_as_pathname begin ActionController::Base.page_cache_directory = Pathname.new(FILE_STORE_PATH) + + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + end + get :ok assert_response :ok assert_page_cached :ok @@ -249,17 +345,180 @@ end end - private + def test_page_caching_directory_set_on_controller_instance + draw do + get "/page_caching_test/ok", to: "page_caching_test#ok" + end + + file_store_path = File.join(TEST_TMP_DIR, "instance_cache") + @controller.page_cache_directory = file_store_path + + get :ok + assert_response :ok + assert_page_cached :ok, path: file_store_path + end +end + +class ProcPageCachingTestController < CachingController + self.page_cache_directory = -> { File.join(TEST_TMP_DIR, request.domain) } + + caches_page :ok + + def ok + render html: "ok" + end + + def expire_ok + expire_page action: :ok + head :ok + end +end + +class ProcPageCachingTest < ActionController::TestCase + include PageCachingTestHelpers + tests ProcPageCachingTestController + + def test_page_is_cached_by_domain + draw do + get "/proc_page_caching_test/ok", to: "proc_page_caching_test#ok" + get "/proc_page_caching_test/ok/expire", to: "proc_page_caching_test#expire_ok" + end + + @request.env["HTTP_HOST"] = "www.foo.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/foo.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/foo.com" + + @request.env["HTTP_HOST"] = "www.bar.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/bar.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/bar.com" + end + + def test_class_level_cache_page_raise_error + assert_raises(RuntimeError, /class-level cache_page method/) do + @controller.class.cache_page "cached content", "/proc_page_caching_test/ok" + end + end +end + +class SymbolPageCachingTestController < CachingController + self.page_cache_directory = :domain_cache_directory + + caches_page :ok + + def ok + render html: "ok" + end + + def expire_ok + expire_page action: :ok + head :ok + end + + protected + def domain_cache_directory + File.join(TEST_TMP_DIR, request.domain) + end +end + +class SymbolPageCachingTest < ActionController::TestCase + include PageCachingTestHelpers + tests SymbolPageCachingTestController + + def test_page_is_cached_by_domain + draw do + get "/symbol_page_caching_test/ok", to: "symbol_page_caching_test#ok" + get "/symbol_page_caching_test/ok/expire", to: "symbol_page_caching_test#expire_ok" + end + + @request.env["HTTP_HOST"] = "www.foo.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/foo.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/foo.com" - def assert_page_cached(action, message = "#{action} should have been cached") - assert page_cached?(action), message + @request.env["HTTP_HOST"] = "www.bar.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/bar.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/bar.com" + end + + def test_class_level_cache_page_raise_error + assert_raises(RuntimeError, /class-level cache_page method/) do + @controller.class.cache_page "cached content", "/symbol_page_caching_test/ok" end + end +end + +class CallablePageCachingTestController < CachingController + class DomainCacheDirectory + def self.call(request) + File.join(TEST_TMP_DIR, request.domain) + end + end - def assert_page_not_cached(action, message = "#{action} shouldn't have been cached") - assert !page_cached?(action), message + self.page_cache_directory = DomainCacheDirectory + + caches_page :ok + + def ok + render html: "ok" + end + + def expire_ok + expire_page action: :ok + head :ok + end +end + +class CallablePageCachingTest < ActionController::TestCase + include PageCachingTestHelpers + tests CallablePageCachingTestController + + def test_page_is_cached_by_domain + draw do + get "/callable_page_caching_test/ok", to: "callable_page_caching_test#ok" + get "/callable_page_caching_test/ok/expire", to: "callable_page_caching_test#expire_ok" end - def page_cached?(action) - File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html" + @request.env["HTTP_HOST"] = "www.foo.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/foo.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/foo.com" + + @request.env["HTTP_HOST"] = "www.bar.com" + get :ok + assert_response :ok + assert_page_cached :ok, path: TEST_TMP_DIR + "/bar.com" + + get :expire_ok + assert_response :ok + assert_page_not_cached :ok, path: TEST_TMP_DIR + "/bar.com" + end + + def test_class_level_cache_page_raise_error + assert_raises(RuntimeError, /class-level cache_page method/) do + @controller.class.cache_page "cached content", "/callable_page_caching_test/ok" end + end end diff -Nru ruby-actionpack-page-caching-1.0.2/test/log_subscriber_test.rb ruby-actionpack-page-caching-1.1.0/test/log_subscriber_test.rb --- ruby-actionpack-page-caching-1.0.2/test/log_subscriber_test.rb 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/test/log_subscriber_test.rb 2018-06-26 18:41:59.000000000 +0000 @@ -1,6 +1,6 @@ -require 'abstract_unit' -require 'active_support/log_subscriber/test_helper' -require 'action_controller/log_subscriber' +require "abstract_unit" +require "active_support/log_subscriber/test_helper" +require "action_controller/log_subscriber" module Another class LogSubscribersController < ActionController::Base @@ -9,8 +9,8 @@ self.perform_caching = true def with_page_cache - cache_page('Super soaker', '/index.html') - render nothing: true + cache_page("Super soaker", "/index.html") + head :ok end end end @@ -22,12 +22,9 @@ def setup super - @routes = SharedTestRoutes - @routes.draw do - get ':controller(/:action)' - end + @routes = ActionDispatch::Routing::RouteSet.new - @cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__)) + @cache_path = File.expand_path("../tmp/test_cache", __FILE__) ActionController::Base.page_cache_directory = @cache_path @controller.cache_store = :file_store, @cache_path ActionController::LogSubscriber.attach_to :action_controller @@ -43,12 +40,18 @@ end def test_with_page_cache - get :with_page_cache - wait - - logs = @logger.logged(:info) - assert_equal 3, logs.size - assert_match(/Write page/, logs[1]) - assert_match(/\/index\.html/, logs[1]) + with_routing do |set| + set.draw do + get "/with_page_cache", to: "another/log_subscribers#with_page_cache" + end + + get :with_page_cache + wait + + logs = @logger.logged(:info) + assert_equal 3, logs.size + assert_match(/Write page/, logs[1]) + assert_match(/\/index\.html/, logs[1]) + end end end diff -Nru ruby-actionpack-page-caching-1.0.2/.travis.yml ruby-actionpack-page-caching-1.1.0/.travis.yml --- ruby-actionpack-page-caching-1.0.2/.travis.yml 2014-11-21 13:24:17.000000000 +0000 +++ ruby-actionpack-page-caching-1.1.0/.travis.yml 2018-06-26 18:41:59.000000000 +0000 @@ -1,16 +1,55 @@ language: ruby +sudo: false + +cache: + bundler: true + before_install: - gem install bundler + rvm: - 1.9.3 - 2.0.0 + - 2.1.9 + - 2.2.6 + - 2.3.3 + - 2.4.0 + gemfile: - Gemfile - gemfiles/Gemfile-4-0-stable + - gemfiles/Gemfile-4-1-stable + - gemfiles/Gemfile-4-2-stable + - gemfiles/Gemfile-5-0-stable - gemfiles/Gemfile-edge + matrix: allow_failures: - gemfile: gemfiles/Gemfile-edge + exclude: + - rvm: 1.9.3 + gemfile: Gemfile + - rvm: 2.0.0 + gemfile: Gemfile + - rvm: 2.1.9 + gemfile: Gemfile + - rvm: 1.9.3 + gemfile: gemfiles/Gemfile-5-0-stable + - rvm: 2.0.0 + gemfile: gemfiles/Gemfile-5-0-stable + - rvm: 2.1.9 + gemfile: gemfiles/Gemfile-5-0-stable + - rvm: 1.9.3 + gemfile: gemfiles/Gemfile-edge + - rvm: 2.0.0 + gemfile: gemfiles/Gemfile-edge + - rvm: 2.1.9 + gemfile: gemfiles/Gemfile-edge + - rvm: 2.4.0 + gemfile: gemfiles/Gemfile-4-0-stable + - rvm: 2.4.0 + gemfile: gemfiles/Gemfile-4-1-stable + notifications: email: false irc: