Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Parent id nested set options #1633

Closed
wants to merge 1 commit into from

4 participants

@thedarkone

Manually preload :translations association to avoid the extra queries.

@travisbot

This pull request passes (merged 868d93d into 21ab29b).

pages/app/helpers/refinery/admin/pages_helper.rb
@@ -1,6 +1,14 @@
module Refinery
module Admin
module PagesHelper
+ def parent_id_nested_set_options(page)
+ pages = []
+ nested_set_options(::Refinery::Page, page) {|i| pages << i}
+ # page.title needs the :translations association, doing something like
+ # nested_set_options(::Refinery::Page.includes(:translations), page) doesn't work
+ ActiveRecord::Associations::Preloader.new(pages, :translations).run
+ pages.map {|i, id| ["#{'-' * i.level} #{i.title}", id]}
@ugisozols Owner

i is the instance of page and id is nil so this produces select tree with empty values for options. Can you fix it please? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ugisozols
Owner

For the future - please open PR's against master branch instead of 2-0-stable. I'm asking this so that we can put new stuff in the dev branch first and only then backport needed changes/fixes to the stable branches. Thanks :)

@ugisozols
Owner

Bonus points for specs!

@thedarkone

Re-pushed, thanks for catching that!

Dev branches and specs are for sissies :).

@travisbot

This pull request passes (merged 3e2b3ad into 21ab29b).

@ugisozols
Owner

I liked first version better because it was shorter and cleaner. All you had to do was to remove id. i already was an instance of page so you had access to page id. WDYT?

@parndt
Owner

Merged. Specs are for people who want to ensure that we don't break your code later ;-)

@parndt
Owner

Oh, and I merged it to master and with e4c0aae and c8dabae

@parndt
Owner

Opened #1653 for 2-0-stable

@parndt parndt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 pages/app/helpers/refinery/admin/pages_helper.rb
@@ -1,6 +1,18 @@
module Refinery
module Admin
module PagesHelper
+ def parent_id_nested_set_options(current_page)
+ all_pages = []
+ options = nested_set_options(::Refinery::Page, current_page) do |page|
+ all_pages << page
+ page
+ end
+ # page.title needs the :translations association, doing something like
+ # nested_set_options(::Refinery::Page.includes(:translations), page) doesn't work
+ ActiveRecord::Associations::Preloader.new(all_pages, :translations).run
+ options.map! {|page, id| ["#{'-' * page.level} #{page.title}", id]}
+ options
+ end
end
end
end
View
3  pages/app/views/refinery/admin/pages/_form_advanced_options.html.erb
@@ -18,8 +18,7 @@
<%= f.label :parent_id, t('.parent_page') %>
<%= refinery_help_tag t('.parent_page_help') %>
</span>
- <%= f.select :parent_id, nested_set_options(::Refinery::Page, @page) {|i| "#{'-' * i.level} #{i.title}" },
- :include_blank => true %>
+ <%= f.select :parent_id, parent_id_nested_set_options(@page), :include_blank => true %>
</div>
<% if Refinery::Pages.use_layout_templates %>
<div class='field'>
Something went wrong with that request. Please try again.