actionpack 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. data/CHANGELOG +149 -7
  2. data/MIT-LICENSE +1 -1
  3. data/README +1 -1
  4. data/Rakefile +5 -6
  5. data/lib/action_controller.rb +2 -2
  6. data/lib/action_controller/assertions/model_assertions.rb +2 -1
  7. data/lib/action_controller/assertions/response_assertions.rb +4 -2
  8. data/lib/action_controller/assertions/routing_assertions.rb +3 -3
  9. data/lib/action_controller/assertions/selector_assertions.rb +30 -27
  10. data/lib/action_controller/assertions/tag_assertions.rb +3 -3
  11. data/lib/action_controller/base.rb +103 -129
  12. data/lib/action_controller/benchmarking.rb +3 -3
  13. data/lib/action_controller/caching.rb +41 -652
  14. data/lib/action_controller/caching/actions.rb +144 -0
  15. data/lib/action_controller/caching/fragments.rb +138 -0
  16. data/lib/action_controller/caching/pages.rb +154 -0
  17. data/lib/action_controller/caching/sql_cache.rb +18 -0
  18. data/lib/action_controller/caching/sweeping.rb +97 -0
  19. data/lib/action_controller/cgi_ext/cookie.rb +27 -23
  20. data/lib/action_controller/cgi_ext/stdinput.rb +1 -0
  21. data/lib/action_controller/cgi_process.rb +6 -4
  22. data/lib/action_controller/components.rb +7 -6
  23. data/lib/action_controller/cookies.rb +31 -19
  24. data/lib/action_controller/dispatcher.rb +51 -84
  25. data/lib/action_controller/filters.rb +295 -421
  26. data/lib/action_controller/flash.rb +1 -6
  27. data/lib/action_controller/headers.rb +31 -0
  28. data/lib/action_controller/helpers.rb +26 -9
  29. data/lib/action_controller/http_authentication.rb +1 -1
  30. data/lib/action_controller/integration.rb +65 -13
  31. data/lib/action_controller/layout.rb +24 -39
  32. data/lib/action_controller/mime_responds.rb +7 -3
  33. data/lib/action_controller/mime_type.rb +25 -9
  34. data/lib/action_controller/mime_types.rb +1 -1
  35. data/lib/action_controller/polymorphic_routes.rb +32 -17
  36. data/lib/action_controller/record_identifier.rb +10 -4
  37. data/lib/action_controller/request.rb +46 -30
  38. data/lib/action_controller/request_forgery_protection.rb +10 -9
  39. data/lib/action_controller/request_profiler.rb +29 -8
  40. data/lib/action_controller/rescue.rb +24 -24
  41. data/lib/action_controller/resources.rb +66 -23
  42. data/lib/action_controller/response.rb +2 -2
  43. data/lib/action_controller/routing.rb +113 -1229
  44. data/lib/action_controller/routing/builder.rb +204 -0
  45. data/lib/action_controller/{routing_optimisation.rb → routing/optimisations.rb} +13 -12
  46. data/lib/action_controller/routing/recognition_optimisation.rb +158 -0
  47. data/lib/action_controller/routing/route.rb +240 -0
  48. data/lib/action_controller/routing/route_set.rb +435 -0
  49. data/lib/action_controller/routing/routing_ext.rb +46 -0
  50. data/lib/action_controller/routing/segments.rb +283 -0
  51. data/lib/action_controller/session/active_record_store.rb +13 -8
  52. data/lib/action_controller/session/cookie_store.rb +20 -17
  53. data/lib/action_controller/session_management.rb +10 -3
  54. data/lib/action_controller/streaming.rb +45 -31
  55. data/lib/action_controller/test_case.rb +33 -23
  56. data/lib/action_controller/test_process.rb +39 -35
  57. data/lib/action_controller/url_rewriter.rb +18 -12
  58. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +1 -1
  59. data/lib/action_pack.rb +1 -1
  60. data/lib/action_pack/version.rb +2 -2
  61. data/lib/action_view.rb +11 -3
  62. data/lib/action_view/base.rb +73 -390
  63. data/lib/action_view/helpers/active_record_helper.rb +83 -62
  64. data/lib/action_view/helpers/asset_tag_helper.rb +101 -44
  65. data/lib/action_view/helpers/atom_feed_helper.rb +35 -7
  66. data/lib/action_view/helpers/benchmark_helper.rb +5 -3
  67. data/lib/action_view/helpers/cache_helper.rb +3 -2
  68. data/lib/action_view/helpers/capture_helper.rb +1 -2
  69. data/lib/action_view/helpers/date_helper.rb +104 -82
  70. data/lib/action_view/helpers/form_helper.rb +148 -75
  71. data/lib/action_view/helpers/form_options_helper.rb +44 -23
  72. data/lib/action_view/helpers/form_tag_helper.rb +22 -13
  73. data/lib/action_view/helpers/javascripts/controls.js +1 -1
  74. data/lib/action_view/helpers/javascripts/dragdrop.js +1 -1
  75. data/lib/action_view/helpers/javascripts/effects.js +1 -1
  76. data/lib/action_view/helpers/number_helper.rb +10 -3
  77. data/lib/action_view/helpers/prototype_helper.rb +61 -29
  78. data/lib/action_view/helpers/record_tag_helper.rb +3 -3
  79. data/lib/action_view/helpers/sanitize_helper.rb +23 -17
  80. data/lib/action_view/helpers/scriptaculous_helper.rb +86 -60
  81. data/lib/action_view/helpers/text_helper.rb +153 -125
  82. data/lib/action_view/helpers/url_helper.rb +83 -28
  83. data/lib/action_view/inline_template.rb +20 -0
  84. data/lib/action_view/partial_template.rb +70 -0
  85. data/lib/action_view/partials.rb +31 -73
  86. data/lib/action_view/template.rb +127 -0
  87. data/lib/action_view/template_error.rb +8 -7
  88. data/lib/action_view/template_finder.rb +177 -0
  89. data/lib/action_view/template_handler.rb +18 -1
  90. data/lib/action_view/template_handlers/builder.rb +10 -2
  91. data/lib/action_view/template_handlers/compilable.rb +128 -0
  92. data/lib/action_view/template_handlers/erb.rb +37 -2
  93. data/lib/action_view/template_handlers/rjs.rb +14 -1
  94. data/lib/action_view/test_case.rb +58 -0
  95. data/test/abstract_unit.rb +1 -1
  96. data/test/active_record_unit.rb +3 -6
  97. data/test/activerecord/active_record_store_test.rb +1 -2
  98. data/test/activerecord/render_partial_with_record_identification_test.rb +158 -41
  99. data/test/adv_attr_test.rb +20 -0
  100. data/test/controller/action_pack_assertions_test.rb +16 -19
  101. data/test/controller/addresses_render_test.rb +1 -1
  102. data/test/controller/assert_select_test.rb +13 -6
  103. data/test/controller/base_test.rb +48 -2
  104. data/test/controller/benchmark_test.rb +1 -2
  105. data/test/controller/caching_test.rb +282 -21
  106. data/test/controller/capture_test.rb +1 -1
  107. data/test/controller/cgi_test.rb +1 -1
  108. data/test/controller/components_test.rb +1 -1
  109. data/test/controller/content_type_test.rb +2 -2
  110. data/test/controller/cookie_test.rb +13 -2
  111. data/test/controller/custom_handler_test.rb +14 -10
  112. data/test/controller/deprecation/deprecated_base_methods_test.rb +1 -1
  113. data/test/controller/dispatcher_test.rb +31 -49
  114. data/test/controller/fake_controllers.rb +17 -0
  115. data/test/controller/fake_models.rb +6 -0
  116. data/test/controller/filter_params_test.rb +14 -8
  117. data/test/controller/filters_test.rb +44 -16
  118. data/test/controller/flash_test.rb +2 -2
  119. data/test/controller/header_test.rb +14 -0
  120. data/test/controller/helper_test.rb +19 -15
  121. data/test/controller/html-scanner/document_test.rb +1 -2
  122. data/test/controller/html-scanner/node_test.rb +1 -2
  123. data/test/controller/html-scanner/sanitizer_test.rb +8 -5
  124. data/test/controller/html-scanner/tag_node_test.rb +1 -2
  125. data/test/controller/html-scanner/text_node_test.rb +2 -3
  126. data/test/controller/html-scanner/tokenizer_test.rb +8 -2
  127. data/test/controller/http_authentication_test.rb +1 -1
  128. data/test/controller/integration_test.rb +14 -16
  129. data/test/controller/integration_upload_test.rb +43 -0
  130. data/test/controller/layout_test.rb +26 -6
  131. data/test/controller/mime_responds_test.rb +39 -7
  132. data/test/controller/mime_type_test.rb +29 -5
  133. data/test/controller/new_render_test.rb +105 -34
  134. data/test/controller/polymorphic_routes_test.rb +32 -20
  135. data/test/controller/record_identifier_test.rb +38 -2
  136. data/test/controller/redirect_test.rb +21 -1
  137. data/test/controller/render_test.rb +59 -15
  138. data/test/controller/request_forgery_protection_test.rb +92 -5
  139. data/test/controller/request_test.rb +64 -6
  140. data/test/controller/rescue_test.rb +22 -6
  141. data/test/controller/resources_test.rb +102 -14
  142. data/test/controller/routing_test.rb +231 -19
  143. data/test/controller/selector_test.rb +2 -2
  144. data/test/controller/send_file_test.rb +14 -3
  145. data/test/controller/session/cookie_store_test.rb +16 -4
  146. data/test/controller/session/mem_cache_store_test.rb +3 -4
  147. data/test/controller/session_fixation_test.rb +1 -1
  148. data/test/controller/session_management_test.rb +23 -1
  149. data/test/controller/test_test.rb +39 -18
  150. data/test/controller/url_rewriter_test.rb +35 -1
  151. data/test/controller/verification_test.rb +1 -1
  152. data/test/controller/view_paths_test.rb +15 -12
  153. data/test/controller/webservice_test.rb +48 -3
  154. data/test/fixtures/bad_customers/_bad_customer.html.erb +1 -0
  155. data/test/fixtures/company.rb +1 -0
  156. data/test/fixtures/customers/_customer.html.erb +1 -0
  157. data/test/fixtures/db_definitions/sqlite.sql +6 -0
  158. data/test/fixtures/functional_caching/_partial.erb +3 -0
  159. data/test/fixtures/functional_caching/fragment_cached.html.erb +2 -0
  160. data/test/fixtures/functional_caching/html_fragment_cached_with_partial.html.erb +1 -0
  161. data/test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs +1 -0
  162. data/test/fixtures/good_customers/_good_customer.html.erb +1 -0
  163. data/test/fixtures/mascot.rb +3 -0
  164. data/test/fixtures/mascots.yml +4 -0
  165. data/test/fixtures/mascots/_mascot.html.erb +1 -0
  166. data/test/fixtures/multipart/boundary_problem_file +10 -0
  167. data/test/fixtures/public/javascripts/application.js +1 -0
  168. data/test/fixtures/public/javascripts/controls.js +1 -0
  169. data/test/fixtures/public/javascripts/dragdrop.js +1 -0
  170. data/test/fixtures/public/javascripts/effects.js +1 -0
  171. data/test/fixtures/public/javascripts/prototype.js +1 -0
  172. data/test/fixtures/public/javascripts/version.1.0.js +1 -0
  173. data/test/fixtures/public/stylesheets/version.1.0.css +1 -0
  174. data/test/fixtures/reply.rb +1 -0
  175. data/test/fixtures/shared.html.erb +1 -0
  176. data/test/fixtures/symlink_parent/symlinked_layout.erb +5 -0
  177. data/test/fixtures/test/_customer_counter.erb +1 -0
  178. data/test/fixtures/test/_form.erb +1 -0
  179. data/test/fixtures/test/_labelling_form.erb +1 -0
  180. data/test/fixtures/test/_raise.html.erb +1 -0
  181. data/test/fixtures/test/greeting.js.rjs +1 -0
  182. data/test/fixtures/topics/_topic.html.erb +1 -0
  183. data/test/template/active_record_helper_test.rb +25 -8
  184. data/test/template/asset_tag_helper_test.rb +100 -17
  185. data/test/template/atom_feed_helper_test.rb +29 -1
  186. data/test/template/benchmark_helper_test.rb +10 -22
  187. data/test/template/date_helper_test.rb +455 -153
  188. data/test/template/erb_util_test.rb +10 -42
  189. data/test/template/form_helper_test.rb +192 -66
  190. data/test/template/form_options_helper_test.rb +19 -8
  191. data/test/template/form_tag_helper_test.rb +11 -8
  192. data/test/template/javascript_helper_test.rb +3 -9
  193. data/test/template/number_helper_test.rb +6 -3
  194. data/test/template/prototype_helper_test.rb +27 -40
  195. data/test/template/record_tag_helper_test.rb +54 -0
  196. data/test/template/sanitize_helper_test.rb +5 -6
  197. data/test/template/scriptaculous_helper_test.rb +7 -13
  198. data/test/template/tag_helper_test.rb +3 -6
  199. data/test/template/template_finder_test.rb +73 -0
  200. data/test/template/template_object_test.rb +95 -0
  201. data/test/template/test_test.rb +56 -0
  202. data/test/template/text_helper_test.rb +46 -33
  203. data/test/template/url_helper_test.rb +8 -10
  204. metadata +65 -12
  205. data/lib/action_view/compiled_templates.rb +0 -69
  206. data/test/action_view_test.rb +0 -44
  207. data/test/activerecord/fixtures_test.rb +0 -24
  208. data/test/controller/fragment_store_setting_test.rb +0 -47
  209. data/test/template/compiled_templates_test.rb +0 -197
  210. data/test/template/deprecate_ivars_test.rb +0 -51
