Skip to content

Commit

Permalink
added if_attachments tag (works like if_children)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihoka committed Oct 16, 2008
1 parent 99436e6 commit f07ba43
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
24 changes: 6 additions & 18 deletions app/models/page_attachment_associations.rb
@@ -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
Expand All @@ -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
Expand Down
47 changes: 40 additions & 7 deletions app/models/page_attachment_tags.rb
Expand Up @@ -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.
Expand All @@ -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
6 changes: 6 additions & 0 deletions test/functional/page_attachments_extension_test.rb
Expand Up @@ -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>}
Expand Down

0 comments on commit f07ba43

Please sign in to comment.