Skip to content

Commit

Permalink
Now rendering pages/layouts/snippets as XML when requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Stephens committed Jul 13, 2009
1 parent 089a834 commit c2ba448
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 55 deletions.
7 changes: 5 additions & 2 deletions app/controllers/admin/layouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ class Admin::LayoutsController < Admin::ResourceController
:when => [:developer, :admin],
:denied_url => { :controller => 'admin/pages', :action => 'index' },
:denied_message => 'You must have developer privileges to perform this action.'

def show
redirect_to edit_admin_layout_path(params[:id])
respond_to do |format|
format.xml { super }
format.html { redirect_to edit_admin_layout_path(params[:id]) }
end
end
end
13 changes: 8 additions & 5 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Admin::PagesController < Admin::ResourceController
before_filter :initialize_meta_rows_and_buttons, :only => [:new, :edit, :create, :update]
before_filter :count_deleted_pages, :only => [:destroy]

responses do |r|
r.plural.js do
@level = params[:level].to_i
Expand All @@ -10,14 +10,17 @@ class Admin::PagesController < Admin::ResourceController
render :action => 'children.html.haml', :layout => false
end
end

def index
@homepage = Page.find_by_parent_id(nil)
response_for :plural
end

def show
redirect_to edit_admin_page_path(params[:id])
respond_to do |format|
format.xml { super }
format.html { redirect_to edit_admin_page_path(params[:id]) }
end
end

def new
Expand All @@ -27,7 +30,7 @@ def new
end
response_for :singular
end

private
def model_class
if params[:page_id]
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/admin/snippets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Admin::SnippetsController < Admin::ResourceController
def show
redirect_to edit_admin_snippet_path(params[:id])
respond_to do |format|
format.xml { super }
format.html { redirect_to edit_admin_snippet_path(params[:id]) }
end
end
end
end
30 changes: 18 additions & 12 deletions spec/controllers/admin/layouts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
it "should handle Layouts" do
controller.class.model_class.should == Layout
end


describe "show" do
it "should redirect to the edit action" do
get :show, :id => 1
response.should redirect_to(edit_admin_layout_path(params[:id]))
end

it "should show xml when format is xml" do
layout = Layout.first
get :show, :id => layout.id, :format => "xml"
response.body.should == layout.to_xml
end
end

describe "with invalid page id" do
[:edit, :remove].each do |action|
before do
Expand Down Expand Up @@ -66,22 +72,22 @@
end

