ordering thing is almost in, some tests are failing

Oleg committed Sep 16, 2011
commit 63558172b768cc57fabad34798c75a92181f8a3b
Showing 13 changed files with 53 additions and 35 deletions
app/models/cms/file.rb +4 -5
@@ @@ -18,14 +18,13 @@ class Cms::File < ActiveRecord::Base
validates_uniqueness_of :file_file_name,
:scope => :site_id
-
- # -- Scopes ---------------------------------------------------------------
- default_scope order(:position)
# -- Callbacks ------------------------------------------------------------
before_save :assign_label
- before_validation :assign_position,
- :on => :create
+ before_save :assign_position, :on => :create
+
+ # -- Scopes ---------------------------------------------------------------
+ default_scope order(:position)
protected
app/models/cms/layout.rb +2 -3
@@ @@ -14,8 +14,7 @@ class Cms::Layout < ActiveRecord::Base
# -- Callbacks ------------------------------------------------------------
before_validation :assign_label
- before_validation :assign_position,
- :on => :create
+ before_save :assign_position, :on => :create
after_save :clear_cached_page_content
after_destroy :clear_cached_page_content
@@ @@ -78,7 +77,7 @@ protected
end
def assign_position
- max = Cms::Layout.maximum(:position)
+ max = self.site.layouts.maximum(:position)
self.position = max ? max + 1 : 0
end
app/models/cms/page.rb +2 -2
@@ @@ -25,8 +25,8 @@ class Cms::Page < ActiveRecord::Base
before_validation :assigns_label,
:assign_parent,
:assign_full_path
- before_validation :assign_position,
- :on => :create
+ before_save :assign_position,
+ :on => :create
before_save :set_cached_content
after_save :sync_child_pages
app/models/cms/snippet.rb +3 -4
@@ @@ -13,12 +13,10 @@ class Cms::Snippet < ActiveRecord::Base
# -- Callbacks ------------------------------------------------------------
before_validation :assign_label
- before_validation :assign_position,
- :on => :create
+ before_save :assign_position, :on => :create
after_save :clear_cached_page_content
after_destroy :clear_cached_page_content
-
# -- Validations ----------------------------------------------------------
validates :site_id,
:presence => true
@@ @@ -31,6 +29,7 @@ class Cms::Snippet < ActiveRecord::Base
# -- Scopes ---------------------------------------------------------------
default_scope order(:position)
+
protected
def assign_label
@@ @@ -45,7 +44,7 @@ protected
end
def assign_position
- max = Cms::Snippet.maximum(:position)
+ max = self.site.snippets.maximum(:position)
self.position = max ? max + 1 : 0
end
config/routes.rb +6 -16
@@ @@ -4,34 +4,24 @@ Rails.application.routes.draw do
get '/', :to => 'base#jump'
resources :sites do
resources :pages do
- member do
- match :form_blocks
- match :toggle_branch
- end
- collection do
- match :reorder
- end
+ get :form_blocks, :on => :member
+ get :toggle_branch, :on => :member
+ post :reorder, :on => :collection
resources :revisions, :only => [:index, :show, :revert] do
put :revert, :on => :member
end
end
resources :files do
- collection do
- match :reorder
- end
+ post :reorder, :on => :collection
end
resources :layouts do
- collection do
- match :reorder
- end
+ post :reorder, :on => :collection
resources :revisions, :only => [:index, :show, :revert] do
put :revert, :on => :member
end
end
resources :snippets do
- collection do
- match :reorder
- end
+ post :reorder, :on => :collection
resources :revisions, :only => [:index, :show, :revert] do
put :revert, :on => :member
end
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +4 -0
@@ @@ -63,10 +63,12 @@ class CreateCms < ActiveRecord::Migration
t.string :label
t.string :slug
t.text :content
+ t.integer :position, :null => false, :default => 0
t.boolean :is_shared, :null => false, :default => false
t.timestamps
end
add_index :cms_snippets, [:site_id, :slug], :unique => true
+ add_index :cms_snippets, [:site_id, :position]
# -- Files --------------------------------------------------------------
create_table :cms_files do |t|
@@ @@ -76,10 +78,12 @@ class CreateCms < ActiveRecord::Migration
t.string :file_content_type
t.integer :file_file_size
t.string :description, :limit => 2048
+ t.integer :position, :null => false, :default => 0
t.timestamps
end
add_index :cms_files, [:site_id, :label]
add_index :cms_files, [:site_id, :file_file_name]
+ add_index :cms_files, [:site_id, :position]
# -- 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 +9 -3
@@ @@ -1,10 +1,16 @@
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_snippets, :position, :integer, :null => false, :default => 0
+ add_column :cms_files, :position, :integer, :null => false, :default => 0
+
+ add_index :cms_snippets, [:site_id, :position]
+ add_index :cms_files, [:site_id, :position]
end
-
+
def self.down
+ remove_index :cms_snippets, [:site_id, :position]
+ remove_index :cms_files, [:site_id, :position]
+
remove_column :cms_snippets, :position
remove_column :cms_files, :position
end
test/fixtures/cms/files.yml +2 -1
@@ @@ -4,4 +4,5 @@ default:
file_file_name: sample.jpg
file_content_type: image/jpeg
file_file_size: 20099
- description: Description
\ No newline at end of file
+ description: Description
+ position: 0
\ No newline at end of file
test/fixtures/cms/layouts.yml +4 -1
@@ @@ -12,6 +12,7 @@ default:
layout_content_c
css: default_css
js: default_js
+ position: 0
nested:
site: default
@@ @@ -23,6 +24,7 @@ nested:
{{cms:page:content}}
css: nested_css
js: nested_js
+ position: 0
child:
site: default
@@ @@ -33,4 +35,5 @@ child:
{{cms:page:left_column}}
{{cms:page:right_column}}
css: child_css
- js: child_js
\ No newline at end of file
+ js: child_js
+ position: 0
\ No newline at end of file
test/fixtures/cms/snippets.yml +1 -0
@@ @@ -3,3 +3,4 @@ default:
label: Default Snippet
slug: default
content: default_snippet_content
+ position: 0
test/unit/models/file_test.rb +1 -0
@@ @@ -22,6 +22,7 @@ class CmsFileTest < ActiveSupport::TestCase
:file => fixture_file_upload('files/valid_image.jpg')
)
assert_equal 'Valid Image', file.label
+ assert_equal 1, file.position
end
end
test/unit/models/layout_test.rb +1 -0
@@ @@ -34,6 +34,7 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_equal '{{cms:page:content}}', layout.content
assert_equal 'css', layout.css
assert_equal 'js', layout.js
+ assert_equal 1, layout.position
end
end
test/unit/models/snippet_test.rb +14 -0
@@ @@ -23,6 +23,20 @@ class CmsSnippetTest < ActiveSupport::TestCase
assert_equal 'Test', snippet.label
end
+ def test_create
+ assert_difference 'Cms::Snippet.count' do
+ snippet = cms_sites(:default).snippets.create(
+ :label => 'Test Snippet',
+ :slug => 'test',
+ :content => 'Test Content'
+ )
+ assert_equal 'Test Snippet', snippet.label
+ assert_equal 'test', snippet.slug
+ assert_equal 'Test Content', snippet.content
+ assert_equal 1, snippet.position
+ end
+ end
+
def test_update_forces_page_content_reload
snippet = cms_snippets(:default)
page = cms_pages(:default)