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
When loading a preview the root association should return the right page #1399
Comments
1.9.3-p125 :001 > Refinery::Page.new.root
Refinery::Page Load (1.5ms) SELECT "refinery_pages".* FROM "refinery_pages" WHERE "refinery_pages"."parent_id" IS NULL AND ("refinery_pages"."lft" <= NULL AND "refinery_pages"."rgt" >= NULL) ORDER BY "lft" LIMIT 1
=> nil So what you're after is something like this on def root_with_new_record
root_without_new_record || if new_record?
if self.parent_id
Refinery::Page.find(self.parent_id)
else
self
end
end
end
alias_method_chain :root, :new_record Pull requests welcome |
@parndt: Should awesome_nested_set support this? |
@phiggins probably not? |
@phiggins it'd make Refinery code leaner but I'm not sure about it. I mean, on one hand it could. On the other hand I'm not sure about adding this. What do you think? |
I think from the SQL snippet you included above that awesome_nested_set is doing the wrong thing when .root is called on a new record. At the very least it should not do the SQL query since that will never find anything. The monkeypatch you have above seems close to what I'd consider a good alternative, which is essentially to do what .root would do if the record were saved: if it has a parent, the parent's .root, if no parent, self. |
@phiggins @miskander See collectiveidea/awesome_nested_set@689af99 Try it out with Refinery: gem 'awesome_nested_set', :git => 'git://github.com/collectiveidea/awesome_nested_set.git' |
@parndt I just tried this and calling .root on a new page still returns nil. |
Yes the page has to actually have a parent_id assigned otherwise root |
I imagined that calling root will always return something, since in reality a page that has not children is a root. Basically what @phiggins was saying in the comment above. |
Oh so it should return self? |
Totally agree, Doh. |
@miskander try |
@parndt Works well, that does the trick for me. :) Thanks! |
@miskander thanks, I'll see about releasing a patch version of |
@miskander please remove the git dependency and bundle update to version 2.1.3 :-) |
I am having to add a lot of checks in my layouts because page.root is returning nil. I understand that the page is not saved yet, but .root could probably still return either self (no root) OR the associated parent page that was associated before clicking preview. This way the layout that is generated would be as close as possible to what would happen after saving.
The text was updated successfully, but these errors were encountered: