Basically, all my controllers inherit from GenericController, all my
models from GenericModel, so I can single source stuff like these
behaviors, but override if necessary...
GenericController:
def create
model = params[:controller].singularize.downcase
@object =
params[:controller].singularize.camelcase.constantize.new(params[model])
if @object.save
# expire caches invalidated by the creation of this object
@object.expire_caches 'create'
end
end
def update
model = params[:controller].singularize.downcase
@object =
params[:controller].singularize.camelcase.constantize.find(params[:id])
orig_name = @object.name
if @object.update_attributes(params[model])
expire_type = (@object.name == orig_name ? 'update' : 'rename')
# expire caches invalidated by just an update, or an update
# including a name change
@object.expire_caches expire_type
end
end
Generic Model:
# models have fragments in basically
# modelname.id.row,
# modelname.id.show,
# modelname.id.related, and a bunch of other 'sub-fragments'
# that make up the 'related' items navigation sidebar
def expire_caches(method)
model = self.class.table_name.singularize
case method
when 'update'
# just expire the 'show' fragment for this model
expire_base_fragment model, self.id
when 'destroy'
# destroy all the fragments for this model, and all other
# fragments that include this model - this is driven by
# an attribute of each model, which tells what the related
# models are
destroy_caches model, self.id
when 'rename'
# just like destroy
destroy_caches model, self.id
when 'create'
# this case looks at models related to the current
# one to see if those caches should be invalidated. i.e.,
# creating this model as a child of another invalidates
# that models "children" cache
... ugly code omitted...
else
# there are other actions that can cause different fragments
# to be expired - relating 2 models will cause 4 fragments to
# be expired:
# this model's list of that related type,
# that model's list of this related type,
# and both model's aggregate 'related' fragment
... ugly code omitted...
end
end
The various "expire" or "destroy" methods are all variants of:
def expire_base_fragment(model, id)
Rails.cache.delete('views/'+model+'/'+id.to_s+'/show')
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---