adding CmsLayout.oprions_for_select

Oleg committed Sep 01, 2010
commit 0d74d9229a8248c3c5ec0f63fc769ac8a44277c4
Showing 7 changed files with 45 additions and 8 deletions
app/models/cms_layout.rb +14 -4
@@ @@ -1,18 +1,28 @@
class CmsLayout < ActiveRecord::Base
+ acts_as_tree
+
# -- Relationships --------------------------------------------------------
has_many :cms_pages, :dependent => :nullify
# -- Validations ----------------------------------------------------------
validates :label,
- :presence => true,
- :uniqueness => true
+ :presence => true
validates :content,
:presence => true
# -- Class Methods --------------------------------------------------------
- def self.options_for_select
- CmsLayout.all(:select => 'id, label').collect { |l| [ l.label, l.id ] }
+ # Tree-like structure for layouts
+ def self.options_for_select(cms_layout = nil, current_layout = nil, depth = 0, spacer = '. . ')
+ out = []
+ [current_layout || CmsLayout.roots].flatten.each do |layout|
+ next if cms_layout == layout
+ out << [ "#{spacer*depth}#{layout.label}", layout.id ]
+ layout.children.each do |child|
+ out += options_for_select(cms_layout, child, depth + 1, spacer)
+ end
+ end
+ return out.compact
end
end
app/models/cms_page.rb +0 -1
@@ @@ -28,7 +28,6 @@ class CmsPage < ActiveRecord::Base
# Tree-like structure for pages
def self.options_for_select(cms_page = nil, current_page = nil, depth = 0, spacer = '. . ')
return [] if (current_page ||= CmsPage.root) == cms_page
-
out = [[ "#{spacer*depth}#{current_page.label}", current_page.id ]]
current_page.children.each do |child|
out += options_for_select(cms_page, child, depth + 1, spacer)
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +4 -0
@@ @@ -4,11 +4,14 @@ class CreateCms < ActiveRecord::Migration
# -- Layouts ------------------------------------------------------------
create_table :cms_layouts do |t|
+ t.integer :parent_id
t.string :label
t.text :content
+ t.integer :position, :null => false, :default => 0
t.timestamps
end
add_index :cms_layouts, :label
+ add_index :cms_layouts, [:parent_id, :position]
# -- Pages --------------------------------------------------------------
create_table :cms_pages do |t|
@@ @@ -56,6 +59,7 @@ class CreateCms < ActiveRecord::Migration
t.timestamps
end
add_index :cms_assets, :cms_page_id
+
end
def self.down
test/fixtures/cms_layouts.yml +16 -2
@@ @@ -1,6 +1,20 @@
- default: &layout
+ default:
label: Default Layout
+ parent:
content: |-
<cms:page:content/>
<cms:page:title:string/>
- <cms:page:number:integer/>
\ No newline at end of file
+ <cms:page:number:integer/>
+
+ nested:
+ label: Nested Layout
+ parent:
+ content: |-
+ <cms:page:content/>
+
+ child:
+ label: Child Layout
+ parent: nested
+ content: |-
+ <cms:page:left_column>
+ <cms:page:right_column>
\ No newline at end of file
test/functional/cms_admin/pages_controller_test.rb +4 -0
@@ @@ -17,6 +17,10 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
assert_select 'form[action=/cms-admin/pages]'
end
+ def test_get_new_as_child_page
+ flunk
+ end
+
def test_get_edit
flunk
end
test/unit/cms_layout_test.rb +6 -0
@@ @@ -14,4 +14,10 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_has_errors_on layout, [:label, :content]
end
+ def test_options_for_select
+ assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'], CmsLayout.options_for_select.collect{|t| t.first}
+ assert_equal ['Default Layout', 'Nested Layout'], CmsLayout.options_for_select(cms_layouts(:child)).collect{|t| t.first}
+ assert_equal ['Default Layout'], CmsLayout.options_for_select(cms_layouts(:nested)).collect{|t| t.first}
+ end
+
end
test/unit/cms_page_test.rb +1 -1
@@ @@ -139,7 +139,7 @@ class CmsPageTest < ActiveSupport::TestCase
end
protected
-
+
def new_params(options = {})
{
:label => 'Test Page',