Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Navigation for CMS Content #28

Closed
patricklehmann opened this issue Nov 28, 2012 · 10 comments
Closed

Create Navigation for CMS Content #28

patricklehmann opened this issue Nov 28, 2012 · 10 comments
Assignees

Comments

@patricklehmann
Copy link

Hey there,
i'm currently working on a cms where i use closure tree for managing the web pages.
Now i want to do a navigation so i wanted to use Page.hash_tree.

@Navigation = Page.hash_tree(:limit_depth => 2) creates this error "undefined method []=' for nil:NilClass" in closure_tree (3.6.4) lib/closure_tree/acts_as_tree.rb:113:inblock in hash_tree'
can someone help me? would be great ;)

kind regards,
patrick

@mceachen
Copy link
Collaborator

mceachen commented Dec 1, 2012

First, try calling rebuild! and seeing if perhaps the _hierarchies table was corrupt in some way.

If that doesn't fix it, we're probably looking at a bug. There's a spec for the hash_tree class method, but the shape of your tree is different—if you could do this:

select id, parent_id from YOURTABLE

and post the result here, that'd be great (and won't expose any sensitive data).

@ghost ghost assigned mceachen Dec 1, 2012
@patricklehmann
Copy link
Author

Hey,

I just did rebuild! but had no success.
For sure i can: here is the result of: select id, parent_id from pages;

40
39 26
38 23
32 31
31 30
16
28 27
22 21
21 16
25 24
34 30
33 31
23 21
24 16
29 27
26 24
37
35
27 16
36
30

does that help?

@mceachen
Copy link
Collaborator

mceachen commented Dec 3, 2012

