when file gets attached to the page only file itself can refresh cached page content

Oleg committed Sep 29, 2011
commit 967d165ac49756e137c59761a880219aa41e5c91
Showing 4 changed files with 15 additions and 2 deletions
app/models/cms/block.rb +1 -0
@@ @@ -39,6 +39,7 @@ protected
self.files.collect{|f| f.mark_for_destruction } if single_file
self.files.build(:site => self.page.site, :file => file)
end
+
self.content = nil unless self.content.is_a?(String)
end
end
app/models/cms/file.rb +9 -1
@@ @@ -18,8 +18,10 @@ class Cms::File < ActiveRecord::Base
validates_attachment_presence :file
# -- Callbacks ------------------------------------------------------------
- before_save :assign_label
+ before_save :assign_label
before_create :assign_position
+ after_save :reload_page_cache
+ after_destroy :reload_page_cache
# -- Scopes ---------------------------------------------------------------
default_scope order(:position)
@@ @@ -35,4 +37,10 @@ protected
self.position = max ? max + 1 : 0
end
+ # FIX: Terrible, but no way of creating cached page content overwise
+ def reload_page_cache
+ return unless self.block
+ self.block.page.save!
+ end
+
end
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb +1 -1
@@ @@ -142,7 +142,7 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
:id => nil
)
content << @template.hidden_field_tag("page[blocks_attributes][#{index}][label]", tag.label, :id => nil)
- simple_field(tag.label, content, :class => tag.class.to_s.demodulize.underscore )
+ simple_field(tag.label.titleize, content, :class => tag.class.to_s.demodulize.underscore )
end
end
test/unit/models/block_test.rb +4 -0
@@ @@ -81,6 +81,9 @@ class CmsBlockTest < ActiveSupport::TestCase
assert_equal nil, block.content
assert_equal 1, block.files.count
assert_equal 'valid_image.jpg', block.files.first.file_file_name
+
+ page.reload
+ assert_equal block.files.first.file.url, page.content
end
assert_no_difference ['Cms::Block.count', 'Cms::File.count'] do
@@ @@ -94,6 +97,7 @@ class CmsBlockTest < ActiveSupport::TestCase
block = page.blocks.first
assert_equal 1, block.files.count
assert_equal 'invalid_file.gif', block.files.first.file_file_name
+ assert_equal block.files.first.file.url, page.content
end
end