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 !
add #liquidize helper method that applies the current context to all 
instantiated liquid drops automatically, add basic UserDrop

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2355 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Tue Oct 10 09:48:13 -0700 2006
commit  d11d5c328764b3eeb7fb9607c1963d31ade4cb5b
tree    934cfd58ced31ef5977308ef93eabbb70e0e9f92
parent  e637c8d67b4d2e2f1be63360207d4b678e6fe3c0
...
2
3
4
 
5
6
7
8
9
10
11
12
13
14
 
15
16
17
18
19
20
21
22
23
24
25
26
27
 
 
 
 
28
29
 
30
31
32
33
 
34
35
36
37
 
38
39
40
...
2
3
4
5
6
7
8
9
10
11
 
 
 
 
12
13
14
 
 
15
16
 
 
 
 
 
17
18
19
20
21
22
23
 
24
25
26
27
28
29
30
31
32
 
33
34
35
36
0
@@ -2,39 +2,35 @@ class ArticleDrop < BaseDrop
0
   include Mephisto::Liquid::UrlMethods
0
   
0
   timezone_dates :published_at, :updated_at
0
+ liquid_attributes << :title << :permalink << :comments_count
0
   
0
   def article() @source end
0
 
0
   def initialize(source, options = {})
0
     super source
0
     @options = options
0
- @article_liquid = {
0
- 'id' => @source.id,
0
- 'title' => @source.title,
0
- 'permalink' => @source.permalink,
0
+ @liquid.update \
0
       'body' => @source.body_html,
0
       'excerpt' => (@source.excerpt_html.nil? || @source.excerpt_html.empty? ? nil : @source.excerpt_html),
0
- 'comments_count' => @source.comments_count,
0
- 'author' => @source.user.to_liquid,
0
       'accept_comments' => @source.accept_comments?,
0
       'is_page_home' => (options[:page] == true)
0
- }
0
- end
0
-
0
- def before_method(method)
0
- @article_liquid[method.to_s]
0
   end
0
   
0
+ def author
0
+ @author ||= liquidize(@source.user).first
0
+ end
0
+
0
   def comments
0
- @comments ||= @source.comments.reject(&:new_record?).collect(&:to_liquid)
0
+ @comments ||= liquidize(*@source.comments.reject(&:new_record?))
0
   end
0
   
0
   def sections
0
     @sections ||= @source.sections.inject([]) { |all, s| s.home? ? all : all << s.to_liquid } # your days are numbered, home section!
0
+ @sections ||= liquidize(*@source.sections) { |s| s.home? ? nil : s.to_liquid }
0
   end
0
 
0
   def tags
0
- @tags ||= @source.tags.collect(&:to_liquid)
0
+ @tags ||= liquidize(*@source.tags)
0
   end
0
 
0
   def blog_sections
...
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
20
21
22
...
1
2
3
4
 
 
 
 
 
 
 
 
 
 
5
6
7
8
9
 
10
11
12
13
0
@@ -1,22 +1,13 @@
0
 class AssetDrop < BaseDrop
0
+ liquid_attributes.push(*[:content_type, :size, :filename, :width, :height])
0
   def asset() @source end
0
 
0
- def initialize(source)
0
- @source = source
0
- super source
0
- @asset_liquid = [:id, :content_type, :size, :filename, :width, :height].inject({}) { |h, k| h.merge k.to_s => @source.send(k) }
0
- end
0
-
0
- def before_method(method)
0
- @asset_liquid[method.to_s]
0
- end
0
-
0
   [:image, :movie, :audio, :other, :pdf].each do |content|
0
     define_method("is_#{content}") { @source.send("#{content}?") }
0
   end
0
   
0
   def tags
0
- @tags ||= @source.tags.collect &:name
0
+ @tags ||= liquidize *@source.tags
0
   end
0
 
0
   def path
...
1
 
 
2
3
4
5
6
 
7
8
9
...
12
13
14
15
 
 
 
 
 
16
17
18
...
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
25
26
...
32
33
34
 
 
 
 
35
36
...
1
2
3
4
5
6
7
8
9
10
11
12
...
15
16
17
 
