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 !
refactor liquid drops so that @site is available from the current 
context's site value

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2345 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Mon Oct 09 14:49:12 -0700 2006
commit  a061d65dc73cc614c8a443ed8ae06177ff1e34ba
tree    710000d8ad242dad7951e810c2ee02e91c92ccee
parent  89401c31f2bdc0c45cd9bda02eaf5e42cf73844a
...
25
26
27
28
 
29
30
31
...
25
26
27
 
28
29
30
31
0
@@ -25,7 +25,7 @@ class Admin::ArticlesController < Admin::BaseController
0
     @article = site.articles.find(params[:id])
0
     @comments = @article.comments.collect &:to_liquid
0
     Mephisto::Liquid::CommentForm.article = @article
0
- @article = @article.to_liquid(:mode => :single, :site => site)
0
+ @article = @article.to_liquid(:mode => :single)
0
     
0
     render :text => site.render_liquid_for(site.sections.home, :single, 'articles' => [@article], 'article' => @article, 'comments' => @comments, 'site' => site.to_liquid)
0
   end
...
42
43
44
45
46
47
 
 
48
49
50
51
52
...
42
43
44
 
45
 
46
47
48
 
49
50
51
0
@@ -42,11 +42,10 @@ class ApplicationController < ActionController::Base
0
 
0
     def render_liquid_template_for(template_type, assigns = {})
0
       headers["Content-Type"] ||= 'text/html; charset=utf-8'
0
-
0
       if assigns['articles'] && assigns['article'].nil?
0
- assigns['articles'] = assigns['articles'].collect { |a| a.to_liquid :site => site }
0
+ # use collect so it doesn't modify @articles
0
+ assigns['articles'] = assigns['articles'].collect &:to_liquid
0
       end
0
-
0
       status = (assigns.delete(:status) || '200 OK')
0
       @liquid_assigns = assigns
0
       render :text => site.render_liquid_for(@section, template_type, assigns, self), :status => status
...
34
35
36
37
 
38
39
40
...
122
123
124
125
 
126
127
128
...
34
35
36
 
37
38
39
40
...
122
123
124
 
125
126
127
128
0
@@ -34,7 +34,7 @@ class MephistoController < ApplicationController
0
       show_404 and return unless @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
+ 'article' => @article.to_liquid(:mode => :single))
0
     end
0
     
0
     def dispatch_comments
0
@@ -122,7 +122,7 @@ class MephistoController < ApplicationController
0
       find_article if @article.nil?
0
       show_404 and return unless @article || find_article
0
       Mephisto::Liquid::CommentForm.article = @article
0
- @article = @article.to_liquid(:mode => :single, :site => site)
0
+ @article = @article.to_liquid(:mode => :single)
0
       render_liquid_template_for(:single, assigns.merge('articles' => [@article], 'article' => @article))
0
     end
0
     alias dispatch_single show_article_with
...
1
2
3
 
 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
1
2
3
4
5
6
7
8
9
10
 
11
12
13
14
15
16
 
 
17
18
19
0
@@ -1,20 +1,19 @@
0
 class ArticleDrop < BaseDrop
0
   include Mephisto::Liquid::UrlMethods
0
   
0
+ timezone_dates :published_at, :updated_at
0
+
0
   def article() @source end
0
 
0
   def initialize(source, options = {})
0
     super source
0
     @options = options
0
- @site = options.delete(:site) || @source.site
0
     @article_liquid = {
0
       'id' => @source.id,
0
       'title' => @source.title,
0
       'permalink' => @source.permalink,
0
       'body' => @source.body_html,
0
       'excerpt' => (@source.excerpt_html.nil? || @source.excerpt_html.empty? ? nil : @source.excerpt_html),
0
- 'published_at' => (@source.published_at ? @site.timezone.utc_to_local(@source.published_at) : nil),
0
- 'updated_at' => (@source.updated_at ? @site.timezone.utc_to_local(@source.updated_at) : nil),
0
       'comments_count' => @source.comments_count,
0
       'author' => @source.user.to_liquid,
0
       'accept_comments' => @source.accept_comments?,
...
7
8
9
10
 
 
 
11
12
13
...
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
21
22
...
7
8
9
 
10
11
12
13
14
15
...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
0
@@ -7,7 +7,9 @@ class BaseDrop < Liquid::Drop
0
   end
0
   
0
   def context=(current_context)
0
- current_context.registers[:controller].send(:cached_references) << @source if @source
0
+ current_context.registers[:controller].send(:cached_references) << @source if @source && current_context.registers[:controller]
0
+ # @site is set for every drop except SiteDrop, or you get into an infinite loop
0
+ @site = current_context['site'].source if !is_a?(SiteDrop) && @site.nil? && current_context['site']
0
     super
0
   end
0
   
0
@@ -18,4 +20,16 @@ class BaseDrop < Liquid::Drop
0
   def ==(comparison_object)
0
     self.source == (comparison_object.is_a?(self.class) ? comparison_object.source : comparison_object)
0
   end
0
+
0
+ protected
0
+ def self.timezone_dates(*attrs)
0
+ attrs.each do |attr_name|
0
+ module_eval <<-end_eval
0
+ def #{attr_name}
0
+ class << self; attr_reader :#{attr_name}; end
0
+ @#{attr_name} = (@source.#{attr_name} ? @site.timezone.utc_to_local(@source.#{attr_name}) : nil)
0
+ end
0
+ end_eval
0
+ end
0
+ end
0
 end
0
\ No newline at end of file
...
2
3
4
 
 
5
6
7
8
9
 
10
11
12
...
2
3
4
5
6
7
8
9
10
 
11
12
13
14
0
@@ -2,11 +2,13 @@ class CommentDrop < BaseDrop
0
   include Mephisto::Liquid::UrlMethods
0
   include WhiteListHelper
0
   
0
+ timezone_dates :published_at, :updated_at
0
+
0
   def comment() @source end
0
 
0
   def initialize(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 = %w(id author author_email author_ip title).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
0
 
...
40
41
42
43
 
44
45
46
...
40
41
42
 
43
44
45
46
0
@@ -40,7 +40,7 @@ class SectionDrop < BaseDrop
0
     return @pages if @pages
0
     @pages = returning [] do |pages|
0
       @source.articles.each_with_index do |article, i|
0
- pages << article.to_liquid(:page => i.zero?, :site => @context['site'].source)
0
+ pages << article.to_liquid(:page => i.zero?)
0
       end
0
     end
0
   end
...
27
28
29
30
 
31
32
33
...
27
28
29
 
30
31
32
33
0
@@ -27,7 +27,7 @@ module DropFilters
0
 
0
   def monthly_articles(section, date = nil)
0
     date = parse_date(date)
0
- section.source.articles.find_all_in_month(date.year, date.month).collect! { |a| a.to_liquid :site => @context['site'].source, :mode => :single }
0
+ section.source.articles.find_all_in_month(date.year, date.month).collect! { |a| a.to_liquid :mode => :single }
0
   end
0
   
0
   def tagged_articles(tags)
...
212
213
214
 
 
 
 
 
 
 
 
215
216
217
...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
0
@@ -212,6 +212,14 @@ class Test::Unit::TestCase
0
       'domain' => 'example.com'
0
     ) : nil
0
   end
0
+
0
+ # mocks a Liquid::Context
0
+ def mock_context(assigns = {}, registers = {})
0
+ t = Liquid::Template.new
0
+ t.assigns.update assigns
0
+ t.registers.update registers
0
+ Liquid::Context.new t
0
+ end
0
 
0
   # Assert the block redirects to the login
0
   #
...
5
6
7
 
8
9
10
...
5
6
7
8
9
10
11
0
@@ -5,6 +5,7 @@ class ArticleDropTest < Test::Unit::TestCase
0
   
0
   def setup
0
     @article = contents(:welcome).to_liquid :mode => :single
0
+ @article.context = mock_context('site' => sites(:first).to_liquid)
0
   end
0
 
0
   def test_equality
...
4
5
6
7
 
8
9
10
...
38
39
40
41
 
42
43
44
...
136
137
138
 
139
140
141
...
149
150
151
152
 
 
 
153
154
155
...
4
5
6
 
7
8
9
10
...
38
39
40
 
41
42
43
44
...
136
137
138
139
140
141
142
...
150
151
152
 
153
154
155
156
157
158
0
@@ -4,7 +4,7 @@ context "Url Filters" do
0
   include CoreFilters, UrlFilters
0
 
0
   def setup
0
- @context = {'site' => sites(:first).to_liquid, 'section' => sections(:about).to_liquid}
0
+ @context = mock_context 'site' => sites(:first).to_liquid, 'section' => sections(:about).to_liquid
0
   end
0
   
0
   specify "should generate archive url" do
0
@@ -38,7 +38,7 @@ context "Url Filters" do
0
   end
0
 
0
   specify "should generate paged url when site has paged home section" do
0
- @context = {'site' => sites(:hostess).to_liquid, 'section' => sections(:cupcake_home).to_liquid}
0
+ @context = mock_context 'site' => sites(:hostess).to_liquid, 'section' => sections(:cupcake_home).to_liquid
0
     assert_equal "/", page_url(contents(:cupcake_welcome).to_liquid(:page => true))
0
     assert_equal "/welcome-to-cupcake", page_url(contents(:cupcake_welcome).to_liquid)
0
   end
0
@@ -136,6 +136,7 @@ context "Url Filters" do
0
     unencoded = 'Tom & Jerry'
0
     contents(:welcome).title = unencoded
0
     @article = contents(:welcome).to_liquid
0
+ @article.context = @context
0
     @context['section'].instance_variable_get(:@section_liquid)['name'] = unencoded
0
     assert_match %r{>Tom &amp; Jerry<\/a>}, link_to_article(@article)
0
     assert_match %r{>Tom &amp; Jerry<\/a>}, link_to_page(@article)
0
@@ -149,7 +150,9 @@ context "Article Url Filters" do
0
   include CoreFilters, UrlFilters
0
 
0
   def setup
0
- @article = contents(:welcome).to_liquid(:site => sites(:first))
0
+ @context = mock_context 'site' => sites(:first).to_liquid
0
+ @article = contents(:welcome).to_liquid
0
+ @article.context = @context
0
     @permalink = @article.url
0
   end
0
 

Comments

    No one has commented yet.