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:

# 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

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.

    all_new_pages = Page.find_all_by_parent_id( @page.parent.id,
                 :conditions => ["position is null"] )

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"

 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
        i += 1

We do a basic enumeration to determine the position in the array, and assign it to the page.


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:

   @page = Page.find(params[:id])
   @rel = Page.find(params[:rel])

Post a Comment

Your email is never shared. Required fields are marked *