public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
enhanced cache sweeping of liquid objects, and better draft article 
sweeping behavior

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2313 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Tue Oct 03 21:51:56 -0700 2006
commit  aa857684bcf10ba172c8d183ed3c45312beda295
tree    ca0fc9dd0f84c7f005e6175e43528dcca2de9888
parent  4620d901cef50cbb8fbc14cb45f6a989a62b49da
...
1
2
3
4
5
6
7
 
8
9
10
11
12
 
 
 
 
 
 
 
13
14
15
...
1
2
 
 
 
 
 
3
4
5
6
7
 
8
9
10
11
12
13
14
15
16
17
0
@@ -1,15 +1,17 @@
0
 class ArticleSweeper < ActionController::Caching::Sweeper
0
   include Mephisto::SweeperMethods
0
- observe Article
0
-
0
- def after_create(record)
0
- expire_assigned_sections!(record) unless controller.nil? || record.status != :published
0
- end
0
+ observe Article, Section
0
 
0
   def after_save(record)
0
     return if controller.nil?
0
     expire_overview_feed! if record.is_a?(Article)
0
- site.expire_cached_pages controller, "Expired pages referenced by #{record.class} ##{record.id}", site.cached_pages.find_by_reference(record)
0
+ if record.is_a?(Article) && record.status == :published
0
+ expire_assigned_sections!(record)
0
+ end
0
+
0
+ if !record.is_a?(Article) || record.status == :published
0
+ site.expire_cached_pages controller, "Expired pages referenced by #{record.class} ##{record.id}", site.cached_pages.find_by_reference(record)
0
+ end
0
   end
0
 
0
   alias after_destroy after_save
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 class Admin::ArticlesController < Admin::BaseController
0
   with_options :only => [:create, :update, :destroy, :upload] do |c|
0
     c.before_filter :set_default_section_ids
0
- c.cache_sweeper :article_sweeper, :section_sweeper, :assigned_section_sweeper
0
+ c.cache_sweeper :article_sweeper, :assigned_section_sweeper
0
     cache_sweeper :comment_sweeper, :only => [:approve, :unapprove, :destroy_comment]
0
   end
0
 
...
1
2
 
3
4
5
...
1
 
2
3
4
5
0
@@ -1,5 +1,5 @@
0
 class Admin::SectionsController < Admin::BaseController
0
- cache_sweeper :section_sweeper, :except => :index
0
+ cache_sweeper :article_sweeper, :except => :index
0
   before_filter :find_and_sort_templates, :only => [:index, :edit]
0
   before_filter :find_and_reorder_sections, :only => [:index, :edit]
0
   before_filter :find_section, :only => [:destroy, :update]
...
44
45
46
47
48
 
49
50
51
...
44
45
46
 
 
47
48
49
50
0
@@ -44,8 +44,7 @@ class ApplicationController < ActionController::Base
0
       headers["Content-Type"] ||= 'text/html; charset=utf-8'
0
     
0
       if assigns['articles'] && assigns['article'].nil?
0
- self.cached_references += assigns['articles']
0
- assigns['articles'] = assigns['articles'].collect { |a| a.to_liquid :site => site }
0
+ assigns['articles'] = assigns['articles'].collect { |a| a.to_liquid :site => site }
0
       end
0
 
0
       status = (assigns.delete(:status) || '200 OK')
...
9
10
11
12
 
13
...
9
10
11
 
12
13
0
@@ -9,5 +9,5 @@ class BackendController < ApplicationController
0
 
0
   alias xmlrpc api
0
   
0
- cache_sweeper :article_sweeper, :section_sweeper, :assigned_section_sweeper, :comment_sweeper
0
+ cache_sweeper :article_sweeper, :assigned_section_sweeper, :comment_sweeper
0
 end
...
25
26
27
28
29
30
31
...
33
34
35
36
37
38
39
40
...
94
95
96
97
98
99
100
...
103
104
105
106
107
108
109
...
119
120
121
122
123
 
 
124
125
126
...
25
26
27
 
28
29
30
...
32
33
34
 
 
35
36
37
...
91
92
93
 
94
95
96
...
99
100
101
 
102
103
104
...
114
115
116
 
 
117
118
119
120
121
0
@@ -25,7 +25,6 @@ class MephistoController < ApplicationController
0
 
0
     def dispatch_list
0
       @articles = @section.articles.find_by_date(:include => :user, :limit => @section.articles_per_page)
0
- self.cached_references << @section
0
       render_liquid_template_for(:section, 'section' => @section.to_liquid(true),
0
                                            'articles' => @articles)
0
     end
0
@@ -33,8 +32,6 @@ class MephistoController < ApplicationController
0
     def dispatch_page
0
       @article = @dispatch_path.empty? ? @section.articles.find_by_position : @section.articles.find_by_permalink(@dispatch_path.first)
0
       show_404 and return unless @article
0
-
0
- self.cached_references << @section << @article
0
       Mephisto::Liquid::CommentForm.article = @article
0
       render_liquid_template_for(:page, 'section' => @section.to_liquid(true),
0
                                         'article' => @article.to_liquid(:mode => :single, :site => site))
0
@@ -94,7 +91,6 @@ class MephistoController < ApplicationController
0
     
0
     def dispatch_tags
0
       @articles = site.articles.find_all_by_tags(@dispatch_path, site.articles_per_page)
0
- self.cached_references << @section
0
       render_liquid_template_for(:tag, 'articles' => @articles, 'tags' => @dispatch_path)
0
     end
0
 
0
@@ -103,7 +99,6 @@ class MephistoController < ApplicationController
0
       @feed_title = "Comments"
0
       @comments = @article.comments
0
       @comments.reverse!
0
- self.cached_references << @comments
0
       render :action => 'feed', :content_type => 'application/xml'
0
     end
0
 
0
@@ -119,8 +114,8 @@ class MephistoController < ApplicationController
0
     end
0
 
0
     def find_article
0
- @article = site.articles.find_by_permalink(@dispatch_path.first)
0
- self.cached_references << @article if @article
0
+ cached_references << (@article = site.articles.find_by_permalink(@dispatch_path.first))
0
+ @article
0
     end
0
 
0
     def show_article_with(assigns = {})
...
4
5
6
 
7
8
9
10
11
...
4
5
6
7
8
 
9
10
11
0
@@ -4,8 +4,8 @@ class ArticleDrop < BaseDrop
0
   def article() @source end
0
 
0
   def initialize(source, options = {})
0
+ super source
0
     @options = options
0
- @source = source
0
     @site = options.delete(:site) || @source.site
