# -*- encoding : utf-8 -*- module ThreeD module ModelFragments def self.included(base) base.extend ClassMethods end def self.clear_cache_file(type = "") ThreeDCachingLog.debug "delete Fragment: #{ActionController::Base.cache_store.cache_path}/views/#{type}*" %x(rm -f #{ActionController::Base.cache_store.cache_path}/views/#{type}*) end module ClassMethods def model_fragments unless has_model_fragments? if ActionController::Base.cache_store.inspect.match("FileStore") self.send :include, InstanceMethods cattr_accessor :caching_path, :allowed_fragments self.caching_path = ActionController::Base.cache_store.cache_path self.after_save :clear_view_cache self.after_destroy :clear_view_cache # TODO: Build hash or array based settings # to allow only specified fragments to get called. # Provide group ability to clear groups of fragments like # :customers => %w(frag_a frag_b frag_c) # user.clear_view_cache_group(:customers) # => frag_a, frag_b frag_c get wiped! self.allowed_fragments = [] else raise "Currently, ModelFragments only support FileStore\nPlease set 'ActionController::Base.cache_store = :file_store, /path/to/cache'" end end end def has_model_fragments? self.included_modules.include?(InstanceMethods) end def cache_helper # TODO: Here should be the real ActionController caching funtionality #ActionController::Caching::Fragments.new end end module InstanceMethods def cache(data, scope_data=nil, &block) # TODO: Implement Cache function directly from model instance end def cache_name(data, scope_data=nil) data = data.to_s x = "#{self.class.name.parameterize}_#{self.id}_cache_#{data}" x << "_#{scope_data.to_s}" if scope_data return x end def clear_view_cache(data="") %x(rm #{self.full_cache_path(data)}) end def full_cache_path(data="") "#{self.class.caching_path}/views/#{self.class.name.parameterize}_#{self.id}_#{data}*" end end end end