Skip to content

Commit

Permalink
Use ancestry instead of acts_as_tree
Browse files Browse the repository at this point in the history
See #212
Basic functionality seems ok, but specs have obviously blown up
Not sure how to make the migration pass smooth; between the first and second migration, one has to run Page.build_ancestry_from_parent_ids! which failed for me with the archive extension in place (to do with after_update calls from allowed_children_cache)
  • Loading branch information
jomz committed Nov 25, 2011
1 parent 7f7f190 commit ab147d1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/pages_controller.rb
Expand Up @@ -19,7 +19,7 @@ def message
end

def index
@homepage = Page.find_by_parent_id(nil)
@homepage = Page.root
response_for :plural
end

Expand All @@ -40,7 +40,7 @@ def assign_page_attributes
if params[:page_id].blank?
self.model.slug = '/'
end
self.model.parent_id = params[:page_id]
# self.model.parent_id = params[:page_id]
end

def model_class
Expand Down
10 changes: 5 additions & 5 deletions app/models/page.rb
@@ -1,5 +1,4 @@
require 'acts_as_tree'

# require 'acts_as_tree'
class Page < ActiveRecord::Base

class MissingRootPageError < StandardError
Expand All @@ -10,7 +9,7 @@ def initialize(message = 'Database missing root page'); super end
before_save :update_virtual, :update_status, :set_allowed_children_cache

# Associations
acts_as_tree :order => 'virtual DESC, title ASC'
# acts_as_tree :order => 'virtual DESC, title ASC'
has_many :parts, :class_name => 'PagePart', :order => 'id', :dependent => :destroy
accepts_nested_attributes_for :parts, :allow_destroy => true
has_many :fields, :class_name => 'PageField', :order => 'id', :dependent => :destroy
Expand All @@ -27,10 +26,11 @@ def initialize(message = 'Database missing root page'); super end
validates_length_of :breadcrumb, :maximum => 160

validates_format_of :slug, :with => %r{^([-_.A-Za-z0-9]*|/)$}
validates_uniqueness_of :slug, :scope => :parent_id
validates_uniqueness_of :slug, :scope => :ancestry

validate :valid_class_name

has_ancestry
include Radiant::Taggable
include StandardTags
include DeprecatedTags
Expand Down Expand Up @@ -218,7 +218,7 @@ def set_allowed_children_cache
class << self

def root
find_by_parent_id(nil)
find_by_ancestry(nil)
end

def find_by_path(path, live = true)
Expand Down
6 changes: 3 additions & 3 deletions app/models/standard_tags.rb
Expand Up @@ -1224,11 +1224,11 @@ def render_children_with_pagination(tag, opts={})
result = []
tag.locals.previous_headers = {}
displayed_children = paging ? findable.paginate(options.merge(paging)) : findable.all(options)
displayed_children.each_with_index do |item, i|
displayed_children.each do |item|
tag.locals.child = item
tag.locals.page = item
tag.locals.first_child = i == 0
tag.locals.last_child = i == displayed_children.length - 1
tag.locals.first_child = item == displayed_children.first
tag.locals.last_child = item == displayed_children.last
result << tag.expand
end
if paging && displayed_children.total_pages > 1
Expand Down
11 changes: 11 additions & 0 deletions db/20111124233701_add_ancestry_to_pages.rb
@@ -0,0 +1,11 @@
class AddAncestryToPages < ActiveRecord::Migration
def self.up
add_column :pages, :ancestry, :string
add_index :pages, :ancestry
end

def self.down
remove_index :pages, :ancestry
remove_column :pages, :ancestry
end
end
11 changes: 11 additions & 0 deletions db/20111125001223_remove_parent_id_from_pages.rb
@@ -0,0 +1,11 @@
class RemoveParentIdFromPages < ActiveRecord::Migration
def self.up
remove_index :pages, :parent_id
remove_column :pages, :parent_id, :integer
end

def self.down
add_column :pages, :parent_id
add_index :pages, :parent_id
end
end
1 change: 1 addition & 0 deletions radiant.gemspec
Expand Up @@ -26,6 +26,7 @@ a general purpose content managment system--not merely a blogging engine.}
s.add_dependency "rails", "~> 2.3.14"
s.add_dependency "rdoc", "~> 3.9.2"
s.add_dependency "acts_as_tree", "~> 0.1.1"
s.add_dependency "ancestry", "~> 1.2.4"
s.add_dependency "bundler", ">= 1.0.0"
s.add_dependency "compass", "~> 0.11.1"
s.add_dependency "delocalize", "~> 0.2.3"
Expand Down

0 comments on commit ab147d1

Please sign in to comment.