0
     @article_liquid = {
0
       'id' => @source.id,
...
2
3
4
 
 
 
 
 
 
 
 
 
5
6
7
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -2,6 +2,15 @@ class BaseDrop < Liquid::Drop
0
   attr_reader :source
0
   delegate :hash, :to => :source
0
   
0
+ def initialize(source)
0
+ @source = source
0
+ end
0
+
0
+ def context=(current_context)
0
+ current_context.registers[:controller].send(:cached_references) << @source if @source
0
+ super
0
+ end
0
+
0
   def eql?(comparison_object)
0
     self == (comparison_object)
0
   end
...
5
6
7
8
 
9
10
11
...
5
6
7
 
8
9
10
11
0
@@ -5,7 +5,7 @@ class CommentDrop < BaseDrop
0
   def comment() @source end
0
 
0
   def initialize(source)
0
- @source = source
0
+ super
0
     @comment_liquid = %w(id author author_email author_ip created_at title published_at).inject({}) { |l, a| l.update(a => comment.send(a)) }
0
     @comment_liquid.update 'is_approved' => comment.approved?, 'body' => white_list(comment.body_html)
0
   end
...
5
6
7
8
 
9
10
11
...
5
6
7
 
8
9
10
11
0
@@ -5,7 +5,7 @@ class SectionDrop < BaseDrop
0
   def current() @current == true end
0
 
0
   def initialize(source, current = false)
0
- @source = source
0
+ super source
0
     @current = current
0
     @section_liquid = [:id, :name, :path, :archive_path].inject({}) { |h, k| h.update k.to_s => @source.send(k) }
0
     @section_liquid['articles_count'] = @source.send(:read_attribute, :articles_count)
...
3
4
5
6
 
7
8
9
...
3
4
5
 
6
7
8
9
0
@@ -3,7 +3,7 @@ class SiteDrop < BaseDrop
0
   def current_section() @current_section_liquid end
0
 
0
   def initialize(source, section = nil)
0
- @source = source
0
+ super source
0
     @current_section = section
0
     @current_section_liquid = section ? section.to_liquid : nil
0
     @site_liquid = [:id, :host, :subtitle, :title, :articles_per_page, :tag_path, :search_path].inject({}) { |h, k| h.merge k.to_s => @source.send(k) }
...
20
21
22
23
 
24
25
26
...
88
89
90
 
 
 
 
 
 
 
 
 
91
92
93
...
20
21
22
 
23
24
25
26
...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
0
@@ -20,7 +20,7 @@ class Article < Content
0
     end
0
 
0
     def pending?
0
- published? && Time.now.utc < published_at
0
+ !published? || Time.now.utc < published_at
0
     end
0
     
0
     def status
0
@@ -88,6 +88,15 @@ class Article < Content
0
     @new_sections = new_sections
0
   end
0
 
0
+ def published_at=(value)
0
+ @recently_published = published_at.nil? && value
0
+ write_attribute :published_at, value
0
+ end
0
+
0
+ def recently_published?
0
+ @recently_published
0
+ end
0
+
0
   # :mode - single / list. Specifies whether the body is only the excerpt or not
0
   # :page - true / false. Specifies whether the article is the main section page.
0
   def to_liquid(options = {})
...
272
273
274
 
275
276
277
...
272
273
274
275
276
277
278
0
@@ -272,6 +272,7 @@ class Site < ActiveRecord::Base
0
     
0
     def set_content_template(section, template_type)
0
       preferred_template = section.template if [:page, :section].include?(template_type)
0
+ preferred_template = section.archive_template if template_type == :archive
0
       find_preferred_template(template_type, preferred_template)
0
     end
0
     
...
40
41
42
43
44
 
 
 
 
 
 
45
...
40
41
42
 
43
44
45
46
47
48
49
50
0
@@ -40,4 +40,9 @@ cupcake_welcome_about:
0
   id: 8
0
   article_id: 8
0
   section_id: 4
0
- position: 1
0
\ No newline at end of file
0
+ position: 1
0
+draft_home:
0
+ id: 9
0
+ article_id: 11
0
+ section_id: 1
0
+ position: 5
0
\ No newline at end of file
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-<h1>{{ article.title }}</h1>
0
+<h1>{{ article.title }} in {{ section.name }}</h1>
0
 <div class="article">
0
   <h2>{{ article.published_at | format_date: 'standard' }}</h2>
0
   {{ article.body }}
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-<p>section</p>
0
+<p>{{ section.name }}</p>
0
 
0
 {% for article in articles %}
0
   <p>{{ article | link_to_article }}</p>
...
19
20
21
22
 
23
24
25
...
19
20
21
 
22
23
24
25
0
@@ -19,7 +19,7 @@ class Admin::SectionsControllerTest < Test::Unit::TestCase
0
     assert_equal sites(:first), assigns(:site)
0
     assert_equal sections(:home), assigns(:home)
0
     assert_equal 7, assigns(:sections).length, "Sections: #{assigns(:sections).collect(&:id).to_sentence}"
0
- assert_equal 3, assigns(:article_count)['1']
0
+ assert_equal 4, assigns(:article_count)['1']
0
     assert_equal 3, assigns(:article_count)['2']
0
   end
0
 
...
181
182
183
184
 
185
186
187
...
181
182
183
 
184
185
186
187
0
@@ -181,7 +181,7 @@ class MephistoControllerTest < Test::Unit::TestCase
0
   def test_should_render_liquid_templates_by_sections
0
     dispatch 'about'
0
     assert_dispatch_action :page
0
- assert_tag :tag => 'h1', :content => contents(:welcome).title
0
+ assert_tag :tag => 'h1', :content => "#{contents(:welcome).title} in #{sections(:about).name}"
0
   end
0
 
0
   def test_should_render_with_alternate_search_layout
...
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
43
...
245
246
247
248
 
249
250
251
252
 
253
254
255
...
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
305
306
307
 
308
309
310
 
 
311
312
313
314
0
@@ -38,6 +38,66 @@ class CachingTest < ActionController::IntegrationTest
0
     end
0
   end
0
 
0
+ def test_should_only_expire_overview_when_creating_draft
0
+ visitor = visit
0
+ writer = login_as :quentin
0
+
0
+ visit_sections_and_feeds_with visitor
0
+
0
+ assert_caches_page overview_path do
0
+ visitor.get_with_basic 'admin/overview.xml', :login => :quentin
0
+ end
0
+
0
+ assert_difference Article, :count do
0
+ assert_expires_page overview_path do
0
+ writer.create :title => 'This is a new article & title', :body => 'this is a new article body', :draft => 0
0
+ end
0
+ end
0
+
0
+ assert_cached section_url_for(:home)
0
+ assert_cached feed_url_for(:home)
0
+ assert_cached section_url_for(:about)
0
+ assert_cached feed_url_for(:about)
0
+ end
0
+
0
+ def test_should_only_expire_overview_when_revising_draft
0
+ visitor = visit
0
+ writer = login_as :quentin
0
+
0
+ visit_sections_and_feeds_with visitor
0
+
0
+ assert_caches_page overview_path do
0
+ visitor.get_with_basic 'admin/overview.xml', :login => :quentin
0
+ end
0
+
0
+ assert_expires_page overview_path do
0
+ writer.revise contents(:draft), :title => 'This is a new article & title', :draft => 0
0
+ end
0
+
0
+ assert_cached section_url_for(:home)
0
+ assert_cached feed_url_for(:home)
0
+ assert_cached section_url_for(:about)
0
+ assert_cached feed_url_for(:about)
0
+ end
0
+
0
+ def test_should_expire_sections_when_publishing_draft
0
+ visitor = visit
0
+ writer = login_as :quentin
0
+
0
+ visit_sections_and_feeds_with visitor
0
+
0
+ assert_caches_page overview_path do
0
+ visitor.get_with_basic 'admin/overview.xml', :login => :quentin
0
+ end
0
+
0
+ assert_expires_page overview_path, section_url_for(:home), feed_url_for(:home) do
0
+ writer.revise contents(:draft), :title => 'This is a new article & title', :draft => 1, :published_at => 5.minutes.ago
0
+ end
0
+
0
+ assert_cached section_url_for(:about)
0
+ assert_cached feed_url_for(:about)
0
+ end
0
+
0
   def test_should_expire_articles_after_editing
0
     visitor = visit
0
     writer = login_as :quentin
0
@@ -245,11 +305,10 @@ class CachingTest < ActionController::IntegrationTest
0
     visitor = visit
0
     writer = login_as :quentin
0
     visit_sections_and_feeds_with visitor
0
- assert_expires_pages feed_url_for(:about) do
0
+ assert_expires_pages feed_url_for(:about), section_url_for(:about) do
0
       writer.revise contents(:site_map), 'sitemap whoo'
0
     end
0
-
0
- assert_cached section_url_for(:about) # paged section only shows the homepage
0
+
0
     assert_cached section_url_for(:home)
0
     assert_cached feed_url_for(:home)
0
 
...
32
33
34
35
 
36
37
38
39
 
40
41
42
...
32
33
34
 
35
36
37
38
 
39
40
41
42
0
@@ -32,11 +32,11 @@ class SectionDropTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_should_get_earliest_article_published_date
0
- assert_equal contents(:welcome).published_at.beginning_of_month.to_date, sections(:home).to_liquid['earliest_month']
0
+ assert_equal contents(:another).published_at.beginning_of_month.to_date, sections(:home).to_liquid['earliest_month']
0
   end
0
   
0
   def test_should_get_month_array
0
- months = sections(:home).articles.collect { |a| a.published_at.beginning_of_month.to_date.to_s(:db) }.uniq.sort.reverse
0
+ months = sections(:home).articles.collect { |a| a.published? ? a.published_at.beginning_of_month.to_date.to_s(:db) : nil }.compact.uniq.sort.reverse
0
     assert_equal months, sections(:home).to_liquid['months'].collect { |d| d.to_s(:db) }
0
   end
0
 end

Comments

    No one has commented yet.