Skip to content
Browse files

Changed the sort controller method, it's now a couple of lines long a…

…nd also specced.

Few style fixes and we'll be good to go.
  • Loading branch information...
1 parent 9cc1356 commit f73cd962ad1a8d37ba241d059f26d9c5c4b002fa Dirk Kelly committed
View
2 app/views/admin/pages/_node.html.haml
@@ -1,4 +1,4 @@
-%li.node.page{:class =>"#{level}#{children_class}#{virtual_class}", :id => "page_#{page.id}"}
+%li.node.page{:class =>"#{level}#{children_class}#{virtual_class}", :id => "page_#{page.id}", :'data-page_id' => page.id}
- render_region :node, :locals => {:page => page, :level => level, :simple => simple} do |node|
- node.title_column do
%span.title{:style => "padding-left: 15px"}
View
4 config/routes.rb
@@ -1,7 +1,7 @@
ActionController::Routing::Routes.draw do |map|
- map.with_options :controller => 'admin/pages' do |page|
- page.admin_pages_move_to "admin/pages/:id/move_to/:rel/:pos/:copy", :action => "move_to"
+ map.namespace :admin do |admin|
+ admin.resources :pages, :collection => { :sort => :put }
end
end
View
145 lib/drag_order/controllers/admin/pages_controller.rb
@@ -8,141 +8,30 @@ def self.included(base)
helper_method :order_dragger
- def move_to
- @page = Page.find(params[:id])
- @old_parent = @page.parent
- @current_position = params[:pos].to_i
-
- ensure_no_nil_position_values
-
- remove_page_from_old_position unless copying?
-
- @target = Page.find(params[:rel])
-
- make_room_for_page if @current_position != 2
-
- if copying?
- @orig_parts = @page.parts
- @page = @page.clone
- end
-
- @target.reload
-
- put_page
-
- solve_slug_conflicts if copying? || new_parent_different?
-
- @page.save!
-
- create_copy_of_parts if copying?
-
- clear_cache
- redirect_back_or_to_admin_pages_page
- end
-
- private
- def order_dragger
- %{<img src="/images/admin/extensions/drag_order/handle.png" alt ="Drag this icon to move the page" />}
- end
-
- def copying?
- params[:copy].to_i > 0
- end
-
- def ensure_no_nil_position_values
- if @page.newly_created_siblings?
- i = 1
- @page.siblings_and_self.each do |p|
- p.position = i
- p.save
- i += 1
+ def sort
+ begin
+ parent = Page.find(params[:parent_id])
+ params[:children].split(',').each_with_index do |child,i|
+ Page.find(child).update_attributes!(
+ :position => i+1,
+ :parent_id => params[:parent_id]
+ )
end
- @page.reload
- end
- end
-
- def remove_page_from_old_position
- @page.following_siblings.each do |sibling|
- sibling.position -= 1
- sibling.save!
- end
- end
-
- def put_page
- if @current_position != 2
- @page.parent = @target.parent
- @page.position = @target.position.to_i + (@current_position == 1 ? 1 : -1)
- else
- @page.parent = @target
- @page.position = 1
- end
- end
-
- def make_room_for_page
- new_siblings = Page.children_of_after_position(@target.parent_id, @target.position + @current_position)
- new_siblings.each do |sibling|
- if sibling != @page || copying?
- sibling.position += 1
- sibling.save!
+
+ respond_to do |format|
+ format.js { render :text => 'Pages successfully sorted.' }
end
- end
- end
-
- def new_parent_different?
- # @page.parent.changed? always gives false...
- @page.parent != @old_parent
- end
-
- def solve_slug_conflicts
- check_slug = @page.slug.sub(/-copy-?[0-9]*$/, "")
- count = 0
- parent_id = @current_position == 2 ? @target.id : @target.parent.id
- duplicates = Page.children_of_with_slug_like(parent_id, check_slug )
- duplicates.each do |d|
- m = d.slug.match("^#{check_slug}(-copy-?([0-9]*))?$")
- if !m.nil?
- if !(m[2].nil? || m[2] == "")
- nc = m[2].to_i + 1
- elsif m[1]
- nc = 2
- else
- nc = 1
- end
- count = nc if nc > count
+ rescue
+ respond_to do |format|
+ format.js { render :text => 'Could not sort Pages.', :status => :unprocessable_entity }
end
end
- if count > 0
- # Remove old copy counters
- re = / - COPY ?[0-9]*$/
- @page.title.sub! re, ""
- @page.breadcrumb.sub! re, ""
- # Add new copy counters
- @page.slug = check_slug + "-copy" + (count > 1 ? "-" + count.to_s : "")
- @page.title += " - COPY" + (count > 1 ? " " + count.to_s : "")
- @page.breadcrumb += " - COPY" + (count > 1 ? " " + count.to_s : "")
- end
end
- def create_copy_of_parts
- @orig_parts.each do |op|
- @page.parts.create({
- :name => op.name,
- :filter_id => op.filter_id,
- :content => op.content
- })
- end
- end
-
- def redirect_back_or_to_admin_pages_page
- redirect_to(:back) rescue redirect_to(admin_page_url)
- end
+ private
- def clear_cache
- if defined? ResponseCache == 'constant'
- ResponseCache.instance.clear
- else
- Radiant::Cache.clear
- end
+ def order_dragger
+ %{<img src="/images/admin/extensions/drag_order/handle.png" alt ="Drag this icon to move the page" />}
end
end
View
16 public/javascripts/admin/extensions/drag_order/drag_order.js
@@ -20,6 +20,22 @@ var DragOrderIndex = Class.create({
onEnd: function(drag, event) {
drag.element.removeClassName('drag_move');
}
+ },
+ onDrop: function(drag, drop, event){
+ var parent = drag.element.up('.page')
+ var elements = '';
+ parent.down('.children').immediateDescendants().each(function(page) {
+ elements += page.readAttribute('data-page_id') + ','
+ });
+ elements = elements.slice(0,-1);
+
+ new Ajax.Request('/admin/pages/sort.js', {
+ method: 'put',
+ parameters: {
+ 'parent_id': parent.readAttribute('data-page_id'),
+ 'children' : elements
+ }
+ })
}
});
tree.setSortable();
View
48 spec/controllers/pages_controller_spec.rb
@@ -0,0 +1,48 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+describe Admin::PagesController do
+
+ dataset :users, :sort_pages
+
+ before :each do
+ login_as :admin
+ end
+
+ describe '#sort' do
+ before :each do
+ @params = {
+ :parent_id => pages(:one).id,
+ :children => "#{pages(:four).id},#{pages(:three).id},#{pages(:two).id}"
+ }
+ end
+
+ context 'parent not sent' do
+ it 'should return an error' do
+ put :sort, :children => @params[:children], :format => 'js'
+
+ response.should_not be_success
+ response.body.should === 'Could not sort Pages.'
+ end
+ end
+
+ context 'children not sent' do
+ it 'should return an error' do
+ put :sort, :parent_id => @params[:parent_id], :format => 'js'
+
+ response.should_not be_success
+ response.body.should === 'Could not sort Pages.'
+ end
+ end
+
+ context 'parent and children sent' do
+ it 'should return success' do
+ put :sort, :parent_id => @params[:parent_id], :children => @params[:children], :format => 'js'
+
+ response.should be_success
+ response.body.should === 'Pages successfully sorted.'
+ end
+ end
+
+ end
+
+end
View
31 spec/datasets/sort_pages.rb
@@ -0,0 +1,31 @@
+class SortPagesDataset < Dataset::Base
+ def load
+ # TODO Investigate why inflectors are breaking here
+ create_record :page, :one,
+ :title => 'One',
+ :slug => 'one',
+ :breadcrumb => 'one',
+ :position => 1
+
+ create_record :page, :two,
+ :title => 'Two',
+ :slug => 'two',
+ :breadcrumb => 'two',
+ :position => 2,
+ :parent => pages(:one)
+
+ create_record :page, :three,
+ :title => 'Three',
+ :slug => 'three',
+ :breadcrumb => 'three',
+ :position => 3,
+ :parent => pages(:one)
+
+ create_record :page, :four,
+ :title => 'Four',
+ :slug => 'four',
+ :breadcrumb => 'four',
+ :position => 4,
+ :parent => pages(:one)
+ end
+end
View
21 spec/spec_helper.rb
@@ -11,27 +11,12 @@
end
require "#{RADIANT_ROOT}/spec/spec_helper"
-if File.directory?(File.dirname(__FILE__) + "/scenarios")
- Scenario.load_paths.unshift File.dirname(__FILE__) + "/scenarios"
-end
+Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")
+
if File.directory?(File.dirname(__FILE__) + "/matchers")
Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
end
Spec::Runner.configure do |config|
- # config.use_transactional_fixtures = true
- # config.use_instantiated_fixtures = false
- # config.fixture_path = RAILS_ROOT + '/spec/fixtures'
-
- # You can declare fixtures for each behaviour like this:
- # describe "...." do
- # fixtures :table_a, :table_b
- #
- # Alternatively, if you prefer to declare them only once, you can
- # do so here, like so ...
- #
- # config.global_fixtures = :table_a, :table_b
- #
- # If you declare global fixtures, be aware that they will be declared
- # for all of your examples, even those that don't use them.
+ config.mock_with :rr
end

0 comments on commit f73cd96

Please sign in to comment.
Something went wrong with that request. Please try again.