Drag Order Fix For New Blank Pages
We forked Drag Order extension, a cool extension for Radiant CMS, and fixed some stuff. What we didn’t take into account in our last fix was this case:
1) Create new pages, or import a data set with a ton of new pages
2) Try to reorder a page.
Turns out, the way it’s written, there’s an assumption that there’s just one new page:
1 2 3 4 5 6 7 8 |
<code># Set initial position if the page has none yet
if @page.position == nil
@last_page = Page.find_all_by_parent_id( @page.parent.id,
:order => [ "position DESC" ], :limit = > 1 )
@last_page.each do |p|
@page.position = p.position.to_i + 1
end
end</code> |
So in our case we had multiple new pages-and the above would only work on one of them, the one being dragged. Later it would fail because there are other nil pages.
We ended up replacing the above block, to query for more records.
1 2 3 4 |
<code>
all_new_pages = Page.find_all_by_parent_id( @page.parent.id,
:conditions => ["position is null"] )
</code> |
Once we have that set, we query the rest of the records to find out the inherent order. To find out this order of pages as the user sees it- and expects it, you could argue- we just mimic the query that’s on the index view for Radiant’s admin pages page, :order=> "position ASC"
1 2 3 4 5 6 7 8 9 10 |
<code> if(all_new_pages.size > 0)
all_pages = Page. find_all_by_parent_id( @page.parent.id,
:order => ["position ASC"] )
i = 1
all_pages.each do |p|
p.position = i
p.save
i += 1
end
end</code> |
We do a basic enumeration to determine the position in the array, and assign it to the page.
1 2 3 4 |
<code>
p.position=i
p.save
</code> |
This works fine, but at this point in the code, the original arguments passed in - there are 4, two of which are the used to create local arrays of database records, are now out of date. So we reload those from the database:
1 2 3 4 |
<code>
@page = Page.find(params[:id])
@rel = Page.find(params[:rel])
</code> |