18
19
20
21
22
23
24
25
...
27
28
29
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
...
54
55
56
57
58
59
60
61
62
0
@@ -1,9 +1,12 @@
0
 class BaseDrop < Liquid::Drop
0
+ class_inheritable_reader :liquid_attributes
0
+ write_inheritable_attribute :liquid_attributes, [:id]
0
   attr_reader :source
0
   delegate :hash, :to => :source
0
   
0
   def initialize(source)
0
     @source = source
0
+ @liquid = liquid_attributes.inject({}) { |h, k| h.update k.to_s => @source.send(k) }
0
   end
0
   
0
   def context=(current_context)
0
@@ -12,7 +15,11 @@ class BaseDrop < Liquid::Drop
0
     @site = current_context['site'].source if !is_a?(SiteDrop) && @site.nil? && current_context['site']
0
     super
0
   end
0
-
0
+
0
+ def before_method(method)
0
+ @liquid[method.to_s]
0
+ end
0
+
0
   def eql?(comparison_object)
0
     self == (comparison_object)
0
   end
0
@@ -20,7 +27,22 @@ 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
+
0
+ # converts an array of records to an array of liquid drops, and assigns the given context to each of them
0
+ def self.liquidize(current_context, *records, &block)
0
+ i = -1
0
+ records =
0
+ records.inject [] do |all, r|
0
+ i+=1
0
+ attrs = (block && block.arity == 1) ? [r] : [r, i]
0
+ all << (block ? block.call(*attrs) : r.to_liquid)
0
+ all.last.context = current_context if all.last.is_a?(Liquid::Drop)
0
+ all
0
+ end
0
+ records.compact!
0
+ records
0
+ end
0
+
0
   protected
0
     def self.timezone_dates(*attrs)
0
       attrs.each do |attr_name|
0
@@ -32,4 +54,8 @@ class BaseDrop < Liquid::Drop
0
         end_eval
0
       end
0
     end
0
+
0
+ def liquidize(*records, &block)
0
+ self.class.liquidize(@context, *records, &block)
0
+ end
0
 end
0
\ No newline at end of file
...
3
4
5
 
6
7
8
9
10
11
12
13
14
15
16
 
17
18
19
...
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
12
13
14
15
0
@@ -3,17 +3,13 @@ class CommentDrop < BaseDrop
0
   include WhiteListHelper
0
   
0
   timezone_dates :published_at, :created_at
0
+ liquid_attributes.push(*[:author, :author_email, :author_ip, :title])
0
   
0
   def comment() @source end
0
 
0
   def initialize(source)
0
     super
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
-
0
- def before_method(method)
0
- @comment_liquid[method.to_s]
0
+ @liquid.update 'is_approved' => comment.approved?, 'body' => white_list(comment.body_html)
0
   end
0
   
0
   def author_url
...
1
2
3
 
 
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
17
18
19
...
25
26
27
28
29
30
 
31
32
33
34
35
36
 
37
38
39
40
41
42
43
44
 
 
45
46
47
...
1
2
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
12
13
14
15
16
...
22
23
24
 
 
 
25
26
27
28
 
 
 
29
30
31
32
 
 
 
 
 
33
34
35
36
37
0
@@ -1,19 +1,16 @@
0
 class SectionDrop < BaseDrop
0
   include Mephisto::Liquid::UrlMethods
0
   
0
+ liquid_attributes.push(*[:name, :path, :archive_path])
0
+
0
   def section() @source end
0
   def current() @current == true end
0
 
0
   def initialize(source, current = false)
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)
0
- {:is_blog => :blog?, :is_paged => :paged?, :is_home => :home?}.each { |k, v| @section_liquid[k.to_s] = @source.send(v) }
0
- end
0
-
0
- def before_method(method)
0
- @section_liquid[method.to_s]
0
+ @liquid['articles_count'] = @source.send(:read_attribute, :articles_count)
0
+ {:is_blog => :blog?, :is_paged => :paged?, :is_home => :home?}.each { |k, v| @liquid[k.to_s] = @source.send(v) }
0
   end
0
   
0
   def articles
0
@@ -25,23 +22,16 @@ class SectionDrop < BaseDrop
0
   end
0
 