@@ -0,0 +1,144 @@
1
+ require 'set'
2
+
3
+ module ActionController #:nodoc:
4
+ module Caching
5
+ # Action caching is similar to page caching by the fact that the entire output of the response is cached, but unlike page caching,
6
+ # every request still goes through the Action Pack. The key benefit of this is that filters are run before the cache is served, which
7
+ # allows for authentication and other restrictions on whether someone is allowed to see the cache. Example:
8
+ #
9
+ # class ListsController < ApplicationController
10
+ # before_filter :authenticate, :except => :public
11
+ # caches_page :public
12
+ # caches_action :index, :show, :feed
13
+ # end
14
+ #
15
+ # In this example, the public action doesn't require authentication, so it's possible to use the faster page caching method. But both the
16
+ # show and feed action are to be shielded behind the authenticate filter, so we need to implement those as action caches.
17
+ #
18
+ # Action caching internally uses the fragment caching and an around filter to do the job. The fragment cache is named according to both
19
+ # the current host and the path. So a page that is accessed at http://david.somewhere.com/lists/show/1 will result in a fragment named
20
+ # "david.somewhere.com/lists/show/1". This allows the cacher to differentiate between "david.somewhere.com/lists/" and
21
+ # "jamis.somewhere.com/lists/" -- which is a helpful way of assisting the subdomain-as-account-key pattern.
22
+ #
23
+ # Different representations of the same resource, e.g. <tt>http://david.somewhere.com/lists</tt> and <tt>http://david.somewhere.com/lists.xml</tt>
24
+ # are treated like separate requests and so are cached separately. Keep in mind when expiring an action cache that <tt>:action => 'lists'</tt> is not the same
25
+ # as <tt>:action => 'list', :format => :xml</tt>.
26
+ #
27
+ # You can set modify the default action cache path by passing a :cache_path option. This will be passed directly to ActionCachePath.path_for. This is handy
28
+ # for actions with multiple possible routes that should be cached differently. If a block is given, it is called with the current controller instance.
29
+ #
30
+ # And you can also use :if to pass a Proc that specifies when the action should be cached.
31
+ #
32
+ # class ListsController < ApplicationController
33
+ # before_filter :authenticate, :except => :public
34
+ # caches_page :public
35
+ # caches_action :index, :if => Proc.new { |c| !c.request.format.json? } # cache if is not a JSON request
36
+ # caches_action :show, :cache_path => { :project => 1 }
37
+ # caches_action :feed, :cache_path => Proc.new { |controller|
38
+ # controller.params[:user_id] ?
39
+ # controller.send(:user_list_url, c.params[:user_id], c.params[:id]) :
40
+ # controller.send(:list_url, c.params[:id]) }
41
+ # end
42
+ #
43
+ module Actions
44
+ def self.included(base) #:nodoc:
45
+ base.extend(ClassMethods)
46
+ base.class_eval do
47
+ attr_accessor :rendered_action_cache, :action_cache_path
48
+ end
49
+ end
50
+
51
+ module ClassMethods
52
+ # Declares that +actions+ should be cached.
53
+ # See ActionController::Caching::Actions for details.
54
+ def caches_action(*actions)
55
+ return unless cache_configured?
56
+ options = actions.extract_options!
57
+ around_filter(ActionCacheFilter.new(:cache_path => options.delete(:cache_path)), {:only => actions}.merge(options))
58
+ end
59
+ end
60
+
61
+ protected
62
+ def expire_action(options = {})
63
+ return unless cache_configured?
64
+
65
+ if options[:action].is_a?(Array)
66
+ options[:action].dup.each do |action|
67
+ expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action })))
68
+ end
69
+ else
70
+ expire_fragment(ActionCachePath.path_for(self, options))
71
+ end
72
+ end
73
+
74
+ class ActionCacheFilter #:nodoc:
75
+ def initialize(options, &block)
76
+ @options = options
77
+ end
78
+
79
+ def before(controller)
80
+ cache_path = ActionCachePath.new(controller, path_options_for(controller, @options))
81
+ if cache = controller.read_fragment(cache_path.path)
82
+ controller.rendered_action_cache = true
83
+ set_content_type!(controller, cache_path.extension)
84
+ controller.send!(:render_for_text, cache)
85
+ false
86
+ else
87
+ controller.action_cache_path = cache_path
88
+ end
89
+ end
90
+
91
+ def after(controller)
92
+ return if controller.rendered_action_cache || !caching_allowed(controller)
93
+ controller.write_fragment(controller.action_cache_path.path, controller.response.body)
94
+ end
95
+
96
+ private
97
+ def set_content_type!(controller, extension)
98
+ controller.response.content_type = Mime::Type.lookup_by_extension(extension).to_s if extension
99
+ end
100
+
101
+ def path_options_for(controller, options)
102
+ ((path_options = options[:cache_path]).respond_to?(:call) ? path_options.call(controller) : path_options) || {}
103
+ end
104
+
105
+ def caching_allowed(controller)
106
+ controller.request.get? && controller.response.headers['Status'].to_i == 200
107
+ end
108
+ end
109
+
110
+ class ActionCachePath
111
+ attr_reader :path, :extension
112
+
113
+ class << self
114
+ def path_for(controller, options)
115
+ new(controller, options).path
116
+ end
117
+ end
118
+
119
+ def initialize(controller, options = {})
120
+ @extension = extract_extension(controller.request.path)
121
+ path = controller.url_for(options).split('://').last
122
+ normalize!(path)
123
+ add_extension!(path, @extension)
124
+ @path = URI.unescape(path)
125
+ end
126
+
127
+ private
128
+ def normalize!(path)
129
+ path << 'index' if path[-1] == ?/
130
+ end
131
+
132
+ def add_extension!(path, extension)
133
+ path << ".#{extension}" if extension
134
+ end
135
+
136
+ def extract_extension(file_path)
137
+ # Don't want just what comes after the last '.' to accommodate multi part extensions
138
+ # such as tar.gz.
139
+ file_path[/^[^.]+\.(.+)$/, 1]
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,138 @@
1
+ module ActionController #:nodoc:
2
+ module Caching
3
+ # Fragment caching is used for caching various blocks within templates without caching the entire action as a whole. This is useful when
4
+ # certain elements of an action change frequently or depend on complicated state while other parts rarely change or can be shared amongst multiple
5
+ # parties. The caching is doing using the cache helper available in the Action View. A template with caching might look something like:
6
+ #
7
+ # <b>Hello <%= @name %></b>
8
+ # <% cache do %>
9
+ # All the topics in the system:
10
+ # <%= render :partial => "topic", :collection => Topic.find(:all) %>
11
+ # <% end %>
12
+ #
13
+ # This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would
14
+ # be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>.
15
+ #
16
+ # This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using
17
+ # <tt>caches_action</tt>, so we also have the option to qualify the name of the cached fragment with something like:
18
+ #
19
+ # <% cache(:action => "list", :action_suffix => "all_topics") do %>
20
+ #
21
+ # That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a
22
+ # different suffix. Note that the URL doesn't have to really exist or be callable - the url_for system is just used to generate unique
23
+ # cache names that we can refer to when we need to expire the cache.
24
+ #
25
+ # The expiration call for this example is:
26
+ #
27
+ # expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")
28
+ module Fragments
29
+ def self.included(base) #:nodoc:
30
+ base.class_eval do
31
+ class << self
32
+ def fragment_cache_store=(store_option) #:nodoc:
33
+ ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=')
34
+ self.cache_store = store_option
35
+ end
36
+
37
+ def fragment_cache_store #:nodoc:
38
+ ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store')
39
+ cache_store
40
+ end
41
+ end
42
+
43
+ def fragment_cache_store=(store_option) #:nodoc:
44
+ ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=')
45
+ self.cache_store = store_option
46
+ end
47
+
48
+ def fragment_cache_store #:nodoc:
49
+ ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store')
50
+ cache_store
51
+ end
52
+ end
53
+ end
54
+
55
+ # Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,
56
+ # writing, or expiring a cached fragment. If the key is a hash, the generated key is the return
57
+ # value of url_for on that hash (without the protocol). All keys are prefixed with "views/" and uses
58
+ # ActiveSupport::Cache.expand_cache_key for the expansion.
59
+ def fragment_cache_key(key)
60
+ ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views)
61
+ end
62
+
63
+ def fragment_for(block, name = {}, options = nil) #:nodoc:
64
+ unless perform_caching then block.call; return end
65
+
66
+ buffer = yield
67
+
68
+ if cache = read_fragment(name, options)
69
+ buffer.concat(cache)
70
+ else
71
+ pos = buffer.length
72
+ block.call
73
+ write_fragment(name, buffer[pos..-1], options)
74
+ end
75
+ end
76
+
77
+ # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
78
+ def write_fragment(key, content, options = nil)
79
+ return unless cache_configured?
80
+
81
+ key = fragment_cache_key(key)
82
+
83
+ self.class.benchmark "Cached fragment miss: #{key}" do
84
+ cache_store.write(key, content, options)
85
+ end
86
+
87
+ content
88
+ end
89
+
90
+ # Reads a cached fragment from the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
91
+ def read_fragment(key, options = nil)
92
+ return unless cache_configured?
93
+
94
+ key = fragment_cache_key(key)
95
+
96
+ self.class.benchmark "Cached fragment hit: #{key}" do
97
+ cache_store.read(key, options)
98
+ end
99
+ end
100
+
101
+ # Check if a cached fragment from the location signified by <tt>key</tt> exists (see <tt>expire_fragment</tt> for acceptable formats)
102
+ def fragment_exist?(key, options = nil)
103
+ return unless cache_configured?
104
+
105
+ key = fragment_cache_key(key)
106
+
107
+ self.class.benchmark "Cached fragment exists?: #{key}" do
108
+ cache_store.exist?(key, options)
109
+ end
110
+ end
111
+
112
+ # Name can take one of three forms:
113
+ # * String: This would normally take the form of a path like "pages/45/notes"
114
+ # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 }
115
+ # * Regexp: Will destroy all the matched fragments, example:
116
+ # %r{pages/\d*/notes}
117
+ # Ensure you do not specify start and finish in the regex (^$) because
118
+ # the actual filename matched looks like ./cache/filename/path.cache
119
+ # Regexp expiration is only supported on caches that can iterate over
120
+ # all keys (unlike memcached).
121
+ def expire_fragment(key, options = nil)
122
+ return unless cache_configured?
123
+
124
+ key = key.is_a?(Regexp) ? key : fragment_cache_key(key)
125
+
126
+ if key.is_a?(Regexp)
127
+ self.class.benchmark "Expired fragments matching: #{key.source}" do
128
+ cache_store.delete_matched(key, options)
129
+ end
130
+ else
131
+ self.class.benchmark "Expired fragment: #{key}" do
132
+ cache_store.delete(key, options)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,154 @@
1
+ require 'fileutils'
2
+ require 'uri'
3
+
4
+ module ActionController #:nodoc:
5
+ module Caching
6
+ # Page caching is an approach to caching where the entire action output of is stored as a HTML file that the web server
7
+ # can serve without going through Action Pack. This is the fastest way to cache your content as opposed to going dynamically
8
+ # through the process of generating the content. Unfortunately, this incredible speed-up is only available to stateless pages
9
+ # where all visitors are treated the same. Content management systems -- including weblogs and wikis -- have many pages that are
10
+ # a great fit for this approach, but account-based systems where people log in and manipulate their own data are often less
11
+ # likely candidates.
12
+ #
13
+ # Specifying which actions to cache is done through the <tt>caches_page</tt> class method:
14
+ #
15
+ # class WeblogController < ActionController::Base
16
+ # caches_page :show, :new
17
+ # end
18
+ #
19
+ # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>,
20
+ # which match the URLs used to trigger the dynamic generation. This is how the web server is able
21
+ # pick up a cache file when it exists and otherwise let the request pass on to Action Pack to generate it.
22
+ #
23
+ # Expiration of the cache is handled by deleting the cached file, which results in a lazy regeneration approach where the cache
24
+ # is not restored before another hit is made against it. The API for doing so mimics the options from +url_for+ and friends:
25
+ #
26
+ # class WeblogController < ActionController::Base
27
+ # def update
28
+ # List.update(params[:list][:id], params[:list])
29
+ # expire_page :action => "show", :id => params[:list][:id]
30
+ # redirect_to :action => "show", :id => params[:list][:id]
31
+ # end
32
+ # end
33
+ #
34
+ # Additionally, you can expire caches using Sweepers that act on changes in the model to determine when a cache is supposed to be
35
+ # expired.
36
+ #
37
+ # == Setting the cache directory
38
+ #
39
+ # The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
40
+ # For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>RAILS_ROOT + "/public"</tt>). Changing
41
+ # this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
42
+ # web server to look in the new location for cached files.
43
+ #
44
+ # == Setting the cache extension
45
+ #
46
+ # Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
47
+ # order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
48
+ # If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
49
+ # extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
50
+ module Pages
51
+ def self.included(base) #:nodoc:
52
+ base.extend(ClassMethods)
53
+ base.class_eval do
54
+ @@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
55
+ cattr_accessor :page_cache_directory
56
+
57
+ @@page_cache_extension = '.html'
58
+ cattr_accessor :page_cache_extension
59
+ end
60
+ end
61
+
62
+ module ClassMethods
63
+ # Expires the page that was cached with the +path+ as a key. Example:
64
+ # expire_page "/lists/show"
65
+ def expire_page(path)
66
+ return unless perform_caching
67
+
68
+ benchmark "Expired page: #{page_cache_file(path)}" do
69
+ File.delete(page_cache_path(path)) if File.exist?(page_cache_path(path))
70
+ end
71
+ end
72
+
73
+ # Manually cache the +content+ in the key determined by +path+. Example:
74
+ # cache_page "I'm the cached content", "/lists/show"
75
+ def cache_page(content, path)
76
+ return unless perform_caching
77
+
78
+ benchmark "Cached page: #{page_cache_file(path)}" do
79
+ FileUtils.makedirs(File.dirname(page_cache_path(path)))
80
+ File.open(page_cache_path(path), "wb+") { |f| f.write(content) }
81
+ end
82
+ end
83
+
84
+ # Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that
85
+ # matches the triggering url.
86
+ #
87
+ # Usage:
88
+ #
89
+ # # cache the index action
90
+ # caches_page :index
91
+ #
92
+ # # cache the index action except for JSON requests
93
+ # caches_page :index, :if => Proc.new { |c| !c.request.format.json? }
94
+ def caches_page(*actions)
95
+ return unless perform_caching
96
+ options = actions.extract_options!
97
+ after_filter({:only => actions}.merge(options)) { |c| c.cache_page }
98
+ end
99
+
100
+ private
101
+ def page_cache_file(path)
102
+ name = (path.empty? || path == "/") ? "/index" : URI.unescape(path.chomp('/'))
103
+ name << page_cache_extension unless (name.split('/').last || name).include? '.'
104
+ return name
105
+ end
106
+
107
+ def page_cache_path(path)
108
+ page_cache_directory + page_cache_file(path)
109
+ end
110
+ end
111
+
112
+ # Expires the page that was cached with the +options+ as a key. Example:
113
+ # expire_page :controller => "lists", :action => "show"
114
+ def expire_page(options = {})
115
+ return unless perform_caching
116
+
117
+ if options.is_a?(Hash)
118
+ if options[:action].is_a?(Array)
119
+ options[:action].dup.each do |action|
120
+ self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action)))
121
+ end
122
+ else
123
+ self.class.expire_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true)))
124
+ end
125
+ else
126
+ self.class.expire_page(options)
127
+ end
128
+ end
129
+
130
+ # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used
131
+ # If no options are provided, the requested url is used. Example:
132
+ # cache_page "I'm the cached content", :controller => "lists", :action => "show"
133
+ def cache_page(content = nil, options = nil)
134
+ return unless perform_caching && caching_allowed
135
+
136
+ path = case options
137
+ when Hash
138
+ url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
139
+ when String
140
+ options
141
+ else
142
+ request.path
143
+ end
144
+
145
+ self.class.cache_page(content || response.body, path)
146
+ end
147
+
148
+ private
149
+ def caching_allowed
150
+ request.get? && response.headers['Status'].to_i == 200
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,18 @@
1
+ module ActionController #:nodoc:
2
+ module Caching
3
+ module SqlCache
4
+ def self.included(base) #:nodoc:
5
+ if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:cache)
6
+ base.alias_method_chain :perform_action, :caching
7
+ end
8
+ end
9
+
10
+ protected
11
+ def perform_action_with_caching
12
+ ActiveRecord::Base.cache do
13
+ perform_action_without_caching
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end