Permalink
Browse files

added if_attachments tag (works like if_children)

  • Loading branch information...
1 parent 99436e6 commit f07ba434e4a6742573108a9d2f6af74f3e0082cd @ihoka ihoka committed Oct 16, 2008
@@ -1,18 +1,7 @@
module PageAttachmentAssociations
def self.included(base)
base.class_eval {
- has_many :attachments, :class_name => "PageAttachment", :dependent => :destroy, :order => 'position' do
- def by_extensions(extensions, options={})
- conditions = unless extensions.blank?
- [ extensions.map { |ext| "page_attachments.filename LIKE ?"}.join(' OR '),
- *extensions.map { |ext| "%#{ext}" } ]
- else
- nil
- end
- find(:all, options.merge(:conditions => conditions))
- end
- end
-
+ has_many :attachments, :class_name => "PageAttachment", :dependent => :destroy, :order => 'position'
attr_accessor :add_attachments
after_save :save_attachments
include InstanceMethods
@@ -28,14 +17,13 @@ def attachment(name)
def save_attachments
if @add_attachments && ! @add_attachments['file'].blank?
- i = 0
+ i = 0
@add_attachments['file'].each do |page_attach|
attachments << PageAttachment.new(
- :uploaded_data => page_attach,
- :title => @add_attachments['title'][i],
- :description => @add_attachments['description'][i]
- )
- i += 1
+ :uploaded_data => page_attach,
+ :title => @add_attachments['title'][i],
+ :description => @add_attachments['description'][i])
+ i += 1
end
end
@add_attachments = nil
@@ -180,18 +180,28 @@ class TagError < StandardError; end
}
tag "attachment:each" do |tag|
page = tag.locals.page
- order = tag.attr["order"] || "asc"
- by = tag.attr["by"] || "position"
- limit = tag.attr["limit"] || nil
- offset = tag.attr["offset"] || nil
- extensions = tag.attr["extensions"] && tag.attr["extensions"].split('|') || []
+
returning String.new do |output|
- page.attachments.by_extensions(extensions, :order => [by, order].join(" "), :limit => limit, :offset => offset).each do |att|
+ page.attachments.find(:all, attachments_find_options(tag)).each do |att|
tag.locals.attachment = att
output << tag.expand
end
end
- end
+ end
+
+ desc %{
+ Renders the contained elements only if the current contextual page has one or
+ more attachments. The @min_count@ attribute specifies the minimum number of required
+ attachments. You can also filter by extensions with the @extensions@ attribute.
+
+ *Usage:*
+ <pre><code><r:if_attachments [min_count="n"] [extensions="doc|pdf"]>...</r:if_attachments></code></pre>
+ }
+ tag "if_attachments" do |tag|
+ count = tag.attr['min_count'] && tag.attr['min_count'].to_i || 0
+ attachments = tag.locals.page.attachments.count(:conditions => attachments_find_options(tag)[:conditions])
+ tag.expand if attachments >= count
+ end
desc %{
Renders the 'extension' virtual attribute of the attachment, extracted from filename.
@@ -213,4 +223,27 @@ class TagError < StandardError; end
attachment = tag.locals.attachment
attachment.filename[/\.(\w+)$/, 1]
end
+
+ private
+ def attachments_find_options(tag)
+ attr = tag.attr.symbolize_keys
+
+ extensions = attr[:extensions] && attr[:extensions].split('|') || []
+ conditions = unless extensions.blank?
+ [ extensions.map { |ext| "page_attachments.filename LIKE ?"}.join(' OR '),
+ *extensions.map { |ext| "%.#{ext}" } ]
+ else
+ nil
+ end
+
+ by = attr[:by] || "position"
+ order = attr[:order] || "asc"
+
+ options = {
+ :order => "#{by} #{order}",
+ :limit => attr[:limit] || nil,
+ :offset => attr[:offset] || nil,
+ :conditions => conditions
+ }
+ end
end
@@ -132,6 +132,12 @@ def test_filter_by_extension
assert_renders "rails.png", %{<r:attachment:each extensions="png"><r:filename/></r:attachment:each>}
assert_renders "rails.pngfoo.txt", %{<r:attachment:each extensions="png|txt"><r:filename/></r:attachment:each>}
end
+
+ def test_if_attachment_tag
+ assert_renders "content", %{<r:if_attachments>content</r:if_attachments>}
+ assert_renders "", %{<r:if_attachments min_count="3">content</r:if_attachments>}
+ assert_renders "content", %{<r:if_attachments min_count="1" extensions="png">content</r:if_attachments>}
+ end
def test_extension_tag
assert_renders "pngtxt", %{<r:attachment:each><r:extension/></r:attachment:each>}

0 comments on commit f07ba43

Please sign in to comment.