Skip to content
This repository
Browse code

Fix terribad looping block antipattern with slightly less terribad one.

  • Loading branch information...
commit 347153f599791d42c4aebeafefae953e78a7f9b8 1 parent 01f7b9f
Jason Webster authored
78 lib/tumblargh/renderer.rb
@@ -61,60 +61,7 @@ def should_render?
61 61 end
62 62 end
63 63
64   - # Posts Loop
65   - #
66   - # {block:Posts} is executed once for each post. Some post related tags can
67   - # exist outside of a `type` block, such as {Title} or {Permalink}, so
68   - # they should be defined here
69   - class Posts < Base
70 64
71   - contextual_tag :post_id, :id
72   - contextual_tag :post_type, :type
73   - contextual_tag :title
74   - contextual_tag :caption
75   -
76   - def permalink
77   - url = context.post_url
78   -
79   - url.gsub(/^http:\/\/[^\/]+/, '')
80   - end
81   -
82   - # TODO: This should not be necessary
83   - def permalink?
84   - context.context.permalink?
85   - end
86   -
87   - def post_notes_url
88   - # http://bikiniatoll.tumblr.com/notes/1377511430/vqS0xw8sm
89   - "/notes/#{context.id}/"
90   - end
91   -
92   - def reblog_url
93   - "/reblog/#{context.reblog_key}"
94   - end
95   -
96   - def render
97   - sig, type, *nodes = node
98   -
99   - res = nodes.map do |n|
100   - renderer = Renderer.factory(n, self)
101   -
102   - # TODO LOLOLOLOLOLOLOL
103   - if renderer.class.name == 'Tumblargh::Renderer::Blocks::Tags'
104   - context.posts.tags.map do |t|
105   - t.context = self
106   - post_renderer = renderer.class.new(n, t)
107   - post_renderer.render
108   - end
109   - else
110   - renderer.render
111   - end
112   - end
113   -
114   - res.flatten.join('')
115   - end
116   -
117   - end
118 65
119 66 # Common post blocks
120 67 class Title < Base
@@ -261,26 +208,6 @@ def should_render?
261 208 end
262 209 end
263 210
264   - # Rendered for each of a post's tags.
265   - # TODO: Render for each tag in a post
266   - class Tags < Base
267   - def tag
268   - context.name
269   - end
270   -
271   - def url_safe_tag
272   - escape_url(tag)
273   - end
274   -
275   - def tag_url
276   - "/tagged/#{url_safe}"
277   - end
278   -
279   - def tag_url_chrono
280   - "#{tag_url}/chrono"
281   - end
282   - end
283   -
284 211
285 212 # Rendered on index (post) pages.
286 213 class IndexPage < Base
@@ -378,10 +305,11 @@ def likes
378 305 require 'tumblargh/renderer/blocks/answer'
379 306 require 'tumblargh/renderer/blocks/audio'
380 307 require 'tumblargh/renderer/blocks/dates'
  308 + require 'tumblargh/renderer/blocks/navigation'
381 309 require 'tumblargh/renderer/blocks/notes'
  310 + require 'tumblargh/renderer/blocks/posts'
382 311 require 'tumblargh/renderer/blocks/reblogs'
383   - require 'tumblargh/renderer/blocks/navigation'
384   -
  312 + require 'tumblargh/renderer/blocks/tags'
385 313
386 314 end
387 315 end
2  lib/tumblargh/renderer/base.rb
@@ -29,7 +29,7 @@ def initialize(node, context)
29 29
30 30 def context_post
31 31 real_post = context
32   - while not real_post.is_a?(API::Post)
  32 + while not real_post.is_a?(::Tumblargh::Resource::Post)
33 33 real_post = real_post.context
34 34 end
35 35
4 lib/tumblargh/renderer/blocks/base.rb
@@ -24,9 +24,7 @@ def render
24 24 sig, type, *nodes = node
25 25
26 26 res = nodes.map do |n|
27   - # puts "#{self.class.name} --> #{self.context.class.name}"
28   - renderer = Renderer.factory(n, self)
29   - renderer.render
  27 + Renderer.factory(n, self).render
