Skip to content
Browse files

Added "parent" attribute to categories:each

  • Loading branch information...
1 parent 7e2eaae commit 980d1ed75863a0bdafdcd7c0da406f07e55e9c49 @jstirk committed Sep 18, 2009
Showing with 36 additions and 9 deletions.
  1. +7 −7 README.rdoc
  2. +20 −2 lib/simple_product_manager_tag.rb
  3. +9 −0 spec/lib/category_tags_spec.rb
View
14 README.rdoc
@@ -45,8 +45,8 @@ The categories and products are exposed to the site using the following tags:
=== Category Tags
- <r:category:find where="" tag=""> ... </r:category:find>
- <r:categories:each where="" tag="" order=""> ... </r:categories:each>
+ <r:category:find [where=""] [tag=""]> ... </r:category:find>
+ <r:categories:each [where=""] [tag=""] [order=""] [parent="id or title"]> ... </r:categories:each>
<r:category:id />
<r:category:title />
<r:category:description />
@@ -62,12 +62,12 @@ only within that Category's products.
In this case, only Products directly owned by the current Category are shown -
they do not recurse down into sub-Categories.
- <r:product:find where=""> ... </r:product:find>
- <r:products:each where="" order=""> ... </r:products:each>
+ <r:product:find [where=""]> ... </r:product:find>
+ <r:products:each [where=""] [order=""]> ... </r:products:each>
<r:product:id />
<r:product:title />
<r:product:description />
- <r:product:price precision="" unit="" separator="" delimiter="" />
+ <r:product:price [precision="2"] [unit="$"] [separator="."] [delimiter=","] />
<r:product:image />
<r:product:photo_url />
<r:product:field name="" />
@@ -79,8 +79,8 @@ Subcategory tags are only valid within the <r:category:find> or <r:categories:ea
They will restrict their search to include only Categories which are direct descendants of
the currently selected Category.
- <r:subcategory:find where="" tag=""> ... </r:subcategory:find>
- <r:subcategories:each where="" tag="" order=""> ... </r:subcategories:each>
+ <r:subcategory:find [where=""] [tag=""]> ... </r:subcategory:find>
+ <r:subcategories:each [where=""] [tag=""] [order=""]> ... </r:subcategories:each>
<r:subcategory:id />
<r:subcategory:title />
<r:subcategory:description />
View
22 lib/simple_product_manager_tag.rb
@@ -135,7 +135,8 @@ module SimpleProductManagerTag
end
end
- desc "Iterate over all categories in the system, optionally sorted by the field specified by 'order', or constrained by 'where' or 'tag'."
+ desc "Iterate over all categories in the system, optionally sorted by the field specified by 'order', or constrained by 'where', 'tag' or 'parent'
+If specified, 'parent' can be either the ID of the parent Category, or it's title."
tag 'categories:each' do |tag|
attr = tag.attr.symbolize_keys
order=attr[:order] || 'title ASC'
@@ -147,8 +148,25 @@ module SimpleProductManagerTag
where=[where, tag_snippet].compact.join(' AND ')
end
+ if attr[:parent] then
+ if attr[:parent] =~ /^\d+$/ then
+ # It's a number, use it as an ID
+ parent_id=attr[:parent].to_i
+ else
+ parent=Category.find(:first, :conditions => { :title => attr[:parent] })
+ parent_id=parent.id
+ end
+ tag_snippet="parent_id = #{parent_id}"
+ where=[where, tag_snippet].compact.join(' AND ')
+ end
+
result = []
- Category.find_all_top_level(:conditions => where, :order => order).each do |category|
+ if attr[:parent] then
+ cats=Category.find(:all, :conditions => where, :order => order)
+ else
+ cats=Category.find_all_top_level(:conditions => where, :order => order)
+ end
+ cats.each do |category|
tag.locals.category = category
result << tag.expand
end
View
9 spec/lib/category_tags_spec.rb
@@ -37,6 +37,15 @@
it "should restrict OK by tags with ordering" do
pages(:home).should render('<r:categories:each tag="Gluten Free" order="title DESC"><r:category:title /><br /></r:categories:each>').as('Salads<br />Pastries<br />')
end
+
+ it "should restrict OK by parent id" do
+ c=Category.find_by_title('Bread')
+ pages(:home).should render("<r:categories:each parent=\"#{c.id}\" order=\"title ASC\"><r:category:title /><br /></r:categories:each>").as('Sourdough Breads<br />Spelt Breads<br />Wholemeal Breads<br />')
+ end
+
+ it "should restrict OK by parent title" do
+ pages(:home).should render("<r:categories:each parent=\"Bread\" order=\"title ASC\"><r:category:title /><br /></r:categories:each>").as('Sourdough Breads<br />Spelt Breads<br />Wholemeal Breads<br />')
+ end
end
%w(id title description).each do |type|

0 comments on commit 980d1ed

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