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

When loading a preview the root association should return the right page #1399

Closed
miskander opened this issue Mar 5, 2012 · 15 comments
Closed

Comments

@miskander
Copy link
Contributor

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.

@parndt
Copy link
Member

parndt commented Mar 18, 2012

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 Refinery::Page:

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

@phiggins
Copy link
Contributor

@parndt: Should awesome_nested_set support this?

@parndt
Copy link
Member

parndt commented Mar 31, 2012

@phiggins probably not?

@parndt
Copy link
Member

parndt commented Mar 31, 2012

@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?

@phiggins
Copy link
Contributor

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.

@parndt
Copy link
Member

parndt commented Apr 9, 2012

@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'

@miskander
Copy link
Contributor Author

@parndt I just tried this and calling .root on a new page still returns nil.

@parndt
Copy link
Member

parndt commented Apr 9, 2012

Yes the page has to actually have a parent_id assigned otherwise root
doesn't make sense to be anything but nil

@miskander
Copy link
Contributor Author

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.

@parndt
Copy link
Member

parndt commented Apr 9, 2012

Oh so it should return self?

@parndt
Copy link
Member

parndt commented Apr 9, 2012

Totally agree, Doh.

@parndt
Copy link
Member

parndt commented Apr 9, 2012

@miskander try bundle update to get this commit which assigns to self not nil collectiveidea/awesome_nested_set@45d0435

@miskander
Copy link
Contributor Author

@parndt Works well, that does the trick for me. :)

Thanks!

@parndt
Copy link
Member

parndt commented Apr 11, 2012

@miskander thanks, I'll see about releasing a patch version of awesome_nested_set.

@parndt parndt closed this as completed in a8030d2 Apr 11, 2012
@parndt
Copy link
Member

parndt commented Apr 11, 2012

@miskander please remove the git dependency and bundle update to version 2.1.3 :-)

nicholassm pushed a commit to subsis/refinerycms that referenced this issue Apr 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants