Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixing scope="current_page"

  • Loading branch information...
commit 007475b862c2c1de84d0bf90065bfc642198987b 1 parent a2f0ba2
@jomz authored
Showing with 39 additions and 24 deletions.
  1. +39 −24 app/models/radius_tags.rb
View
63 app/models/radius_tags.rb
@@ -5,36 +5,24 @@ module RadiusTags
class TagError < StandardError; end
desc %{
+ Expands if a <pre><r:tagged with="" /></pre> call would return items. Takes the same options as the 'tagged' tag.
+ The <pre><r:unless_tagged with="" /></pre> is also available.
+ }
+ tag "if_tagged" do |tag|
+ tag.expand unless find_with_tag_options(tag).empty?
+ end
+ tag "unless_tagged" do |tag|
+ tag.expand if find_with_tag_options(tag).empty?
+ end
+
+ desc %{
Find all pages with certain tags, within in an optional scope. Additionally, you may set with_any to true to select pages that have any of the listed tags (opposed to all listed tags which is the provided default).
*Usage:*
<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|
- options = tagged_with_options(tag)
- with_any = tag.attr['with_any'] || false
- scope_attr = tag.attr['scope'] || '/'
- result = []
- 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]
-
- scope = scope_attr == 'current_page' ? tag.locals.page : Page.find_by_url(scope_attr)
- return "The scope attribute must be a valid url to an existing page." if scope.class_name.eql?('FileNotFoundPage')
-
- 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
- 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
- end
- end
- result
+ find_with_tag_options(tag)
end
desc "Render a Tag cloud"
@@ -142,6 +130,33 @@ def tag_item_url(name)
"#{Radiant::Config['tags.results_page_url']}?tag=#{name}"
end
+ def find_with_tag_options(tag)
+ options = tagged_with_options(tag)
+ with_any = tag.attr['with_any'] || false
+ scope_attr = tag.attr['scope'] || '/'
+ result = []
+ 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]
+
+ scope = scope_attr == 'current_page' ? Page.find_by_url(@request.request_uri) : Page.find_by_url(scope_attr)
+ return "The scope attribute must be a valid url to an existing page." if scope.class_name.eql?('FileNotFoundPage')
+
+ 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
+ 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
+ end
+ end
+ result
+ end
+
def tagged_with_options(tag)
attr = tag.attr.symbolize_keys
Please sign in to comment.
Something went wrong with that request. Please try again.