Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05abbc7
commit 00a7ddc
Showing
14 changed files
with
369 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
class PagesController < ApplicationController | ||
layout "main" | ||
# GET /pages | ||
# GET /pages.xml | ||
|
||
def index | ||
@pages = Page.find( :all, :order => :position ) | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @pages } | ||
end | ||
end | ||
|
||
# GET /pages/1 | ||
# GET /pages/1.xml | ||
|
||
def show | ||
@page = Page.find_by_name(params[:id]) # GET/pages/name | ||
@page ||= Page.find(params[:id]) # GET/pages/id | ||
@pages = Page.find( :all, :order => :position) | ||
#@page = Page.find(params[:id]) | ||
rescue | ||
redirect_to "/404.html" | ||
else | ||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @page } | ||
end | ||
end | ||
|
||
# GET /pages/new | ||
# GET /pages/new.xml | ||
|
||
def new | ||
@page = Page.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.xml { render :xml => @page } | ||
end | ||
end | ||
|
||
# GET /pages/1/edit | ||
|
||
def edit | ||
@page = Page.find(params[:id]) | ||
end | ||
|
||
# POST /pages | ||
# POST /pages.xml | ||
|
||
def create | ||
@page = Page.new(params[:page]) | ||
|
||
respond_to do |format| | ||
if @page.save | ||
flash[:notice] = 'Page was successfully created.' | ||
format.html { redirect_to(@page) } | ||
format.xml { render :xml => @page, :status => :created, :location => @page } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @page.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /pages/1 | ||
# PUT /pages/1.xml | ||
|
||
def update | ||
@page = Page.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @page.update_attributes(params[:page]) | ||
flash[:notice] = 'Page was successfully updated.' | ||
format.html { redirect_to(@page) } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @page.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /pages/1 | ||
# DELETE /pages/1.xml | ||
|
||
def destroy | ||
@page = Page.find(params[:id]) | ||
@page.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(pages_url) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PagesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Page < ActiveRecord::Base | ||
validates_presence_of :name, :title | ||
validates_uniqueness_of :name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | ||
<title>Pages: <%= controller.action_name %></title> | ||
<%= stylesheet_link_tag 'scaffold' %> | ||
</head> | ||
<body> | ||
|
||
<p style="color: green"><%= flash[:notice] %></p> | ||
|
||
<%= yield %> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<h1>Editing page</h1> | ||
|
||
<% form_for(@page) do |f| %> | ||
<%= f.error_messages %> | ||
|
||
<p> | ||
<%= f.label :name %><br /> | ||
<%= f.text_field :name %> | ||
</p> | ||
<p> | ||
<%= f.label :title %><br /> | ||
<%= f.text_field :title %> | ||
</p> | ||
<p> | ||
<%= f.label :body %><br /> | ||
<%= f.text_area :body %> | ||
</p> | ||
<p> | ||
<%= f.label :position %><br /> | ||
<%= f.text_field :position %> | ||
</p> | ||
<p> | ||
<%= f.submit "Update" %> | ||
</p> | ||
<% end %> | ||
<%= link_to 'Show', @page %> | | ||
<%= link_to 'Back', pages_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<h1>Listing pages</h1> | ||
|
||
<table> | ||
<tr> | ||
<th>Name</th> | ||
<th>Title</th> | ||
<th>Body</th> | ||
<th>Updated on</th> | ||
<th>Position</th> | ||
<th>Actions</th> | ||
</tr> | ||
|
||
<%= render(:partial => "index_item", :collection => @pages)%> | ||
</table> | ||
|
||
<br/> | ||
|
||
<%= link_to 'New page', new_page_path %> | ||
<%= link_to 'Admin', :controller => 'admin' %> | ||
<%= link_to 'Logout', :controller => 'admin', :action => 'logout' %> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<h1>New page</h1> | ||
|
||
<% form_for(@page) do |f| %> | ||
<%= f.error_messages %> | ||
|
||
<p> | ||
<%= f.label :name %><br /> | ||
<%= f.text_field :name %> | ||
</p> | ||
<p> | ||
<%= f.label :title %><br /> | ||
<%= f.text_field :title %> | ||
</p> | ||
<p> | ||
<%= f.label :body %><br /> | ||
<%= f.text_area :body %> | ||
</p> | ||
<p> | ||
<%= f.label :position %><br /> | ||
<%= f.text_field :position %> | ||
</p> | ||
<p> | ||
<%= f.submit "Create" %> | ||
</p> | ||
<% end %> | ||
<%= link_to 'Back', pages_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<% content_for(:sidemenu) do %> | ||
<h3>Pages:</h3> | ||
<ul> | ||
<% for item in @pages %> | ||
<li><%= link_to item.title, page_url(:id => item.name) %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
|
||
<div> | ||
<%= h @page.title %> | ||
</div> | ||
|
||
<div> | ||
<%= h @page.body %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CreatePages < ActiveRecord::Migration | ||
def self.up | ||
create_table :pages do |t| | ||
t.string :name | ||
t.string :title | ||
t.text :body | ||
t.integer :position | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :pages | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AddTestUser < ActiveRecord::Migration | ||
def self.up | ||
User.delete_all | ||
|
||
User.create(:name => 'Dabbi', | ||
:email => 'dabbi@example.com', | ||
:password => 'abc123', | ||
:password_confirmation => 'abc123') | ||
end | ||
|
||
def self.down | ||
User.delete_all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class AddTestPages < ActiveRecord::Migration | ||
def self.up | ||
Page.delete_all | ||
|
||
Page.create(:name => 'about', | ||
:title => 'About Us', | ||
:body => 'This is the About page.') | ||
Page.create(:name => 'contact', | ||
:title => 'Contact Us', | ||
:body => 'This is the Contact page.') | ||
Page.create(:name => 'privacy', | ||
:title => 'Privacy policy', | ||
:body => 'This is the Privacy policy page.') | ||
|
||
end | ||
|
||
def self.down | ||
Page.delete_all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
|
||
one: | ||
name: MyString | ||
title: MyString | ||
body: MyText | ||
position: 1 | ||
|
||
two: | ||
name: MyString | ||
title: MyString | ||
body: MyText | ||
position: 1 | ||
|
||
valid_page: | ||
id: 1 | ||
name: about | ||
title: About Us | ||
body: This is the About page. | ||
position: 0 | ||
updated_at: 2009-05-05 17:41:00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'test_helper' | ||
|
||
class PagesControllerTest < ActionController::TestCase | ||
fixtures :pages | ||
|
||
test "index with out user" do | ||
get :index | ||
assert_redirected_to :action => "login" | ||
assert_equal "Please log in", flash[:notice] | ||
end | ||
|
||
test "should get index" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
assert_response :success | ||
assert_not_nil assigns(:pages) | ||
end | ||
|
||
test "should get new" do | ||
get :new, {}, { :user_id => users(:valid_user).id } | ||
assert_response :success | ||
end | ||
|
||
test "should create page" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
assert_difference('Page.count') do | ||
post :create, :page => { :name => 'after', :title => 'After Page' } | ||
end | ||
|
||
assert_redirected_to page_path(assigns(:page)) | ||
end | ||
|
||
test "should show page" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
get :show, :id => pages(:one).id | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
get :edit, :id => pages(:one).id | ||
assert_response :success | ||
end | ||
|
||
test "should update page" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
put :update, :id => pages(:one).id, :page => { } | ||
assert_response :success | ||
end | ||
|
||
test "should destroy page" do | ||
get :index, {}, { :user_id => users(:valid_user).id } | ||
assert_difference('Page.count', -1) do | ||
delete :destroy, :id => pages(:one).id | ||
end | ||
|
||
assert_redirected_to pages_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test_helper' | ||
|
||
class PageTest < ActiveSupport::TestCase | ||
fixtures :pages | ||
|
||
test "create valid page" do | ||
page = Page.new(:name => 'new', | ||
:title => 'New Page') | ||
assert page.save | ||
end | ||
|
||
test "invalid identical pages" do | ||
page = Page.new(:name => 'about', | ||
:title => 'About Us') | ||
assert !page.save | ||
end | ||
|
||
test "invalid with empty attributes" do | ||
page = Page.new | ||
assert !page.valid? | ||
assert page.errors.invalid?(:name) | ||
assert page.errors.invalid?(:title) | ||
end | ||
end |