lib/catche/controller/base.rb in catche-0.1.2 vs lib/catche/controller/base.rb in catche-0.2

- old
+ new

@@ -2,49 +2,50 @@ module Controller module Base extend ActiveSupport::Concern + included do + + class_attribute :catche_model, + :catche_resource_name + + end + module ClassMethods # Caches a controller action by tagging it. # Supports any option parameters caches_action supports. # - # catche Project, :index - # catche Task, :through => :project + # catche Project, :index, :resource_name => :project def catche(model, *args) options = args.extract_options! - object = Catche::Tag::Object.register!(model, self, options) - tag = Proc.new { |controller| object } + self.catche_model = model + self.catche_resource_name = options[:resource_name] || self.catche_model.name.downcase.to_sym + # Use Rails caches_action to pass along the tag - caches_action(*args, { :tag => tag }.merge(options)) + caches_action(*args, options.merge({ :catche => true })) end end - def _save_fragment(name, options={}) - key = fragment_cache_key(name) - object = options[:tag] - tags = [] + def catche_tags + return @catche_tags if ! @catche_tags.nil? - if object.present? - if object.respond_to?(:call) - object = self.instance_exec(self, &object) + if resource = Catche::ResourceLoader.fetch_one(self, self.class.catche_resource_name) + tags = Catche::Tag::Collect.resource(resource) + @catche_tags = tags[:set] + else + tags = Catche::Tag::Collect.collection(self, self.class.catche_model) + @catche_tags = tags[:set] + end + end - if object.respond_to?(:tags) - tags = object.tags(self) - else - tags = Array.new(object) - end - else - tags = Array.new(object) - end - - Catche::Tag.tag! key, *tags - - # Store for future reference - @catche_cache_object = object + def _save_fragment(name, options={}) + if options[:catche] + key = fragment_cache_key(name) + Catche::Tag.tag! key, *catche_tags end super end \ No newline at end of file