Class: Caboose::PageCacher

Inherits:
Object
  • Object
show all
Defined in:
app/models/caboose/page_cacher.rb

Class Method Summary collapse

Class Method Details

.add_block_type(site, p, bt) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/caboose/page_cacher.rb', line 84

def self.add_block_type(site, p, bt)
  return if bt.nil?
  if !@render_functions.has_key?(bt.id)
    f = ''        
    if bt.use_render_function && bt.render_function
      f = bt.render_function          
    else
      full_name = bt.full_name
      full_name = "lksdjflskfjslkfjlskdfjlkjsdf" if full_name.nil? || full_name.length == 0
      #if    File.file?(Rails.root.join("sites/#{site.name}/blocks/_#{full_name}.html.erb"     )) then f = Rails.root.join("sites/#{site.name}/blocks/_#{full_name}.html.erb")
      #elsif File.file?(Rails.root.join("sites/#{site.name}/blocks/_#{bt.name}.html.erb"       )) then f = Rails.root.join("sites/#{site.name}/blocks/_#{bt.name}.html.erb")
      #elsif File.file?(Rails.root.join("sites/#{site.name}/blocks/_#{bt.field_type}.html.erb" )) then f = Rails.root.join("sites/#{site.name}/blocks/_#{bt.field_type}.html.erb")
      if    File.file?(Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{full_name}.html.erb"     )) then f = Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{full_name}.html.erb")
      elsif File.file?(Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{bt.name}.html.erb"       )) then f = Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{bt.name}.html.erb")
      elsif File.file?(Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{bt.field_type}.html.erb" )) then f = Rails.root.join("app/views/caboose/blocks/#{site.name}/_#{bt.field_type}.html.erb")            
      elsif File.file?(Rails.root.join("app/views/caboose/blocks/_#{full_name}.html.erb"                  )) then f = Rails.root.join("app/views/caboose/blocks/_#{full_name}.html.erb")
      elsif File.file?(Rails.root.join("app/views/caboose/blocks/_#{bt.name}.html.erb"                    )) then f = Rails.root.join("app/views/caboose/blocks/_#{bt.name}.html.erb")
      elsif File.file?(Rails.root.join("app/views/caboose/blocks/_#{bt.field_type}.html.erb"              )) then f = Rails.root.join("app/views/caboose/blocks/_#{bt.field_type}.html.erb")
      elsif File.file?("#{Caboose.root}/app/views/caboose/blocks/_#{full_name}.html.erb"                   ) then f = "#{Caboose.root}/app/views/caboose/blocks/_#{full_name}.html.erb"
      elsif File.file?("#{Caboose.root}/app/views/caboose/blocks/_#{bt.name}.html.erb"                     ) then f = "#{Caboose.root}/app/views/caboose/blocks/_#{bt.name}.html.erb"
      elsif File.file?("#{Caboose.root}/app/views/caboose/blocks/_#{bt.field_type}.html.erb"               ) then f = "#{Caboose.root}/app/views/caboose/blocks/_#{bt.field_type}.html.erb"
      end
      f = (f && f.to_s.strip.length > 0 ? File.read(f) : '')
    end
    f.gsub!(/block\.partial\((.*?),(.*?)\)/ , 'render :partial => \1')
    f.gsub!(/block\.render\((.*?),(.*?)\)/  , 'render_block_type(block, \1, page, view, controller_view_content, modal, empty_text, editing, css, js, csrf_meta_tags, csrf_meta_tags2, logged_in_user, site)')        
    f.gsub!(/\=\s*render_block_type\(/      , 'render_block_type(')
    f.gsub!(/\=\s*raw render_block_type\(/  , 'render_block_type(')
    f.gsub!(/block.children.where\(([^\)]*?)\)\.all/, 'block.children_where(\1)')                
            
    #@render_functions[bt.id] = "<% def render_block_type_#{bt.id}(block, page, view, controller_view_content, modal, empty_text, editing, css, js, csrf_meta_tags, csrf_meta_tags2, logged_in_user, site) %>#{f}<% end %>"
      
    @render_functions[bt.id] = f
  end
  
  is_dup = false
  f = @render_functions[bt.id]
  @render_functions.each do |bt_id, f2|
    if f == f2 && @page_render_functions[p.id].include?(bt_id)
      @dups[bt_id] = [] if !@dups.has_key?(bt_id)
      @dups[bt_id] << bt.id if !@dups[bt_id].include?(bt.id)
      is_dup = true
      break
    end
  end
  
  if !is_dup        
    @page_render_functions[p.id] = [] if !@page_render_functions.has_key?(p.id)
    @page_render_functions[p.id] << bt.id if !@page_render_functions[p.id].include?(bt.id)
  end
end

.cache(page_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/caboose/page_cacher.rb', line 26

def self.cache(page_id)    
  p = (page_id.is_a?(Integer) ? Page.where(:id => page_id).first : page_id)
  return if p.nil?
  return if p.site_id.nil?
  return if p.site.nil?
  return if p.block.nil?
        
  @render_functions = {} if @render_functions.nil?
  @page_render_functions = {} if @page_render_functions.nil?      
  @page_render_functions[p.id] = []
  @dups = {}
              
  self.cache_helper(p.site, p, p.block)

  arr = @page_render_functions[p.id].collect do |bt_id|                        
    "<% def render_block_type_#{bt_id}(block, page, view, controller_view_content, modal, empty_text, editing, css, js, csrf_meta_tags, csrf_meta_tags2, logged_in_user, site) %>#{@render_functions[bt_id]}<% end %>"         
  end                  
  
  dups = []
  @dups.each{ |bt_id, arr| dups << "#{bt_id} => [#{arr.join(',')}]" }
  dups = dups.join(', ')
    
  str = arr.join("\n\n")      
  str << "<%\n"      
  str << "def dup_id(bt_id)\n"
  str << "  dups = {#{dups}}\n"
  str << "  dups.each do |bt_id2, arr|\n"
  str << "    return bt_id2 if arr.include?(bt_id)\n"
  str << "  end\n"
  str << "  return bt_id\n"
  str << "end\n"            
  str << "def render_block_type(block, child_block, page, view, controller_view_content, modal, empty_text, editing, css, js, csrf_meta_tags, csrf_meta_tags2, logged_in_user, site)\n"
  str << "  b = child_block && child_block.is_a?(String) ? block.child(child_block) : child_block\n"      
  str << "  send(\"render_block_type_\#\{dup_id(b.block_type_id)\}\", b, page, view, controller_view_content, modal, empty_text, editing, css, js, csrf_meta_tags, csrf_meta_tags2, logged_in_user, site)\n"
  str << "end\n\n"            
  str << "render_block_type_#{p.block.block_type_id}(@block, @page, @view, @controller_view_content, @modal, @empty_text, @editing, @css, @js, @csrf_meta_tags, @csrf_meta_tags2, @logged_in_user, @site)\n"      
  str << "%>\n"      
        
  pc = PageCache.where(:page_id => p.id).first
  pc = PageCache.new(:page_id => p.id) if pc.nil?
  pc.render_function = str
  pc.block = Marshal.dump(BlockCache.new(p.block))
  pc.refresh = false
  pc.save              
end

.cache_allObject



18
19
20
21
22
23
24
# File 'app/models/caboose/page_cacher.rb', line 18

def self.cache_all
  query = ["select id from pages order by id"]
  rows = ActiveRecord::Base.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, query))
  if rows
  #      rows.each{ |row| self.delay(:queue => 'caboose_cache').cache(row['id'].to_i) }
  end            
end

.cache_helper(site, p, block) ⇒ Object



72
73
74
75
76
# File 'app/models/caboose/page_cacher.rb', line 72

def self.cache_helper(site, p, block)
  return if block.nil?
  self.cache_helper2(site, p, block.block_type)            
  block.children.each{ |b2| self.cache_helper(site, p, b2) }
end

.cache_helper2(site, p, bt) ⇒ Object



78
79
80
81
82
# File 'app/models/caboose/page_cacher.rb', line 78

def self.cache_helper2(site, p, bt)
  return if bt.nil?      
  self.add_block_type(site, p, bt)
  bt.children.each{ |bt2| self.cache_helper2(site, p, bt2) }
end

.refreshObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/caboose/page_cacher.rb', line 5

def self.refresh
  PageCache.where(:refresh => true).all.each do |pc|
 #       self.delay(:queue => 'caboose_cache').cache(pc.page_id)
  end
  
  # Make sure all pages are cached
  query = ["select id from pages where id not in (select distinct(page_id) from page_cache)"]
  rows = ActiveRecord::Base.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, query))
  if rows
#        rows.each{ |row| self.delay(:queue => 'caboose_cache').cache(row['id'].to_i) }
  end
end