Skip to content

Commit

Permalink
refactoring so that results can be re-used within <r:if_tagged>
Browse files Browse the repository at this point in the history
  • Loading branch information
jomz committed Jun 8, 2009
1 parent 336a4fd commit c37e241
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions app/models/radius_tags.rb
Expand Up @@ -9,7 +9,8 @@ class TagError < StandardError; end
The <pre><r:unless_tagged with="" /></pre> is also available.
}
tag "if_tagged" do |tag|
tag.expand unless find_with_tag_options(tag).empty?
tag.locals.tagged_results = find_with_tag_options(tag)
tag.expand unless tag.locals.tagged_results.empty?
end
tag "unless_tagged" do |tag|
tag.expand if find_with_tag_options(tag).empty?
Expand All @@ -22,7 +23,17 @@ class TagError < StandardError; end
<pre><code><r:tagged with="shoes diesel" [scope="/fashion/cult-update"] [with_any="true"] [offset="number"] [limit="number"] [by="attribute"] [order="asc|desc"]>...</r:tagged></code></pre>
}
tag "tagged" do |tag|
find_with_tag_options(tag)
unless tag.locals.tagged_results.nil? # We're inside an r:if_tagged, so results are already available;
results = tag.locals.tagged_results
else
results = find_with_tag_options(tag)
end
output = []
results.each do |page|
tag.locals.page = page
output << tag.expand
end
output
end

desc %{
Expand Down Expand Up @@ -159,7 +170,7 @@ def find_with_tag_options(tag)
options = tagged_with_options(tag)
with_any = tag.attr['with_any'] || false
scope_attr = tag.attr['scope'] || '/'
result = []
results = []
raise TagError, "`tagged' tag must contain a `with' attribute." unless (tag.attr['with'] || tag.locals.page.class_name = TagSearchPage)
ttag = tag.attr['with'] || @request.parameters[:tag]

Expand All @@ -169,17 +180,15 @@ def find_with_tag_options(tag)
if with_any
Page.tagged_with_any(ttag, options).each do |page|
next unless (page.ancestors.include?(scope) or page == scope)
tag.locals.page = page
result << tag.expand
results << page
end
else
Page.tagged_with(ttag, options).each do |page|
next unless (page.ancestors.include?(scope) or page == scope)
tag.locals.page = page
result << tag.expand
results << page
end
end
result
results
end

def tagged_with_options(tag)
Expand Down

0 comments on commit c37e241

Please sign in to comment.