merging upload tags into one

Oleg committed Jun 04, 2011
commit 9ddd1341b900f0da39a9724c6795e29e286b22ba
Showing 10 changed files with 93 additions and 211 deletions
README.md +0 -9
@@ @@ -83,15 +83,6 @@ Here's a number of tag variations:
{{ cms:partial:path/to/partial:a:b }} # same as <%= render :partial => 'path/to/partial',
# :locals => { :param_1 => 'a', :param_1 => 'b' } %>
- # Upload tags are wrappers that interact with the uploads.
-
- {{ cms:upload:upload_file_name }} # URL of upload_file_name
- {{ cms:upload:upload_file_name:text }} # The same.
- {{ cms:upload:upload_file_name:image }} # An image html tag (img) for this upload
- {{ cms:upload:upload_file_name:image:alt }} # An image html tag (img) for this upload with "alt" as text alternate
- {{ cms:upload:upload_file_name:link }} # A link html tag (a) to this upload
- {{ cms:upload:upload_file_name:link:text }} # A link html tag (a) to this upload using text as description
-
Multiple Sites
--------------
Sofa is able to manage multiple sites from the same application. For instance: 'site-a.example.com' and 'site-b.example.com' will have distinct set of layouts, pages, snippets, etc. To enable multi-site functionality make sure you have this setting in the initializer: `config.enable_multiple_sites = true`.
comfortable_mexican_sofa/tag.rb b/lib/comfortable_mexican_sofa/tag.rb +2 -5
@@ @@ -84,12 +84,9 @@ module ComfortableMexicanSofa::Tag
page.site.snippets.detect{|s| s.slug == self.label.to_s} || page.site.snippets.build(:slug => self.label.to_s)
end
+ # Initializing Cms::Upload object
def upload
- if ComfortableMexicanSofa.config.enable_mirror_sites
- Cms::Upload.all.detect{|s| s.file_file_name == self.label.to_s} || nil
- else
- page.site.uploads.detect{|s| s.file_file_name == self.label.to_s} || nil
- end
+ page.site.uploads.detect{|s| s.file_file_name == self.label.to_s}
end
# Checks if this tag is using Cms::Block
comfortable_mexican_sofa/tags/upload.rb b/lib/comfortable_mexican_sofa/tags/upload.rb +24 -0
@@ @@ -0,0 +1,24 @@
+ class ComfortableMexicanSofa::Tag::Upload
+ include ComfortableMexicanSofa::Tag
+
+ def self.regex_tag_signature(label = nil)
+ label ||= /[\w\-\.]+/
+ /\{\{\s*cms:upload:(#{label}):?(.*?)\s*\}\}/
+ end
+
+ def content
+ return unless upload
+
+ format = params[0]
+ text = params[1] || label
+
+ case format
+ when 'link'
+ "<a href='#{upload.file.url}' target='_blank'>#{text}</a>"
+ when 'image'
+ "<img src='#{upload.file.url}' alt='#{text}' />"
+ else
+ upload.file.url
+ end
+ end
+ end
\ No newline at end of file
comfortable_mexican_sofa/tags/upload_image.rb b/lib/comfortable_mexican_sofa/tags/upload_image.rb +0 -18
@@ @@ -1,18 +0,0 @@
- class ComfortableMexicanSofa::Tag::UploadImage
- include ComfortableMexicanSofa::Tag
-
- def self.regex_tag_signature(label = nil)
- label ||= /[\w\-\.]+/
- /\{\{\s*cms:upload:(#{label}):image:?(.*?)\s*\}\}/
- end
-
- def content
- return nil if upload.nil?
- alt = if params.empty?
- upload.file_file_name
- else
- params
- end
- "<img src='#{upload.file.url}' alt='#{alt}' />"
- end
- end
\ No newline at end of file
comfortable_mexican_sofa/tags/upload_link.rb b/lib/comfortable_mexican_sofa/tags/upload_link.rb +0 -19
@@ @@ -1,19 +0,0 @@
- class ComfortableMexicanSofa::Tag::UploadLink
- include ComfortableMexicanSofa::Tag
-
- def self.regex_tag_signature(label = nil)
- label ||= /[\w\-\.]+/
- /\{\{\s*cms:upload:(#{label}):link:?(.*?)\s*\}\}/
- end
-
- def content
- return nil if upload.nil?
- text = if params.empty?
- upload.file_file_name
- else
- params
- end
- "<a href='#{upload.file.url}'>#{text}</a>"
- end
-
- end
\ No newline at end of file
comfortable_mexican_sofa/tags/upload_text.rb b/lib/comfortable_mexican_sofa/tags/upload_text.rb +0 -13
@@ @@ -1,13 +0,0 @@
- class ComfortableMexicanSofa::Tag::UploadText
- include ComfortableMexicanSofa::Tag
-
- def self.regex_tag_signature(label = nil)
- label ||= /[\w\-\.]+/
- /\{\{\s*cms:upload:(#{label}):?(?:text)?\s*\}\}/
- end
-
- def content
- upload.nil? ? nil : upload.file.url
- end
-
- end
\ No newline at end of file
test/unit/tags/upload_image_test.rb +0 -51
@@ @@ -1,51 +0,0 @@
- require File.expand_path('../../test_helper', File.dirname(__FILE__))
-
- class UploadImageTest < ActiveSupport::TestCase
-
- def test_initialize_tag
- assert tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), '{{ cms:upload:label:image }}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), '{{cms:upload:label:image}}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), '{{cms:upload:dash-label:image}}'
- )
- assert_equal 'dash-label', tag.label
- end
-
- def test_initialize_tag_failure
- [
- '{{cms:upload}}',
- '{{cms:not_upload:image}}',
- '{not_a_tag}'
- ].each do |tag_signature|
- assert_nil ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), tag_signature
- )
- end
- end
-
- def test_content_and_render
- tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), '{{cms:upload:sample.jpg:image}}'
- )
- assert_equal "<img src='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}' alt='sample.jpg' />", tag.content
- assert_equal "<img src='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}' alt='sample.jpg' />", tag.render
-
- tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), '{{cms:upload:sample.jpg:image:a sample image}}'
- )
- assert_equal "<img src='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}' alt='a sample image' />", tag.content
- assert_equal "<img src='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}' alt='a sample image' />", tag.render
-
- tag = ComfortableMexicanSofa::Tag::UploadImage.initialize_tag(
- cms_pages(:default), "{{cms:upload:doesnot_exist:image}}"
- )
- assert_equal nil, tag.content
- assert_equal '', tag.render
- end
- end
\ No newline at end of file
test/unit/tags/upload_link_test.rb +0 -51
@@ @@ -1,51 +0,0 @@
- require File.expand_path('../../test_helper', File.dirname(__FILE__))
-
- class UploadLinkTest < ActiveSupport::TestCase
-
- def test_initialize_tag
- assert tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), '{{ cms:upload:label:link }}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), '{{cms:upload:label:link}}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), '{{cms:upload:dash-label:link}}'
- )
- assert_equal 'dash-label', tag.label
- end
-
- def test_initialize_tag_failure
- [
- '{{cms:upload}}',
- '{{cms:not_upload:link}}',
- '{not_a_tag}'
- ].each do |tag_signature|
- assert_nil ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), tag_signature
- )
- end
- end
-
- def test_content_and_render
- tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), '{{cms:upload:sample.jpg:link}}'
- )
- assert_equal "<a href='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}'>sample.jpg</a>", tag.content
- assert_equal "<a href='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}'>sample.jpg</a>", tag.render
-
- tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), '{{cms:upload:sample.jpg:link:a sample image}}'
- )
- assert_equal "<a href='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}'>a sample image</a>", tag.content
- assert_equal "<a href='#{Cms::Upload.find_by_file_file_name("sample.jpg").file.url}'>a sample image</a>", tag.render
-
- tag = ComfortableMexicanSofa::Tag::UploadLink.initialize_tag(
- cms_pages(:default), "{{cms:upload:doesnot_exist:link}}"
- )
- assert_equal nil, tag.content
- assert_equal '', tag.render
- end
- end
\ No newline at end of file
test/unit/tags/upload_test.rb +67 -0
@@ @@ -0,0 +1,67 @@
+ require File.expand_path('../../test_helper', File.dirname(__FILE__))
+
+ class UploadTest < ActiveSupport::TestCase
+
+ def test_initialize_tag
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg:image:alt text }}'
+ )
+ assert_equal cms_uploads(:default), tag.upload
+ assert_equal 'sample.jpg', tag.label
+ assert_equal ['image', 'alt text'], tag.params
+ end
+
+ def test_initialize_tag_failure
+ [
+ '{{cms:upload}}',
+ '{{cms:not_upload:label}}',
+ '{not_a_tag}'
+ ].each do |tag_signature|
+ assert_nil ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), tag_signature
+ )
+ end
+ end
+
+ def test_render_for_invalid
+ tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{cms:upload:invalid.jpg}}'
+ )
+ assert_nil tag.upload
+ assert_equal '', tag.render
+ end
+
+ def test_render
+ upload = cms_uploads(:default)
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg }}'
+ )
+ assert_equal upload.file.url, tag.render
+ end
+
+ def test_render_for_link
+ upload = cms_uploads(:default)
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg:link }}'
+ )
+ assert_equal "<a href='#{upload.file.url}' target='_blank'>sample.jpg</a>", tag.render
+
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg:link:link text }}'
+ )
+ assert_equal "<a href='#{upload.file.url}' target='_blank'>link text</a>", tag.render
+ end
+
+ def test_render_for_image
+ upload = cms_uploads(:default)
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg:image }}'
+ )
+ assert_equal "<img src='#{upload.file.url}' alt='sample.jpg' />", tag.render
+
+ assert tag = ComfortableMexicanSofa::Tag::Upload.initialize_tag(
+ cms_pages(:default), '{{ cms:upload:sample.jpg:image:alt text }}'
+ )
+ assert_equal "<img src='#{upload.file.url}' alt='alt text' />", tag.render
+ end
+ end
\ No newline at end of file
test/unit/tags/upload_text_test.rb +0 -45
@@ @@ -1,45 +0,0 @@
- require File.expand_path('../../test_helper', File.dirname(__FILE__))
-
- class UploadTextTest < ActiveSupport::TestCase
-
- def test_initialize_tag
- assert tag = ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), '{{ cms:upload:label }}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), '{{cms:upload:label}}'
- )
- assert_equal 'label', tag.label
- assert tag = ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), '{{cms:upload:dash-label}}'
- )
- assert_equal 'dash-label', tag.label
- end
-
- def test_initialize_tag_failure
- [
- '{{cms:upload}}',
- '{{cms:not_upload:text}}',
- '{not_a_tag}'
- ].each do |tag_signature|
- assert_nil ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), tag_signature
- )
- end
- end
-
- def test_content_and_render
- tag = ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), '{{cms:upload:sample.jpg}}'
- )
- assert_equal Cms::Upload.find_by_file_file_name("sample.jpg").file.url, tag.content
- assert_equal Cms::Upload.find_by_file_file_name("sample.jpg").file.url, tag.render
-
- tag = ComfortableMexicanSofa::Tag::UploadText.initialize_tag(
- cms_pages(:default), "{{cms:upload:doesnot_exist}}"
- )
- assert_equal nil, tag.content
- assert_equal '', tag.render
- end
- end
\ No newline at end of file