0
   def latest_articles(limit = nil)
0
- returning @source.articles.find_by_date(:limit => (limit || @source.articles_per_page)) do |articles|
0
- articles.collect! &:to_liquid
0
- end
0
+ liquidize(*@source.articles.find_by_date(:limit => (limit || @source.articles_per_page)))
0
   end
0
 
0
   def latest_comments(limit = nil)
0
- returning @source.find_comments(:limit => (limit || @source.articles_per_page)) do |comments|
0
- comments.collect! &:to_liquid
0
- end
0
+ liquidize(*@source.find_comments(:limit => (limit || @source.articles_per_page)))
0
   end
0
 
0
   def pages
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?)
0
- end
0
+ @pages ||= liquidize(*@source.articles) do |article, i|
0
+ article.to_liquid(:page => i.zero?)
0
     end
0
   end
0
 
...
1
 
2
3
4
...
6
7
8
9
10
11
12
13
14
 
15
16
17
18
 
 
 
 
19
20
21
...
24
25
26
27
28
29
 
30
31
32
33
34
35
36
37
 
38
39
40
...
43
44
45
46
 
47
48
49
50
51
52
53
 
54
55
56
57
58
59
60
 
61
62
63
...
1
2
3
4
5
...
7
8
9
 
 
 
 
 
 
10
11
12
13
 
14
15
16
17
18
19
20
...
23
24
25
 
 
 
26
27
28
29
30
31
 
 
 
32
33
34
35
...
38
39
40
 
41
42
43
44
45
 
 
 
46
47
48
49
50
 
 
 
51
52
53
54
0
@@ -1,4 +1,5 @@
0
 class SiteDrop < BaseDrop
0
+ liquid_attributes.push(*[:host, :subtitle, :title, :articles_per_page, :tag_path, :search_path])
0
   def site() @source end
0
   def current_section() @current_section_liquid end
0
 
0
@@ -6,16 +7,14 @@ class SiteDrop < BaseDrop
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) }
0
- @site_liquid['accept_comments'] = @source.accept_comments?
0
- end
0
-
0
- def before_method(method)
0
- @site_liquid[method.to_s]
0
+ @liquid['accept_comments'] = @source.accept_comments?
0
   end
0
 
0
   def sections
0
- @sections ||= @source.sections.inject([]) { |all, s| all.send(s.home? ? :unshift : :<<, s.to_liquid(s == @current_section)) }
0
+ return @sections if @sections
0
+ @sections = @source.sections.inject([]) { |all, s| all.send(s.home? ? :unshift : :<<, s.to_liquid(s == @current_section)) }
0
+ @sections.each { |s| s.context = @context }
0
+ @sections
0
   end
0
   
0
   def home_section
0
@@ -24,17 +23,13 @@ class SiteDrop < BaseDrop
0
 
0
   def latest_articles(limit = nil)
0
     return @articles if @articles && limit == @source.articles_per_page
0
- articles = returning @source.articles.find_by_date(:limit => (limit || @source.articles_per_page)) do |articles|
0
- articles.collect! &:to_liquid
0
- end
0
+ articles = liquidize(*@source.articles.find_by_date(:limit => (limit || @source.articles_per_page)))
0
     limit == @source.articles_per_page ? (@articles = articles) : articles
0
   end
0
 
0
   def latest_comments(limit = nil)
0
     return @comments if @comments && limit == @source.articles_per_page
0
- comments = returning @source.comments.find(:all, :limit => (limit || @source.articles_per_page)) do |comments|
0
- comments.collect! &:to_liquid
0
- end
0
+ comments = liquidize(*@source.comments.find(:all, :limit => (limit || @source.articles_per_page)))
0
     limit == @source.articles_per_page ? (@comments = comments) : comments
0
   end
0
 
0
@@ -43,21 +38,17 @@ class SiteDrop < BaseDrop
0
     return @section_index[path] if @section_index[path]
0
     @section_index[path] ||= @current_section_liquid if @current_section && @current_section.path == path
0
     @section_index[path] ||= @sections.detect { |s| s['path'] == path } if @sections
0
- @section_index[path] ||= @source.sections.find_by_path(path).to_liquid
0
+ @section_index[path] ||= liquidize(@source.sections.find_by_path(path)).first
0
   end
0
   
0
   def find_child_sections(path)
0
     path_search = path + (path == '' ? '%' : '/%')
0
- returning @source.sections.find(:all, :conditions => ['path != ? AND path LIKE ? AND path NOT LIKE ?', path, path_search, "#{path_search}/%"]) do |sections|
0
- sections.collect! &:to_liquid
0
- end
0
+ liquidize(*@source.sections.find(:all, :conditions => ['path != ? AND path LIKE ? AND path NOT LIKE ?', path, path_search, "#{path_search}/%"]))
0
   end
0
   
0
   def find_descendant_sections(path)
0
     path_search = path + (path == '' ? '%' : '/%')
0
- returning @source.sections.find(:all, :conditions => ['path != ? AND path LIKE ?', path, path_search]) do |sections|
0
- sections.collect! &:to_liquid
0
- end
0
+ liquidize(*@source.sections.find(:all, :conditions => ['path != ? AND path LIKE ?', path, path_search]))
0
   end
0
   
0
   def blog_sections
...
52
53
54
 
 
 
 
 
55
...
52
53
54
55
56
57
58
59
60
0
@@ -52,4 +52,9 @@ module CoreFilters
0
   def assign_to_global(value, name)
0
     @context.scopes.last[name] = value ; nil
0
   end
0
+
0
+ protected
0
+ def liquidize(*records, &block)
0
+ BaseDrop.liquidize(@context, *records, &block)
0
+ end
0
 end
...
27
28
29
30
 
31
32
33
34
 
35
36
37
38
 
39
40
41
42
 
43
44
45
...
27
28
29
 
30
31
32
33
 
34
35
36
37
 
38
39
40
41
 
42
43
44
45
0
@@ -27,18 +27,18 @@ 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 :mode => :single }
0
+ liquidize(*section.source.articles.find_all_in_month(date.year, date.month)) { |r| r.to_liquid :mode => :single }
0
   end
0
   
0
   def tagged_articles(tags)
0
- @context['site'].source.articles.find(:all, :include => :tags, :conditions => ['tags.name in (?)', Tag.parse(tags)], :order => 'contents.created_at desc').collect!(&:to_liquid)
0
+ liquidize(*@context['site'].source.articles.find(:all, :include => :tags, :conditions => ['tags.name in (?)', Tag.parse(tags)], :order => 'contents.created_at desc'))
0
   end
0
   
0
   def assets_by_type(type)
0
- @context['site'].source.assets.find_all_by_content_types([type.to_sym], :all, :order => 'created_at desc').collect!(&:to_liquid)
0
+ liquidize(*@context['site'].source.assets.find_all_by_content_types([type.to_sym], :all, :order => 'created_at desc'))
0
   end
0
   
0
   def tagged_assets(tags)
0
- @context['site'].source.assets.find(:all, :include => :tags, :conditions => ['tags.name in (?)', Tag.parse(tags)], :order => 'assets.created_at desc').collect!(&:to_liquid)
0
+ liquidize(*@context['site'].source.assets.find(:all, :include => :tags, :conditions => ['tags.name in (?)', Tag.parse(tags)], :order => 'assets.created_at desc'))
0
   end
0
 end
0
\ No newline at end of file
...
85
86
87
88
 
89
90
91
...
85
86
87
 
88
89
90
91
0
@@ -85,7 +85,7 @@ class User < ActiveRecord::Base
0
   end
0
 
0
   def to_liquid
0
- [:login, :email].inject({}) { |hsh, attr_name| hsh.update attr_name.to_s => send(attr_name) }
0
+ UserDrop.new self
0
   end
0
 
0
   protected
...
218
219
220
221
 
 
 
 
 
 
 
222
223
224
...
218
219
220
 
221
222
223
224
225
226
227
228
229
230
0
@@ -218,7 +218,13 @@ class Test::Unit::TestCase
0
     t = Liquid::Template.new
0
     t.assigns.update assigns
0
     t.registers.update registers
