Permalink
Browse files

Move add-child menu inline

  • Loading branch information...
1 parent 268921b commit 236c7c15e07a34ea67334cbfaed2a500bdb0dc06 @joshfrench joshfrench committed Aug 3, 2010
@@ -0,0 +1,42 @@
+module PageTypesHelper
+ def self.included(base)
+ base.alias_method_chain :children_for, :roles
+ end
+
+ def child_link_for(page)
+ case page.allowed_children.flatten.compact.size
+ when 0
+ content_tag :span, image('plus_disabled') + ' ' + t('add_child'), :class => 'action disabled'
+ when 1
+ link_to image('plus') + ' ' + t('add_child'), new_admin_page_child_path(page, :page_class => children_for(page).first), :class => "action"
+ else
+ link_to image('plus') + ' ' + t('add_child'), "#allowed_children_#{page.id}", :class => "action dropdown"
+ end
+ end
+
+ def child_menu_for(page)
+ children = children_for(page)
+ return nil if children.size < 2
+ children.unshift(children.delete(page.default_child), :separator) if children.include?(page.default_child)
+ name_for = proc { |p| (name = p.name.to_name('Page')).blank? ? 'Page' : name }
+ content_tag :ul, :class => 'menu', :id => "allowed_children_#{page.id}" do
+ children.map do |child|
+ if child == :separator
+ content_tag :li, nil, :class => 'separator'
+ else
+ content_tag :li, link_to(name_for[child], new_admin_page_child_path(page, :page_class => child))
+ end
+ end
+ end
+ end
+
+ def children_for(page)
+ page.allowed_children
+ end
+
+ def children_for_with_roles(page)
+ children = children_for_without_roles(page)
+ children.reject! { |p| p.new.virtual? } unless admin?
+ children
+ end
+end
@@ -1,4 +1,5 @@
- include_javascript 'admin/dropdown'
%td.actions
- = link_to image('plus') + ' ' + t('add_child'), admin_page_types_url(page), :class => "action dropdown"
- = link_to image('minus') + ' ' + t('remove'), remove_admin_page_url(page), :class => "action"
+ = child_link_for(page)
+ = link_to image('minus') + ' ' + t('remove'), remove_admin_page_url(page), :class => "action"
+ = child_menu_for(page)
@@ -0,0 +1,8 @@
+module PageTypesExtension::PageExtensions
+ def default_child
+ Page
+ end
+ def allowed_children
+ [default_child, *Page.descendants.sort_by(&:name)]
+ end
+end
@@ -7,5 +7,7 @@ class PageTypesExtension < Radiant::Extension
def activate
Admin::PagesController.send :include, PagesControllerExtensions
+ Admin::PagesController.helper :page_types
+ Page.send :include, PageExtensions
end
end
@@ -0,0 +1,38 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+describe PageTypesExtension::PageTypesHelper do
+ dataset :home_page
+ attr_reader :current_user
+ include ApplicationHelper
+ include PageTypesExtension::PageTypesHelper
+
+ before do
+ @page = pages(:home)
+ @current_user = stub(:admin? => true)
+ end
+
+ describe "#child_link" do
+ it "should disable a link with no options" do
+ @page.stub!(:allowed_children).and_return([])
+ child_link_for(@page).should match('<span class="action disabled">')
+ end
+
+ it "should link to Page#new with a single option" do
+ @page.stub!(:allowed_children).and_return([Page])
+ child_link_for(@page).should match(Regexp.escape(new_admin_page_child_path(@page, :page_class => 'Page')))
+ end
+
+ it "should link to div with multiple options" do
+ @page.stub!(:allowed_children).and_return([Page,FileNotFoundPage])
+ child_link_for(@page).should match("#allowed_children_#{page_id(:home)}")
+ end
+ end
+
+ describe "#children_for" do
+ it "should not show virtual pages to designers" do
+ @page.stub!(:allowed_children).and_return([Page, ArchivePage, FileNotFoundPage])
+ current_user.stub!(:admin?).and_return(false)
+ children_for(@page).flatten.should_not include(FileNotFoundPage)
+ end
+ end
+end
@@ -0,0 +1,14 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+describe Page do
+
+ before do
+ @page = Page.new
+ end
+
+ it "should list descendants" do
+ children = @page.allowed_children
+ children.should include(Page)
+ children.should include(FileNotFoundPage)
+ end
+end

0 comments on commit 236c7c1

Please sign in to comment.