backend for file tags complete
Oleg
committed Sep 28, 2011
commit 9930f44b1cd58a61be0c927b5cc49173c6c07d2d
Showing 4
changed files with
100 additions
and 11 deletions
comfortable_mexican_sofa/tags/page_files.rb b/lib/comfortable_mexican_sofa/tags/page_files.rb
+36
-1
| @@ | @@ -1,8 +1,43 @@ |
| - | class ComfortableMexicanSofa::Tag::PageFiles < ComfortableMexicanSofa::Tag::PageFile |
| + | class ComfortableMexicanSofa::Tag::PageFiles |
| + | include ComfortableMexicanSofa::Tag |
| def self.regex_tag_signature(label = nil) | |
| label ||= /[\w\-]+/ | |
| /\{\{\s*cms:page_files:(#{label}):?(.*?)\s*\}\}/ | |
| end | |
| + | def type |
| + | %w(partial url image link).member?(params[0]) ? params[0] : 'url' |
| + | end |
| + | |
| + | def content |
| + | block.files |
| + | end |
| + | |
| + | def render |
| + | if (files = block.files).present? |
| + | case self.type |
| + | when 'url' |
| + | files.collect do |file| |
| + | file.file.url |
| + | end.join(', ') |
| + | when 'link' |
| + | files.collect do |file| |
| + | "<a href='#{file.file.url}' target='_blank'>#{file.label}</a>" |
| + | end.join(' ') |
| + | when 'image' |
| + | files.collect do |file| |
| + | "<img src='#{file.file.url}' alt='#{file.label}' />" |
| + | end.join(' ') |
| + | when 'partial' |
| + | path = params[1] || 'partials/page_files' |
| + | ps = (self.params[2..-1] || []).collect_with_index{|p, i| ":param_#{i+1} => '#{p}'"}.join(', ') |
| + | ps = ps.present?? ", #{ps}" : '' |
| + | "<%= render :partial => '#{path}', :locals => {:identifier => #{files.collect(&:id)}#{ps}} %>" |
| + | end |
| + | else |
| + | '' |
| + | end |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |
test/unit/models/block_test.rb
+3
-3
| @@ | @@ -111,13 +111,13 @@ class CmsBlockTest < ActiveSupport::TestCase |
| :slug => 'test_page', | |
| :parent_id => cms_pages(:default).id, | |
| :blocks_attributes => [ | |
| - | { :label => 'file', |
| + | { :label => 'files', |
| :content => [fixture_file_upload('files/valid_image.jpg'), fixture_file_upload('files/invalid_file.gif')] } | |
| ] | |
| ) | |
| assert_equal 1, page.blocks.count | |
| block = page.blocks.first | |
| - | assert_equal 'file', block.label |
| + | assert_equal 'files', block.label |
| assert_equal nil, block.content | |
| assert_equal 2, block.files.count | |
| assert_equal ['valid_image.jpg', 'invalid_file.gif'], block.files.collect(&:file_file_name) | |
| @@ | @@ -128,7 +128,7 @@ class CmsBlockTest < ActiveSupport::TestCase |
| assert_difference 'Cms::File.count', 2 do | |
| page.update_attributes!( | |
| :blocks_attributes => [ | |
| - | { :label => 'file', |
| + | { :label => 'files', |
| :content => [fixture_file_upload('files/valid_image.jpg'), fixture_file_upload('files/invalid_file.gif')] } | |
| ] | |
| ) | |
test/unit/tags/page_file_test.rb
+4
-4
| @@ | @@ -47,19 +47,19 @@ class PageFileTagTest < ActiveSupport::TestCase |
| assert_equal "/system/files/#{file.id}/original/valid_image.jpg?#{timestamp}", tag.render | |
| assert tag = ComfortableMexicanSofa::Tag::PageFile.initialize_tag(page, '{{ cms:page_file:file:link }}') | |
| - | assert_equal "<a href='/system/files/593363171/original/valid_image.jpg?#{timestamp}' target='_blank'>file</a>", |
| + | assert_equal "<a href='/system/files/#{file.id}/original/valid_image.jpg?#{timestamp}' target='_blank'>file</a>", |
| tag.render | |
| assert tag = ComfortableMexicanSofa::Tag::PageFile.initialize_tag(page, '{{ cms:page_file:file:link:link label }}') | |
| - | assert_equal "<a href='/system/files/593363171/original/valid_image.jpg?#{timestamp}' target='_blank'>link label</a>", |
| + | assert_equal "<a href='/system/files/#{file.id}/original/valid_image.jpg?#{timestamp}' target='_blank'>link label</a>", |
| tag.render | |
| assert tag = ComfortableMexicanSofa::Tag::PageFile.initialize_tag(page, '{{ cms:page_file:file:image }}') | |
| - | assert_equal "<img src='/system/files/593363171/original/valid_image.jpg?#{timestamp}' alt='file' />", |
| + | assert_equal "<img src='/system/files/#{file.id}/original/valid_image.jpg?#{timestamp}' alt='file' />", |
| tag.render | |
| assert tag = ComfortableMexicanSofa::Tag::PageFile.initialize_tag(page, '{{ cms:page_file:file:image:image alt }}') | |
| - | assert_equal "<img src='/system/files/593363171/original/valid_image.jpg?#{timestamp}' alt='image alt' />", |
| + | assert_equal "<img src='/system/files/#{file.id}/original/valid_image.jpg?#{timestamp}' alt='image alt' />", |
| tag.render | |
| assert tag = ComfortableMexicanSofa::Tag::PageFile.initialize_tag(page, '{{ cms:page_file:file:partial }}') | |
test/unit/tags/page_files_test.rb
+57
-3
| @@ | @@ -3,15 +3,69 @@ require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| class PageFilesTagTest < ActiveSupport::TestCase | |
| def test_initialize_tag | |
| - | flunk |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag( |
| + | cms_pages(:default), '{{ cms:page_files:label }}' |
| + | ) |
| + | assert 'url', tag.type |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag( |
| + | cms_pages(:default), '{{ cms:page_files:label:partial }}' |
| + | ) |
| + | assert 'partial', tag.type |
| end | |
| def test_initialize_tag_failure | |
| - | flunk |
| + | [ |
| + | '{{cms:page_files}}', |
| + | '{{cms:not_page_files:label}}', |
| + | '{not_a_tag}' |
| + | ].each do |tag_signature| |
| + | assert_nil ComfortableMexicanSofa::Tag::PageFiles.initialize_tag( |
| + | cms_pages(:default), tag_signature |
| + | ) |
| + | end |
| end | |
| def test_content_and_render | |
| - | flunk |
| + | page = cms_pages(:default) |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag( |
| + | page, '{{ cms:page_files:files }}' |
| + | ) |
| + | assert_equal [], tag.content |
| + | assert_equal '', tag.render |
| + | |
| + | page.update_attributes!( |
| + | :blocks_attributes => [ |
| + | { :label => 'files', |
| + | :content => [fixture_file_upload('files/valid_image.jpg'), fixture_file_upload('files/invalid_file.gif')] } |
| + | ] |
| + | ) |
| + | files = tag.block.files |
| + | file_a, file_b = files |
| + | timestamp = file_a.updated_at.to_f.to_i |
| + | |
| + | assert_equal files, tag.content |
| + | assert_equal "/system/files/#{file_a.id}/original/valid_image.jpg?#{timestamp}, /system/files/#{file_b.id}/original/invalid_file.gif?#{timestamp}", tag.render |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag(page, '{{ cms:page_files:files:link }}') |
| + | assert_equal "<a href='/system/files/#{file_a.id}/original/valid_image.jpg?#{timestamp}' target='_blank'>Valid Image</a> <a href='/system/files/#{file_b.id}/original/invalid_file.gif?#{timestamp}' target='_blank'>Invalid File</a>", |
| + | tag.render |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag(page, '{{ cms:page_files:files:image }}') |
| + | assert_equal "<img src='/system/files/#{file_a.id}/original/valid_image.jpg?#{timestamp}' alt='Valid Image' /> <img src='/system/files/#{file_b.id}/original/invalid_file.gif?#{timestamp}' alt='Invalid File' />", |
| + | tag.render |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag(page, '{{ cms:page_files:files:partial }}') |
| + | assert_equal "<%= render :partial => 'partials/page_files', :locals => {:identifier => #{files.collect(&:id)}} %>", |
| + | tag.render |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag(page, '{{ cms:page_files:files:partial:path/to/partial }}') |
| + | assert_equal "<%= render :partial => 'path/to/partial', :locals => {:identifier => #{files.collect(&:id)}} %>", |
| + | tag.render |
| + | |
| + | assert tag = ComfortableMexicanSofa::Tag::PageFiles.initialize_tag(page, '{{ cms:page_files:files:partial:path/to/partial:a:b }}') |
| + | assert_equal "<%= render :partial => 'path/to/partial', :locals => {:identifier => #{files.collect(&:id)}, :param_1 => 'a', :param_2 => 'b'} %>", |
| + | tag.render |
| end | |
| end | |
| \ No newline at end of file | |