30 28 end
31 29
32 30 " #{res.join('')} "
50 lib/tumblargh/renderer/blocks/posts.rb
... ... @@ -0,0 +1,50 @@
  1 +module Tumblargh
  2 + module Renderer
  3 + module Blocks
  4 + # Posts Loop
  5 + #
  6 + # {block:Posts} is executed once for each post. Some post related tags can
  7 + # exist outside of a `type` block, such as {Title} or {Permalink}, so
  8 + # they should be defined here
  9 + class Posts < Base
  10 +
  11 + contextual_tag :post_id, :id
  12 + contextual_tag :post_type, :type
  13 + contextual_tag :title
  14 + contextual_tag :caption
  15 +
  16 + def permalink
  17 + url = context.post_url
  18 + url.gsub(/^http:\/\/[^\/]+/, '')
  19 + end
  20 +
  21 + def permalink?
  22 + context.permalink?
  23 + end
  24 +
  25 + def post_notes_url
  26 + # http://bikiniatoll.tumblr.com/notes/1377511430/vqS0xw8sm
  27 + "/notes/#{context.id}/"
  28 + end
  29 +
  30 + def reblog_url
  31 + "/reblog/#{context.reblog_key}"
  32 + end
  33 +
  34 + def render
  35 + if context.is_a? Resource::Post
  36 + super
  37 + else
  38 + posts = permalink? ? [context.posts.first] : context.posts
  39 +
  40 + posts.map do |post|
  41 + post.context = self
  42 + self.class.new(node, post).render
  43 + end.flatten.join('')
  44 + end
  45 + end
  46 +
  47 + end
  48 + end
  49 + end
  50 +end
37 lib/tumblargh/renderer/blocks/tags.rb
... ... @@ -0,0 +1,37 @@
  1 +module Tumblargh
  2 + module Renderer
  3 + module Blocks
  4 + # Rendered for each of a post's tags.
  5 + # TODO: Render for each tag in a post
  6 + class Tags < Base
  7 + def tag
  8 + context.name
  9 + end
  10 +
  11 + def url_safe_tag
  12 + escape_url(tag)
  13 + end
  14 +
  15 + def tag_url
  16 + "/tagged/#{url_safe}"
  17 + end
  18 +
  19 + def tag_url_chrono
  20 + "#{tag_url}/chrono"
  21 + end
  22 +
  23 + def render
  24 + if context.is_a? Resource::Tag
  25 + super
  26 + else
  27 + context.tags.map do |tag|
  28 + tag.context = self
  29 + self.class.new(node, tag).render
  30 + end.flatten.join('')
  31 + end
  32 + end
  33 +
  34 + end
  35 + end
  36 + end
  37 +end
21 lib/tumblargh/renderer/document.rb
@@ -61,24 +61,9 @@ def custom_value_for_type(type, key)
61 61 # END TAGS ------
62 62
63 63 def render
64   - res = node.map do |n|
65   - renderer = Renderer.factory(n, self)
66   -
67   - # TODO LOLOLOLOLOLOLOL
68   - if renderer.class.name == 'Tumblargh::Renderer::Blocks::Posts'
69   - posts = permalink? ? [context.posts.first] : context.posts
70   -
71   - posts.map do |p|
72   - p.context = self
73   - post_renderer = renderer.class.new(n, p)
74   - post_renderer.render
75   - end
76   - else
77   - renderer.render
78   - end
79   - end
80   -
81   - res.flatten.join('')
  64 + node.map do |n|
  65 + Renderer.factory(n, self).render
  66 + end.flatten.join('')
82 67 end
83 68 end
84 69 end
17 spec/renderer/blocks/posts_spec.rb
... ... @@ -0,0 +1,17 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Tumblargh::Renderer::Blocks::Posts do
  4 +
  5 + before do
  6 + json_path = File.join(FIXTURE_PATH, "data", "staff.tumblr.com-2012-05-06", "posts.json")
  7 + @json = ActiveSupport::JSON.decode(open(json_path).read)["response"].with_indifferent_access
  8 + end
  9 +
  10 + it "should render the correct amount of posts" do
  11 + theme = "{block:Posts}!{/block:Posts}"
  12 +
  13 + result = Tumblargh.render_html(theme, @json)
  14 + result.count("!").should eql @json[:posts].size
  15 + end
  16 +
  17 +end

0 comments on commit 347153f

Please sign in to comment.
Something went wrong with that request. Please try again.