Ticket #45: sortable_pages.mental.diff

File sortable_pages.mental.diff, 16.0 kB (added by jlong, 2 years ago)

Allows you to sort children in the admin. Only works with mental.

  • test/functional/admin/page_controller_test.rb

    old new  
    219219    assert_equal pages(:homepage).children, assigns(:children) 
    220220  end 
    221221   
     222  def test_reorder__post 
     223    pages = [pages(:books), pages(:documentation), pages(:textile)] 
     224    order = pages.map(&:id).join(',') 
     225    post :reorder, :id => 1, :sort_order => order 
     226    pages.each(&:reload) 
     227    assert_equal 0, pages[0].position 
     228    assert_equal 1, pages[1].position 
     229    assert_equal 2, pages[2].position 
     230    assert_redirected_to page_index_url 
     231  end 
     232   
    222233  def test_tag_reference 
    223234    xml_http_request :get, :tag_reference, :class_name => "Page" 
    224235    assert_response :success 
  • app/helpers/admin/page_helper.rb

    old new  
    2727    end 
    2828  end 
    2929   
     30  def page_icon(page) 
     31    icon = page.virtual? ? "virtual-page" : "page" 
     32    image(icon, :class => "icon", :alt => 'page-icon', :title => '', :align => 'center') 
     33  end 
     34   
    3035  def homepage 
    3136    @homepage ||= Page.find_by_parent_id(nil) 
    3237  end 
  • app/models/page.rb

    old new  
    88  before_save :update_published_at, :update_virtual 
    99   
    1010  # Associations 
    11   acts_as_tree :order => 'virtual DESC, title ASC' 
     11  acts_as_tree :order => 'position ASC, virtual DESC, title ASC' 
    1212  has_many :parts, :class_name => 'PagePart', :order => 'id', :dependent => :destroy 
    1313  belongs_to :_layout, :class_name => 'Layout', :foreign_key => 'layout_id' 
    1414  belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by' 
  • app/controllers/admin/page_controller.rb

    old new  
    6060    render(:layout => false) 
    6161  end 
    6262 
     63  def reorder 
     64    if request.post? 
     65      sort_order = params[:sort_order].to_s.split(',').map { |i| Integer(i) rescue nil }.compact 
     66      sort_order.each_with_index do |id, index| 
     67        Page.update(id, :position => index) 
     68      end 
     69      redirect_to page_index_url 
     70    else 
     71      @page = Page.find(params[:id]) 
     72      @children = @page.children 
     73    end 
     74  end 
     75 
    6376  def tag_reference 
    6477    @class_name = params[:class_name] 
    6578    @display_name = @class_name.constantize.display_name 
  • app/views/admin/page/reorder.rhtml

    old new  
     1<% content_for :page_css do %> 
     2.page { 
     3  font-size: 120%; 
     4  font-weight: bold; 
     5  position: relative; 
     6  padding: 10px; 
     7  padding-left: 30px; 
     8  border-top: 1px solid #eaeaea; 
     9} 
     10.page .icon { 
     11  position: absolute; 
     12  margin-top: -7px; 
     13  left: 0px; 
     14} 
     15ul.pages, 
     16ul.pages li { 
     17  list-style: none; 
     18  margin: 0; 
     19  padding: 0 
     20} 
     21ul.pages { 
     22  border-bottom: 1px solid #eaeaea; 
     23} 
     24ul.pages li { 
     25  font-size: 105%; 
     26  font-weight: normal; 
     27  padding: 10px; 
     28  padding-left: 52px; 
     29} 
     30ul.page .icon, 
     31ul.pages .icon { 
     32  position: absolute; 
     33  margin-top: -7px; 
     34  left: 22px; 
     35} 
     36<% end %> 
     37 
     38<h1 id="reorder_pages">Reorder Pages</h1> 
     39 
     40<p>Drag and drop pages to reorder. Click <strong>Finished</strong> when you are done.</p>  
     41 
     42<div id="page" class="page"> 
     43  <%= page_icon(@page) %> <%= @page.title %> 
     44</div> 
     45 
     46<ul id="children" class="pages"> 
     47<% for child in @children -%> 
     48  <li id="item_<%= child.id %>" class="page"><%= page_icon(child) %> <%= child.title %></li> 
     49<% end -%> 
     50</ul> 
     51 
     52<script type="text/javascript"> 
     53//<![CDATA[ 
     54Sortable.create("children", { onUpdate:function(){ $('sort_order').value = Sortable.sequence('children').join(',') } }); 
     55//]]> 
     56</script> 
     57 
     58<% form_tag do %> 
     59  <p class="buttons"><%= submit_tag "Finished", :class => 'button' %> or <%= link_to 'Cancel', page_index_url %></p> 
     60  <div><%= hidden_field_tag "sort_order" %></div> 
     61<% end %> 
  • app/views/admin/page/_node.rhtml

    old new  
    55padding_left = (level * 22) + 4 
    66 
    77children_class = children ? (expanded ? ' children-visible' : ' children-hidden') : ' no-children' 
    8 virtual_class = page.virtual? ? " virtual": "" 
     8virtual_class = page.virtual? ? " virtual" : "" 
    99 
    1010expander = children ? image((expanded ? "collapse" : "expand"), :class => "expander", :alt => 'toggle children', :title => '', :align => 'center') : "" 
    1111 
    12 icon_name = page.virtual? ? "virtual-page" : "page" 
    13 icon = image(icon_name, :class => "icon", :alt => 'page-icon', :title => '', :align => 'center') 
     12icon = page_icon(page) 
    1413 
    1514title = %{<span class="title">#{ page.title }</span>} 
    1615 
     
    3433      </td> 
    3534<% unless simple -%> 
    3635      <td class="status <%= page.status.name.downcase %>-status"><%= page.status.name %></td> 
    37       <td class="add-child"><%= link_to image('add-child', :alt => 'add child'), page_new_url(:parent_id => page) %></td> 
     36      <td class="add-child"><%= link_to image('add-child', :alt => 'add child page'), page_new_url(:parent_id => page) %></td> 
    3837      <td class="remove"><%= link_to image('remove', :alt => 'remove page'), page_remove_url(:id => page) %></td> 
     38      <td class="reorder"><%= page.children.empty? ? '' : link_to(image('reorder', :alt => 'reorder children'), page_reorder_children_url(:id => page)) %></td> 
    3939<% end -%> 
    4040    </tr> 
    4141<% level = level + 1 -%> 
  • app/views/admin/page/index.rhtml

    old new  
    66    <tr> 
    77      <th class="page">Page</th> 
    88      <th class="status">Status</th> 
    9       <th class="modify" colspan="2">Modify</th> 
     9      <th class="modify" colspan="3">Modify</th> 
    1010    </tr> 
    1111  </thead> 
    1212  <tbody> 
  • db/schema.rb

    old new  
    22# migrations feature of ActiveRecord to incrementally modify your database, and 
    33# then regenerate this schema definition. 
    44 
    5 ActiveRecord::Schema.define(:version => 13) do 
     5ActiveRecord::Schema.define(:version => 14) do 
    66 
    77  create_table "config", :force => true do |t| 
    88    t.column "key",   :string, :limit => 40, :default => "", :null => false 
     
    4848    t.column "created_by",   :integer 
    4949    t.column "updated_by",   :integer 
    5050    t.column "virtual",      :boolean,                 :default => false, :null => false 
     51    t.column "position",     :integer 
    5152  end 
    5253 
    5354  create_table "snippets", :force => true do |t| 
  • db/migrate/014_add_position_column_to_page.rb

    old new  
     1class AddPositionColumnToPage < ActiveRecord::Migration 
     2  def self.up 
     3    add_column "pages", "position", :integer 
     4  end 
     5 
     6  def self.down 
     7    remove_column "pages", "position" 
     8  end 
     9end 
  • config/routes.rb

    old new  
    22 
    33  # Admin Routes 
    44  map.with_options(:controller => 'admin/welcome') do |welcome| 
    5     welcome.admin          'admin',                              :action => 'index' 
    6     welcome.welcome        'admin/welcome',                      :action => 'index' 
    7     welcome.login          'admin/login',                        :action => 'login' 
    8     welcome.logout         'admin/logout',                       :action => 'logout' 
     5    welcome.admin               'admin',                              :action => 'index' 
     6    welcome.welcome             'admin/welcome',                      :action => 'index' 
     7    welcome.login               'admin/login',                        :action => 'login' 
     8    welcome.logout              'admin/logout',                       :action => 'logout' 
    99  end 
    1010   
    1111  # Export Routes 
    1212  map.with_options(:controller => 'admin/export') do |export| 
    13     export.export          'admin/export',                             :action => 'yaml' 
    14     export.export_yaml     'admin/export/yaml',                        :action => 'yaml' 
     13    export.export               'admin/export',                       :action => 'yaml' 
     14    export.export_yaml          'admin/export/yaml',                  :action => 'yaml' 
    1515  end 
    1616 
    1717  # Page Routes 
    1818  map.with_options(:controller => 'admin/page') do |page| 
    19     page.page_index        'admin/pages',                        :action => 'index' 
    20     page.page_edit         'admin/pages/edit/:id',               :action => 'edit' 
    21     page.page_new          'admin/pages/:parent_id/child/new',   :action => 'new' 
    22     page.homepage_new      'admin/pages/new/homepage',           :action => 'new',        :slug => '/', :breadcrumb => 'Home' 
    23     page.page_remove       'admin/pages/remove/:id',             :action => 'remove' 
    24     page.page_add_part     'admin/ui/pages/part/add',            :action => 'add_part' 
    25     page.page_children     'admin/ui/pages/children/:id/:level', :action => 'children',   :level => '1' 
    26     page.tag_reference     'admin/ui/pages/tag_reference',       :action => 'tag_reference' 
    27     page.clear_cache       'admin/pages/cache/clear',            :action => 'clear_cache'     
     19    page.page_index             'admin/pages',                        :action => 'index' 
     20    page.page_edit              'admin/pages/edit/:id',               :action => 'edit' 
     21    page.page_new               'admin/pages/:parent_id/child/new',   :action => 'new' 
     22    page.homepage_new           'admin/pages/new/homepage',           :action => 'new',        :slug => '/', :breadcrumb => 'Home' 
     23    page.page_remove            'admin/pages/remove/:id',             :action => 'remove' 
     24    page.page_add_part          'admin/ui/pages/part/add',            :action => 'add_part' 
     25    page.page_children          'admin/ui/pages/children/:id/:level', :action => 'children',   :level => '1' 
     26    page.page_reorder_children  'admin/pages/reorder/:id',            :action => 'reorder' 
     27    page.tag_reference          'admin/ui/pages/tag_reference',       :action => 'tag_reference' 
     28    page.clear_cache            'admin/pages/cache/clear',            :action => 'clear_cache'     
    2829  end 
    2930 
    3031  # Layouts Routes 
    3132  map.with_options(:controller => 'admin/layout') do |layout| 
    32     layout.layout_index    'admin/layouts',                      :action => 'index' 
    33     layout.layout_edit     'admin/layouts/edit/:id',             :action => 'edit' 
    34     layout.layout_new      'admin/layouts/new',                  :action => 'new' 
    35     layout.layout_remove   'admin/layouts/remove/:id',           :action => 'remove'   
     33    layout.layout_index         'admin/layouts',                      :action => 'index' 
     34    layout.layout_edit          'admin/layouts/edit/:id',             :action => 'edit' 
     35    layout.layout_new           'admin/layouts/new',                  :action => 'new' 
     36    layout.layout_remove        'admin/layouts/remove/:id',           :action => 'remove'   
    3637  end   
    3738                        
    3839  # Snippets Routes 
    3940  map.with_options(:controller => 'admin/snippet') do |snippet| 
    40     snippet.snippet_index  'admin/snippets',                     :action => 'index' 
    41     snippet.snippet_edit   'admin/snippets/edit/:id',            :action => 'edit' 
    42     snippet.snippet_new    'admin/snippets/new',                 :action => 'new' 
    43     snippet.snippet_remove 'admin/snippets/remove/:id',          :action => 'remove' 
     41    snippet.snippet_index       'admin/snippets',                     :action => 'index' 
     42    snippet.snippet_edit        'admin/snippets/edit/:id',            :action => 'edit' 
     43    snippet.snippet_new         'admin/snippets/new',                 :action => 'new' 
     44    snippet.snippet_remove      'admin/snippets/remove/:id',          :action => 'remove' 
    4445  end 
    4546                         
    4647  # Users Routes 
    4748  map.with_options(:controller => 'admin/user') do |user| 
    48     user.user_index        'admin/users',                        :action => 'index' 
    49     user.user_edit         'admin/users/edit/:id',               :action => 'edit' 
    50     user.user_new          'admin/users/new',                    :action => 'new' 
    51     user.user_remove       'admin/users/remove/:id',             :action => 'remove' 
    52     user.user_preferences  'admin/preferences',                  :action => 'preferences' 
     49    user.user_index             'admin/users',                        :action => 'index' 
     50    user.user_edit              'admin/users/edit/:id',               :action => 'edit' 
     51    user.user_new               'admin/users/new',                    :action => 'new' 
     52    user.user_remove            'admin/users/remove/:id',             :action => 'remove' 
     53    user.user_preferences       'admin/preferences',                  :action => 'preferences' 
    5354  end 
    5455   
    5556  # Extension Routes 
    5657  map.with_options(:controller => 'admin/extension') do |extension| 
    57     extension.extension_index  'admin/extensions',                :action => 'index' 
    58     extension.extension_update 'admin/extensions/update',         :action => 'update' 
     58    extension.extension_index   'admin/extensions',                   :action => 'index' 
     59    extension.extension_update  'admin/extensions/update',            :action => 'update' 
    5960  end 
    6061   
    6162  # Site URLs 
    6263  map.with_options(:controller => 'site') do |site| 
    63     site.homepage          '',                                   :action => 'show_page', :url => '/' 
    64     site.not_found         'error/404',                          :action => 'not_found' 
    65     site.error             'error/500',                          :action => 'error' 
     64    site.homepage               '',                                   :action => 'show_page', :url => '/' 
     65    site.not_found              'error/404',                          :action => 'not_found' 
     66    site.error                  'error/500',                          :action => 'error' 
    6667 
    6768    # Everything else 
    68     site.connect           '*url',                               :action => 'show_page' 
     69    site.connect                '*url',                               :action => 'show_page' 
    6970  end 
    7071   
    7172end 
  • public/stylesheets/admin/global.css

    old new  
    182182  color: #0c0; 
    183183} 
    184184#content table.index .add-child { 
    185   width: 100px; 
     185  width: 77px; 
    186186  padding-left: 0; 
    187187} 
    188188#content table.index .remove { 
    189   width: 100px; 
     189  width: 69px; 
    190190  padding-left: 0; 
    191191} 
     192#content table.index .reorder { 
     193  width: 70px; 
     194  padding-left: 0; 
     195} 
    192196#content table.index .node .layout, 
    193197#content table.index .node .snippet, 
    194198#content table.index .node .user {