0
- Liquid::Context.new t
0
+ returning Liquid::Context.new(t) do |context|
0
+ assigns.keys.each { |k| context[k].context = context }
0
+ end
0
+ end
0
+
0
+ def liquidize(*records, &block)
0
+ BaseDrop.liquidize(@context, *records, &block)
0
   end
0
 
0
   # Assert the block redirects to the login
...
5
6
7
8
 
9
10
11
...
19
20
21
22
 
23
24
25
26
27
28
29
 
30
31
32
...
52
53
54
55
 
56
57
58
...
5
6
7
 
8
9
10
11
...
19
20
21
 
22
23
24
25
26
27
28
 
29
30
31
32
...
52
53
54
 
55
56
57
58
0
@@ -5,7 +5,7 @@ context "Drop Filters" do
0
 
0
   def setup
0
     @site = sites(:first).to_liquid
0
- @context = {'site' => @site, 'section' => sections(:about).to_liquid}
0
+ @context = mock_context 'site' => @site, 'section' => sections(:about).to_liquid
0
   end
0
 
0
   specify "should find section by path" do
0
@@ -19,14 +19,14 @@ context "Drop Filters" do
0
   end
0
 
0
   specify "should find latest articles by section" do
0
- section = sections(:home).to_liquid
0
+ section = liquidize(sections(:home)).first
0
     assert_models_equal [contents(:welcome), contents(:another)], latest_articles(section).collect(&:source)
0
     assert_models_equal [contents(:welcome), contents(:another)], latest_articles(section, 2).collect(&:source)
0
     assert_equal contents(:welcome), latest_article(section).source
0
   end
0
 
0
   specify "should find latest comments by section" do
0
- section = sections(:home).to_liquid
0
+ section = liquidize(sections(:home)).first
0
     assert_models_equal [contents(:welcome_comment)], latest_comments(section).collect(&:source)
0
     assert_models_equal [contents(:welcome_comment)], latest_comments(section, 1).collect(&:source)
0
   end
0
@@ -52,7 +52,7 @@ context "Drop Filters" do
0
   end
0
 
0
   specify "should find articles by month" do
0
- assert_models_equal sections(:home).articles.find_all_in_month(Time.now.year, Time.now.month), monthly_articles(sections(:home).to_liquid).collect(&:source)
0
+ assert_models_equal sections(:home).articles.find_all_in_month(Time.now.year, Time.now.month), monthly_articles(liquidize(sections(:home)).first).collect(&:source)
0
   end
0
 
0
   specify "should find movies" do
...
41
42
43
44
 
45
46
47
48
 
49
50
51
...
41
42
43
 
44
45
46
47
48
49
50
51
52
0
@@ -41,11 +41,12 @@ class SectionDropTest < Test::Unit::TestCase
0
   end
0
 end
0
 
0
-context "Section Contets" do
0
+context "Section Contents" do
0
   fixtures :sites, :sections, :contents, :assigned_sections
0
 
0
   def setup
0
     @section = sections(:home).to_liquid
0
+ @section.context = mock_context
0
   end
0
 
0
   specify "should list articles" do
...
5
6
7
 
8
9
10
...
27
28
29
 
30
31
32
...
5
6
7
8
9
10
11
...
28
29
30
31
32
33
34
0
@@ -5,6 +5,7 @@ class SiteDropTest < Test::Unit::TestCase
0
   
0
   def setup
0
     @site = sites(:first).to_liquid
0
+ @site.context = mock_context
0
   end
0
 
0
   def test_equality
0
@@ -27,6 +28,7 @@ class SiteDropTest < Test::Unit::TestCase
0
   
0
   def test_should_show_current_section
0
     @site = SiteDrop.new(sites(:first), sections(:about))
0
+ @site.context = mock_context
0
     assert_equal sections(:about), @site.current_section.source
0
     assert_equal [false, true, false, false, false, false, false], @site.sections.collect(&:current)
0
   end
...
137
138
139
140
 
141
142
143
...
137
138
139
 
140
141
142
143
0
@@ -137,7 +137,7 @@ context "Url Filters" do
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
+ @context['section'].instance_variable_get(:@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
     assert_match %r{>Tom &amp; Jerry<\/a>}, link_to_section(@context['section'])

Comments

    No one has commented yet.