locking down difference beetween page_file and page_files
Oleg
committed Sep 28, 2011
commit 4021113c3e2c293d968464149aabf67d85e70446
Showing 5
changed files with
88 additions
and 14 deletions
app/models/cms/block.rb
+7
-1
| @@ | @@ -29,7 +29,13 @@ protected |
| def prepare_files | |
| temp_files = [self.content].flatten.select do |f| | |
| %w(ActionDispatch::Http::UploadedFile Rack::Test::UploadedFile).member?(f.class.name) | |
| - | end.each do |file| |
| + | end |
| + | # only accepting one file if it's PageFile. PageFiles will take all |
| + | single_file = self.tag.is_a?(ComfortableMexicanSofa::Tag::PageFile) |
| + | temp_files = [temp_files.first] if single_file |
| + | |
| + | temp_files.each do |file| |
| + | self.files.collect{|f| f.mark_for_destruction } if single_file |
| self.files.new(:site => self.page.site, :file => file) | |
| end | |
| self.content = nil unless self.content.is_a?(String) | |
app/models/cms/file.rb
+0
-3
| @@ | @@ -17,9 +17,6 @@ class Cms::File < ActiveRecord::Base |
| validates :site_id, :presence => true | |
| validates_attachment_presence :file | |
| - | validates_uniqueness_of :file_file_name, |
| - | :scope => :site_id |
| - | |
| # -- Callbacks ------------------------------------------------------------ | |
| before_save :assign_label | |
| before_create :assign_position | |
comfortable_mexican_sofa/tags/page_file.rb b/lib/comfortable_mexican_sofa/tags/page_file.rb
+6
-1
| @@ | @@ -2,7 +2,7 @@ class ComfortableMexicanSofa::Tag::PageFile |
| include ComfortableMexicanSofa::Tag | |
| # Signature of a tag: | |
| - | # {{ cms:page_file:some_label:file_partial:params }} |
| + | # {{ cms:page_file:some_label:type:params }} |
| # Simple tag can be: | |
| # {{ cms:page_file:some_label }} | |
| def self.regex_tag_signature(label = nil) | |
| @@ | @@ -10,6 +10,11 @@ class ComfortableMexicanSofa::Tag::PageFile |
| /\{\{\s*cms:page_file:(#{label}):?(.*?)\s*\}\}/ | |
| end | |
| + | # Type of the tag controls how file is rendered |
| + | def type |
| + | %w(partial url image link).member?(params[0]) ? params[0] : 'url' |
| + | end |
| + | |
| def content | |
| # ... | |
| end | |
comfortable_mexican_sofa/tags/page_files.rb b/lib/comfortable_mexican_sofa/tags/page_files.rb
+8
-0
| @@ | @@ -0,0 +1,8 @@ |
| + | class ComfortableMexicanSofa::Tag::PageFiles < ComfortableMexicanSofa::Tag::PageFile |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /[\w\-]+/ |
| + | /\{\{\s*cms:page_files:(#{label}):?(.*?)\s*\}\}/ |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
test/unit/models/block_test.rb
+67
-9
| @@ | @@ -14,7 +14,7 @@ class CmsBlockTest < ActiveSupport::TestCase |
| assert_equal 'page_text_default_page_text', block.tag.identifier | |
| end | |
| - | def test_new_via_page_nested_attributes |
| + | def test_creation_via_page_nested_attributes |
| assert_difference ['Cms::Page.count', 'Cms::Block.count'] do | |
| page = Cms::Page.create!( | |
| :site => cms_sites(:default), | |
| @@ | @@ -36,7 +36,7 @@ class CmsBlockTest < ActiveSupport::TestCase |
| end | |
| end | |
| - | def test_new_via_page_nested_attributes_as_hash |
| + | def test_creation_via_page_nested_attributes_as_hash |
| assert_difference ['Cms::Page.count', 'Cms::Block.count'] do | |
| page = Cms::Page.create!( | |
| :site => cms_sites(:default), | |
| @@ | @@ -58,27 +58,85 @@ class CmsBlockTest < ActiveSupport::TestCase |
| end | |
| end | |
| - | def test_new_via_nested_attributes_with_files |
| + | def test_creation_and_update_via_nested_attributes_with_file |
| + | layout = cms_layouts(:default) |
| + | layout.update_attribute(:content, '{{cms:page_file:file}}') |
| + | |
| + | page = nil |
| + | assert_difference ['Cms::Page.count', 'Cms::Block.count', 'Cms::File.count'] do |
| + | page = Cms::Page.create!( |
| + | :site => cms_sites(:default), |
| + | :layout => layout, |
| + | :label => 'test page', |
| + | :slug => 'test_page', |
| + | :parent_id => cms_pages(:default).id, |
| + | :blocks_attributes => [ |
| + | { :label => 'file', |
| + | :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 nil, block.content |
| + | assert_equal 1, block.files.count, 'huh' |
| + | assert_equal 'valid_image.jpg', block.files.first.file_file_name |
| + | end |
| + | |
| + | assert_no_difference ['Cms::Block.count', 'Cms::File.count'] do |
| + | page.update_attributes!( |
| + | :blocks_attributes => [ |
| + | { :label => 'file', |
| + | :content => fixture_file_upload('files/invalid_file.gif') } |
| + | ] |
| + | ) |
| + | page.reload |
| + | block = page.blocks.first |
| + | assert_equal 1, block.files.count |
| + | assert_equal 'invalid_file.gif', block.files.first.file_file_name |
| + | end |
| + | end |
| + | |
| + | def test_creation_and_update_via_nested_attributes_with_files |
| + | layout = cms_layouts(:default) |
| + | layout.update_attribute(:content, '{{cms:page_file:files}}') |
| + | |
| + | page = nil |
| assert_difference ['Cms::Page.count', 'Cms::Block.count'] do | |
| assert_difference 'Cms::File.count', 2 do | |
| page = Cms::Page.create!( | |
| :site => cms_sites(:default), | |
| - | :layout => cms_layouts(:default), |
| + | :layout => layout, |
| :label => 'test page', | |
| :slug => 'test_page', | |
| :parent_id => cms_pages(:default).id, | |
| :blocks_attributes => [ | |
| - | { |
| - | :label => 'default_page_text', |
| - | :content => [fixture_file_upload('files/valid_image.jpg'), fixture_file_upload('files/invalid_file.gif')] |
| - | } |
| + | { :label => 'file', |
| + | :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 'default_page_text', block.label |
| + | assert_equal 'file', 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) |
| + | end |
| + | end |
| + | |
| + | assert_no_difference 'Cms::Block.count' do |
| + | assert_difference 'Cms::File.count', 2 do |
| + | page.update_attributes!( |
| + | :blocks_attributes => [ |
| + | { :label => 'file', |
| + | :content => [fixture_file_upload('files/valid_image.jpg'), fixture_file_upload('files/invalid_file.gif')] } |
| + | ] |
| + | ) |
| + | page.reload |
| + | block = page.blocks.first |
| + | assert_equal 4, block.files.count |
| + | assert_equal ['valid_image.jpg', 'invalid_file.gif', 'valid_image.jpg', 'invalid_file.gif'], |
| + | block.files.collect(&:file_file_name) |
| end | |
| end | |
| end | |