# -*- encoding : utf-8 -*- module ThreeD module ModelFragments def self.included(base) base.extend ClassMethods 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 self.caching_path = ActionController::Base.cache_store.cache_path self.after_save :clear_view_cache self.after_destroy :clear_view_cache 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 end module InstanceMethods 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