it "should allow access to developers for the #{action} action" do
lambda {
send(method, action, :id => layout_id(:main))
}.should restrict_access(:allow => [users(:developer)],
lambda {
send(method, action, :id => layout_id(:main))
}.should restrict_access(:allow => [users(:developer)],
:url => '/admin/pages')
end

it "should allow access to admins for the #{action} action" do
lambda {
send(method, action, :id => layout_id(:main))
}.should restrict_access(:allow => [users(:developer)],
lambda {
send(method, action, :id => layout_id(:main))
}.should restrict_access(:allow => [users(:developer)],
:url => '/admin/pages')
end

it "should deny non-developers and non-admins for the #{action} action" do
lambda {
send(method, action, :id => layout_id(:main))
lambda {
send(method, action, :id => layout_id(:main))
}.should restrict_access(:deny => [users(:non_admin), users(:existing)],
:url => '/admin/pages')
end
Expand Down
64 changes: 35 additions & 29 deletions spec/controllers/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
end

it "should route children to the pages controller" do
route_for(:controller => "admin/pages", :page_id => '1',
route_for(:controller => "admin/pages", :page_id => '1',
:action => "index").should == '/admin/pages/1/children'
route_for(:controller => "admin/pages", :page_id => '1',
route_for(:controller => "admin/pages", :page_id => '1',
:action => 'new').should == '/admin/pages/1/children/new'
end

describe "show" do
it "should redirect to the edit action" do
get :show, :id => 1
response.should redirect_to(edit_admin_page_path(params[:id]))
end

it "should show xml when format is xml" do
page = Page.first
get :show, :id => page.id, :format => "xml"
response.body.should == page.to_xml
end
end

describe "with invalid page id" do
[:edit, :remove].each do |action|
before do
Expand Down Expand Up @@ -52,7 +58,7 @@
flash[:notice].should == 'Page could not be found.'
end
end

describe "viewing the sitemap" do
integrate_views

Expand Down Expand Up @@ -101,7 +107,7 @@
assert_rendered_nodes_where { |page| [nil, page_id(:home), page_id(:parent)].include?(page.parent_id) }
assigns(:homepage).should_not be_nil
end

it "should render the appropriate children when branch of the site map is expanded via AJAX" do
xml_http_request :get, :index, :page_id => page_id(:home), :level => '1'
response.should be_success
Expand All @@ -111,9 +117,9 @@
response.charset.should == 'utf-8'
end
end

describe "permissions" do

[:admin, :developer, :non_admin, :existing].each do |user|
{
:post => :create,
Expand All @@ -125,34 +131,34 @@
send method, action, :id => Page.first.id
response.should redirect_to('/admin/login')
end

it "should allow access to #{user.to_s.humanize}s for the #{action} action" do
login_as user
send method, action, :id => Page.first.id
response.should redirect_to('/admin/pages')
end
end
end

[:index, :show, :new, :edit, :remove].each do |action|
before :each do
@parameters = lambda do
@parameters = lambda do
case action
when :index
{}
when :new
{:page_id => page_id(:home)}
else
{:id => Page.first.id}
{:id => Page.first.id}
end
end
end

it "should require login to access the #{action} action" do
logout
lambda { send(:get, action, @parameters.call) }.should require_login
end

if action == :show
it "should request authentication for API access on show" do
logout
Expand All @@ -161,33 +167,33 @@
end
else
it "should allow access to admins for the #{action} action" do
lambda {
send(:get, action, @parameters.call)
}.should restrict_access(:allow => [users(:admin)],
lambda {
send(:get, action, @parameters.call)
}.should restrict_access(:allow => [users(:admin)],
:url => '/admin/pages')
end

it "should allow access to developers for the #{action} action" do
lambda {
send(:get, action, @parameters.call)
}.should restrict_access(:allow => [users(:developer)],
lambda {
send(:get, action, @parameters.call)
}.should restrict_access(:allow => [users(:developer)],
:url => '/admin/pages')
end

it "should allow non-developers and non-admins for the #{action} action" do
lambda {
send(:get, action, @parameters.call)
lambda {
send(:get, action, @parameters.call)
}.should restrict_access(:allow => [users(:non_admin), users(:existing)],
:url => '/admin/pages')
end
end
end
end


describe "prompting page removal" do
integrate_views

# TODO: This should be in a view or integration spec
it "should render the expanded descendants of the page being removed" do
get :remove, :id => page_id(:parent), :format => 'html' # shouldn't need this!
Expand All @@ -197,7 +203,7 @@
end
end
end

it "should initialize meta and buttons_partials in new action" do
get :new, :page_id => page_id(:home)
response.should be_success
Expand All @@ -211,12 +217,12 @@
assigns(:meta).should be_kind_of(Array)
assigns(:buttons_partials).should be_kind_of(Array)
end

it "should clear the page cache when saved" do
Radiant::Cache.should_receive(:clear)
put :update, :id => page_id(:home), :page => {:breadcrumb => 'Homepage'}
end

protected

def assert_rendered_nodes_where(&block)
Expand Down
16 changes: 11 additions & 5 deletions spec/controllers/admin/snippets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
it "should handle Snippets" do
controller.class.model_class.should == Snippet
end


describe "show" do
it "should redirect to the edit action" do
get :show, :id => 1
response.should redirect_to(edit_admin_snippet_path(params[:id]))
end

it "should show xml when format is xml" do
snippet = Snippet.first
get :show, :id => snippet.id, :format => "xml"
response.body.should == snippet.to_xml
end
end

describe "with invalid snippet id" do
[:edit, :remove].each do |action|
before do
Expand Down Expand Up @@ -96,9 +102,9 @@
end
end
end

it "should clear the page cache when saved" do
Radiant::Cache.should_receive(:clear)
put :update, :id => snippet_id(:first), :snippet => {:content => "Foobar."}
end
end
end

0 comments on commit c2ba448

Please sign in to comment.