Firstly you can add more than one action to caches_page
. This simplify the code to:
# controller_1.rb
caches_page :flu, :baz, :if => Proc.new{ |c| c.request.format && !c.request.format.json? && !is_google_bot? && flash[:notice].nil? && flash[:error].nil? }
# controller_2.rb
caches_page :foo, :bar, :if => Proc.new{ |c| c.request.format && !c.request.format.json? && !is_google_bot? && flash[:notice].nil? && flash[:error].nil? }
Secondly, all your controllers should inherit from your ApplicationController
, hence you can place this method there:
class ApplicationController < ActionController::Base
#...
private
def should_cache_pages?
request.format && request.format.json? && !is_google_bot? && flash[:notice].nil? && flash[:error].nil?
end
end
Which will simplify those lines to:
# controller_1.rb
caches_page :flu, :baz, if: :should_cache_pages?
# controller_2.rb
caches_page :foo, :bar, if: :should_cache_pages?
However you can go even one step further - if you always call caches_page
method with this if
key, I would consider overriding this method (not tested):
class ApplicationController < ActionController::Base
#...
def self.caches_page(*args)
options = args.extract_options!
options[:if] = Array.wrap(option[:if]) << :should_cache_pages?
super *args, options
end
private
def should_cache_pages?
request.format && request.format.json? && !is_google_bot? && flash[:notice].nil? && flash[:error].nil?
end
end
Then it simplifies to:
# controller_1.rb
caches_page :flu, :baz
# controller_2.rb
caches_page :foo, :bar