starting on the block file handling
Oleg
committed Sep 27, 2011
commit a38ac5ceed92f9b61dc5d6b468997d9134cf5738
Showing 5
changed files with
46 additions
and 10 deletions
app/models/cms/block.rb
+14
-6
| @@ | @@ -6,18 +6,26 @@ class Cms::Block < ActiveRecord::Base |
| # -- Relationships -------------------------------------------------------- | |
| belongs_to :page | |
| + | has_many :files, |
| + | :autosave => true, |
| + | :dependent => :destroy |
| - | before_save :save_file |
| + | # -- Callbacks ------------------------------------------------------------ |
| + | before_save :prepare_files |
| # -- Validations ---------------------------------------------------------- | |
| validates :label, | |
| :presence => true, | |
| :uniqueness => { :scope => :page_id } | |
| - | def save_file |
| - | p '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$' |
| - | p self.content |
| - | p self.content.class.name |
| - | end |
| + | 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| |
| + | self.files.new(:site => self.page.site, :file => file) |
| + | end |
| + | self.content = nil unless self.content.is_a?(String) |
| + | end |
| end | |
app/models/cms/file.rb
+1
-4
| @@ | @@ -6,15 +6,12 @@ class Cms::File < ActiveRecord::Base |
| cms_is_categorized | |
| - | attr_accessor :layout_id, |
| - | :page_id, |
| - | :snippet_id |
| - | |
| # -- AR Extensions -------------------------------------------------------- | |
| has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options | |
| # -- Relationships -------------------------------------------------------- | |
| belongs_to :site | |
| + | belongs_to :block |
| # -- Validations ---------------------------------------------------------- | |
| validates :site_id, :presence => true | |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+2
-0
| @@ | @@ -73,6 +73,7 @@ class CreateCms < ActiveRecord::Migration |
| # -- Files -------------------------------------------------------------- | |
| create_table :cms_files do |t| | |
| t.integer :site_id | |
| + | t.integer :block_id |
| t.string :label | |
| t.string :file_file_name | |
| t.string :file_content_type | |
| @@ | @@ -84,6 +85,7 @@ class CreateCms < ActiveRecord::Migration |
| add_index :cms_files, [:site_id, :label] | |
| add_index :cms_files, [:site_id, :file_file_name] | |
| add_index :cms_files, [:site_id, :position] | |
| + | add_index :cms_files, [:site_id, :block_id] |
| # -- Revisions ----------------------------------------------------------- | |
| create_table :cms_revisions, :force => true do |t| | |
migrate/upgrades/06_upgrade_to_1_5_0.rb b/db/migrate/upgrades/06_upgrade_to_1_5_0.rb
+4
-0
| @@ | @@ -2,16 +2,20 @@ class UpgradeTo150 < ActiveRecord::Migration |
| def self.up | |
| add_column :cms_snippets, :position, :integer, :null => false, :default => 0 | |
| add_column :cms_files, :position, :integer, :null => false, :default => 0 | |
| + | add_column :cms_files, :block_id, :integer |
| add_index :cms_snippets, [:site_id, :position] | |
| add_index :cms_files, [:site_id, :position] | |
| + | add_index :cms_files, [:site_id, :block_id] |
| end | |
| def self.down | |
| remove_index :cms_snippets, [:site_id, :position] | |
| remove_index :cms_files, [:site_id, :position] | |
| + | remove_index :cms_files, [:site_id, :block_id] |
| remove_column :cms_snippets, :position | |
| remove_column :cms_files, :position | |
| + | remove_column :cms_files, :block_id |
| end | |
| end | |
test/unit/models/block_test.rb
+25
-0
| @@ | @@ -30,4 +30,29 @@ class CmsBlockTest < ActiveSupport::TestCase |
| end | |
| end | |
| + | def test_new_via_nested_attributes_with_files |
| + | 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), |
| + | :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')] |
| + | } |
| + | ] |
| + | ) |
| + | assert_equal 1, page.blocks.count |
| + | block = page.blocks.first |
| + | assert_equal 'default_page_text', block.label |
| + | assert_equal nil, block.content |
| + | assert_equal 2, block.files.count |
| + | end |
| + | end |
| + | end |
| + | |
| end | |