Yeah! I'll shove that into a spec and see if you've found a bug (it might be a day or two, I'm busy at work).

@patricklehmann
Copy link
Author

hey, how does it looks like?

@mceachen
Copy link
Collaborator

Sorry, didn't get to it yet, thanks for the ping.

@mceachen
Copy link
Collaborator

I can't reproduce (see https://github.com/mceachen/closure_tree/tree/issue_28.) What rails, ruby, and db are you using, and can you paste the model class here?

@patricklehmann
Copy link
Author

Hey,

I'm using ruby 1.9.3p327, Rails 3.2.8 and mysql 5.1.61 and here is my model.

class Page < ActiveRecord::Base
########################################################################################

Global

########################################################################################
has_paper_trail
include Rails.application.routes.url_helpers # neeeded for _path helpers to work in models
acts_as_tree :order => 'sort_order'

########################################################################################

Attributes

########################################################################################
attr_accessible :url, :valid_from, :valid_till, :parent_id, :complete_url,
:header_image, :use_for_navigation, :is_public, :sort_order,
:show_breadcrumb, :show_title,
:page_contents_attributes

########################################################################################

Relations

########################################################################################
has_many :page_aliases, :dependent => :destroy, :order => "created_automatically, created_at"
has_many :page_contents, :dependent => :destroy
has_many :keywords, :through => :page_contents
accepts_nested_attributes_for :page_contents
accepts_nested_attributes_for :keywords
has_many :products

has_attached_file :header_image,
:styles => {
:large => {
:geometry => "920x"
},
:small => {
:geometry => "181x"
}
},
:default_style => :small,
:url => "/images/header_images/:style/:id.:extension"

########################################################################################

This scope is used for showing page on website

########################################################################################
default_scope order(:sort_order)
scope :show_on_website, where{
(is_public == true) & (((valid_from.lte Date.today) | (valid_from == nil)) | ((valid_till.gt Date.today) & (valid_till == nil)))
}

########################################################################################

This scope is used for navigation entries

########################################################################################
scope :navigation, show_on_website.where{
(use_for_navigation == true) & (parent_id == nil)
}.order(:sort_order)

########################################################################################

Validation

########################################################################################
validates :url, :presence => true
validates :complete_url, :presence => true

########################################################################################

Callback

########################################################################################
before_validation do
self.url = self.url.gsub(" ", "_").downcase
self.complete_url = "/" + self.get_complete_url
end

########################################################################################

Functions

########################################################################################
def to_s
"#{content.title}::#{complete_url}"
end

Returns the complete URL for this page

def get_complete_url
tmp = ""
if !self.root?
tmp += self.parent.get_complete_url
end
tmp += self.url+ "/"
end

end

@mceachen
Copy link
Collaborator

Huh, ok. What's the CREATE TABLE for pages look like?

On Monday, December 17, 2012, bulldogge1989 wrote:

Hey,

I'm using ruby 1.9.3p327, Rails 3.2.8 and mysql 5.1.61 and here is my
model.

class Page < ActiveRecord::Base

########################################################################################

Global

########################################################################################
has_paper_trail
include Rails.application.routes.url_helpers # neeeded for _path helpers
to work in models
acts_as_tree :order => 'sort_order'

########################################################################################

Attributes

########################################################################################
attr_accessible :url, :valid_from, :valid_till, :parent_id, :complete_url,
:header_image, :use_for_navigation, :is_public, :sort_order,
:show_breadcrumb, :show_title,
:page_contents_attributes

########################################################################################

Relations

########################################################################################
has_many :page_aliases, :dependent => :destroy, :order =>
"created_automatically, created_at"
has_many :page_contents, :dependent => :destroy
has_many :keywords, :through => :page_contents
accepts_nested_attributes_for :page_contents
accepts_nested_attributes_for :keywords
has_many :products

has_attached_file :header_image,
:styles => {
:large => {
:geometry => "920x"
},
:small => {
:geometry => "181x"
}
},
:default_style => :small,
:url => "/images/header_images/:style/:id.:extension"

########################################################################################

This scope is used for showing page on website

########################################################################################
default_scope order(:sort_order)
scope :show_on_website, where{
(is_public == true) & (((valid_from.lte Date.today) | (valid_from == nil))
| ((valid_till.gt Date.today) & (valid_till == nil)))
}

########################################################################################

This scope is used for navigation entries

########################################################################################
scope :navigation, show_on_website.where{
(use_for_navigation == true) & (parent_id == nil)
}.order(:sort_order)

########################################################################################

Validation

########################################################################################
validates :url, :presence => true
validates :complete_url, :presence => true

########################################################################################

Callback

########################################################################################
before_validation do
self.url = self.url.gsub(" ", "_").downcase
self.complete_url = "/" + self.get_complete_url
end

########################################################################################

Functions

########################################################################################
def to_s
"#{content.title}::#{complete_url}"
end

Returns the complete URL for this page

def get_complete_url
tmp = ""
if !self.root?
tmp += self.parent.get_complete_url
end
tmp += self.url+ "/"
end

end


Reply to this email directly or view it on GitHubhttps://github.com//issues/28#issuecomment-11433450.

@patricklehmann
Copy link
Author

i think you're looking for this?

create_table "page_hierarchies", :id => false, :force => true do |t|
t.integer "ancestor_id", :null => false
t.integer "descendant_id", :null => false
t.integer "generations", :null => false
end

add_index "page_hierarchies", ["ancestor_id", "descendant_id"], :name => "index_page_hierarchies_on_ancestor_id_and_descendant_id", :unique => true
add_index "page_hierarchies", ["descendant_id"], :name => "index_page_hierarchies_on_descendant_id"

create_table "pages", :force => true do |t|
t.string "url"
t.boolean "is_public"
t.date "valid_from"
t.date "valid_till"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "parent_id"
t.integer "sort_order"
t.string "complete_url", :default => "/", :null => false
t.string "header_image_file_name"
t.string "header_image_content_type"
t.integer "header_image_file_size"
t.datetime "header_image_updated_at"
t.boolean "use_for_navigation", :default => false
t.boolean "show_breadcrumb", :default => true
t.boolean "show_title", :default => true
end

add_index "pages", ["complete_url"], :name => "index_pages_on_complete_url"
add_index "pages", ["parent_id"], :name => "index_pages_on_parent_id"

@mceachen
Copy link
Collaborator

I've duplicated your schema and included paper_trail in your model, but stuff seems to still work.

If you can create a breaking test, I'll happily reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants