Good morning,
A bit of background might help. I'm creating a very simple app to be used as an attendance tracking solution - it will sit on a running computer in a gym, and people can enter their names and click a button based on what type of workout they did.
The people running the app are not overly technical - but I wanted to give them a way to change the basic text on the home page, as well as on the 'help' page. I created a Meta model which has columns for "help text" "main page text" etc., which they can update via the ActiveAdmin interface.
I want to do page caching on both the home and help pages (more so just to learn Rails caching for my own more complicated projects) and only want to expire the 'help' page if the Meta -> "help_text" attribute has changed, but expire the home page if any of the other Meta -> attributes have changed.
Is this possible?
My caching code is pretty basic at this point:
class MetaSweeper < ActionController::Caching::Sweeper
observe Meta
def after_create(meta)
expire_cache_for(meta)
puts "[CACHE] Expiring cached pages"
end
def after_update(meta)
expire_cache_for(meta)
puts "[CACHE] Expiring cached pages"
end
def after_destroy(meta)
expire_cache_for(meta)
puts "[CACHE] Expiring cached pages"
end
private
def expire_cache_for(meta)
expire_page(:controller => 'static_pages', :action => 'home')
expire_page(:controller => 'static_pages', :action => 'help')
# Expire a fragment
#expire_fragment('all_available_products')
end
end
And in the application_controller:
cache_sweeper :meta_sweeper
Thanks!
EDIT 1
From the first answer, I've tried to set a virtual attribute on the "Meta" model to try to capture if the help_content attribute has changed, so that I can determine if I should expire the /help page.
meta.rb
attr_accessor :help_changed
before_update :set_cache_expiry_checker
private
def set_cache_expiry_checker
help_changed = "you bet its changed!"
end
meta_sweeper.rb
def after_update(meta)
puts "should be doing something here about expiring...."
if meta.help_changed
puts "should expire help page"
else
puts "should NOT expire help page"
end
When I run my application, I see the output of the first puts, but not the second. meta.help_changed appears to be nil.... even though I do have the virtual attribute on the model. If I do a "puts meta.inspect" at the top of after_update, I see all of the meta attributes EXCEPT the virtual one. Is there something going on in sweepers that only pulls the database stuff?
Is there another place inside the sweeper I can set a variable to do this? I've tried doing a:
expire_help_cache = meta.help_changed
At the very top of meta_sweeper.rb, but that throws errors as there is no "meta" variable when you are outside of the after_update method.