Skip to content

Commit

Permalink
Make better use of nested sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Sep 2, 2010
1 parent 71bb6ef commit 7b88635
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
11 changes: 9 additions & 2 deletions db/migrate/20100831122919_move_page_to_nested_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ def self.up
# Add nested set columns
add_column :pages, :lft, :integer
add_column :pages, :rgt, :integer


add_column :pages, :depth, :integer

add_index :pages, :lft
add_index :pages, :rgt
add_index :pages, :depth

# Rebuild the page table
Page.rebuild!
Expand All @@ -14,8 +17,12 @@ def self.up
def self.down
remove_index :pages, :lft
remove_index :pages, :rgt


remove_index :pages, :depth

remove_column :pages, :lft
remove_column :pages, :rgt

remove_column :pages, :depth
end
end
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@
t.boolean "skip_to_first_child", :default => false
t.integer "lft"
t.integer "rgt"
t.integer "depth"
end

add_index "pages", ["id"], :name => "index_pages_on_id"
add_index "pages", ["lft"], :name => "index_pages_on_lft"
add_index "pages", ["parent_id"], :name => "index_pages_on_parent_id"
add_index "pages", ["rgt"], :name => "index_pages_on_rgt"
add_index "pages", ["depth"], :name => "index_pages_on_depth"

create_table "refinery_settings", :force => true do |t|
t.string "name"
Expand Down
25 changes: 0 additions & 25 deletions vendor/refinerycms/pages/app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Admin::PagesController < Admin::BaseController
:include => [:parts, :slugs, :children, :parent],
:paging => false

before_filter :find_pages_for_parents_list, :only => [:new, :create, :edit, :update]
after_filter :expire_caching, :only => [:create, :update, :destroy, :update_positions]

rescue_from FriendlyId::ReservedError, :with => :show_errors_for_reserved_slug
Expand All @@ -33,30 +32,6 @@ def expire_action_caching
expire_fragment %r{.*/pages/.*}
end

# This finds all of the pages that could possibly be assigned as the current page's parent.
def find_pages_for_parents_list
@pages_for_parents_list = []
Page.find_all_by_parent_id(nil, :order => "position ASC").each do |page|
@pages_for_parents_list << page
@pages_for_parents_list += add_pages_branch_to_parents_list(page)
end
@pages_for_parents_list.flatten.compact!

# We need to remove all references to the current page or any of its decendants or we get a nightmare.
@pages_for_parents_list.reject! do |page|
page.id == @page.id or @page.descendants.include?(page)
end unless @page.nil? or @page.new_record?
end

def add_pages_branch_to_parents_list(page)
list = []
page.children.each do |child|
list << child
list += add_pages_branch_to_parents_list(child) if child.children.any?
end
list
end

def show_errors_for_reserved_slug(exception)
flash[:error] = "Sorry, but that title is a reserved system word."
if params[:action] == 'update'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<%= f.label :parent_id, t('.parent_page') %>
<%= refinery_help_tag t('.parent_page_help') %>
</span>
<%= f.collection_select :parent_id, @pages_for_parents_list, :id, :indented_title, {:include_blank => true}, :class => 'widest' %>
<%= f.select :parent_id, nested_set_options(Page, @page) {|i| "#{'-' * i.level} #{i.title}" }, :include_blank => true %>
</div>
<div class='field'>
<span class='label_with_help'>
Expand Down

0 comments on commit 7b88635

Please sign in to comment.