From 66604d00f459298c18ae47a303b10f6e321ef3e2 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 8 May 2009 01:18:16 +0800 Subject: [PATCH] Add admin/pages scaffold and basic page render --- app/controllers/admin/pages_controller.rb | 82 +++++++++++++++++++++++ app/controllers/pages_controller.rb | 14 ++++ app/helpers/pages_helper.rb | 2 + app/models/page.rb | 9 +++ app/views/admin/pages/edit.html.erb | 21 ++++++ app/views/admin/pages/index.html.erb | 18 +++++ app/views/admin/pages/new.html.erb | 21 ++++++ app/views/layouts/pages.html.erb | 17 +++++ config/database.yml.example | 22 ++++++ config/environment.rb | 10 ++- config/routes.rb | 12 +++- public/stylesheets/scaffold.css | 54 +++++++++++++++ 12 files changed, 277 insertions(+), 5 deletions(-) create mode 100644 app/controllers/admin/pages_controller.rb create mode 100644 app/controllers/pages_controller.rb create mode 100644 app/helpers/pages_helper.rb create mode 100644 app/models/page.rb create mode 100644 app/views/admin/pages/edit.html.erb create mode 100644 app/views/admin/pages/index.html.erb create mode 100644 app/views/admin/pages/new.html.erb create mode 100644 app/views/layouts/pages.html.erb create mode 100644 config/database.yml.example create mode 100644 public/stylesheets/scaffold.css diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb new file mode 100644 index 00000000..2f2dd0b4 --- /dev/null +++ b/app/controllers/admin/pages_controller.rb @@ -0,0 +1,82 @@ +class Admin::PagesController < ApplicationController + # GET /pages + # GET /pages.xml + def index + @pages = Page.all + + 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(params[:id]) + + redirect_to "/#{@page.name}" + 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 => @pages } + 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 admin_pages_url } + format.xml { render :xml => @page, :status => :created, :location => @pages } + 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 admin_pages_url } + 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 admin_pages_url } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb new file mode 100644 index 00000000..195065b9 --- /dev/null +++ b/app/controllers/pages_controller.rb @@ -0,0 +1,14 @@ +class PagesController < ApplicationController + + def show + @page = Page.find_by_name(params[:page_name]) + + if @page + render :text => @page.content + else + render :text => '404 not found' + end + + end + +end diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb new file mode 100644 index 00000000..2c057fd0 --- /dev/null +++ b/app/helpers/pages_helper.rb @@ -0,0 +1,2 @@ +module PagesHelper +end diff --git a/app/models/page.rb b/app/models/page.rb new file mode 100644 index 00000000..56caa2d0 --- /dev/null +++ b/app/models/page.rb @@ -0,0 +1,9 @@ +class Page < CouchFoo::Base + + property :name, String + property :parent_name, String + property :content, String + + validates_presence_of :name + +end \ No newline at end of file diff --git a/app/views/admin/pages/edit.html.erb b/app/views/admin/pages/edit.html.erb new file mode 100644 index 00000000..f5ed1928 --- /dev/null +++ b/app/views/admin/pages/edit.html.erb @@ -0,0 +1,21 @@ +

Editing pages

+ +<% form_for @page, :url => admin_page_path(@page) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name, "Name" %> + <%= f.text_field :name %> +

+ +

+ <%= f.label :content, "Content" %> + <%= f.text_area :content %> +

+ +

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Back', admin_pages_path %> \ No newline at end of file diff --git a/app/views/admin/pages/index.html.erb b/app/views/admin/pages/index.html.erb new file mode 100644 index 00000000..2e0c9baf --- /dev/null +++ b/app/views/admin/pages/index.html.erb @@ -0,0 +1,18 @@ +

Listing pages

+ + + + + +<% @pages.each do |page| %> + + + + + +<% end %> +
<%= link_to page.name, admin_page_path(page) %><%= link_to 'Edit', edit_admin_page_path(page) %><%= link_to 'Destroy', admin_page_path(page), :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New pages', new_admin_page_path %> \ No newline at end of file diff --git a/app/views/admin/pages/new.html.erb b/app/views/admin/pages/new.html.erb new file mode 100644 index 00000000..5026da7e --- /dev/null +++ b/app/views/admin/pages/new.html.erb @@ -0,0 +1,21 @@ +

New pages

+ +<% form_for :page, :url => admin_pages_path do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name, "Name" %> + <%= f.text_field :name %> +

+ +

+ <%= f.label :content, "Content" %> + <%= f.text_area :content %> +

+ +

+ <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', admin_pages_path %> \ No newline at end of file diff --git a/app/views/layouts/pages.html.erb b/app/views/layouts/pages.html.erb new file mode 100644 index 00000000..b2cdf2c5 --- /dev/null +++ b/app/views/layouts/pages.html.erb @@ -0,0 +1,17 @@ + + + + + + Pages: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/config/database.yml.example b/config/database.yml.example new file mode 100644 index 00000000..025d62a8 --- /dev/null +++ b/config/database.yml.example @@ -0,0 +1,22 @@ +# SQLite version 3.x +# gem install sqlite3-ruby (not necessary on OS X Leopard) +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + adapter: sqlite3 + database: db/test.sqlite3 + pool: 5 + timeout: 5000 + +production: + adapter: sqlite3 + database: db/production.sqlite3 + pool: 5 + timeout: 5000 diff --git a/config/environment.rb b/config/environment.rb index ff450c78..67a14735 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -33,9 +33,13 @@ Rails::Initializer.run do |config| # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. - config.time_zone = 'UTC' - + config.time_zone = "Taipei" + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] # config.i18n.default_locale = :de -end \ No newline at end of file + +end + +require 'couch_foo' +CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "r4") \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 4f3d9d22..7cdda3c3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ ActionController::Routing::Routes.draw do |map| + + map.namespace :admin do |admin| + admin.resources :pages + end + # The priority is based upon order of creation: first created -> highest priority. # Sample of regular route: @@ -38,6 +43,9 @@ ActionController::Routing::Routes.draw do |map| # Install the default routes as the lowest priority. # Note: These default routes make all actions in every controller accessible via GET requests. You should # consider removing the them or commenting them out if you're using named routes and resources. - map.connect ':controller/:action/:id' - map.connect ':controller/:action/:id.:format' + + map.connect ':page_name', :controller => 'pages', :action => 'show' + + #map.connect ':controller/:action/:id' + #map.connect ':controller/:action/:id.:format' end diff --git a/public/stylesheets/scaffold.css b/public/stylesheets/scaffold.css new file mode 100644 index 00000000..093c2099 --- /dev/null +++ b/public/stylesheets/scaffold.css @@ -0,0 +1,54 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +.fieldWithErrors { + padding: 2px; + background-color: red; + display: table; +} + +#errorExplanation { + width: 400px; + border: 2px solid red; + padding: 7px; + padding-bottom: 12px; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#errorExplanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + background-color: #c00; + color: #fff; +} + +#errorExplanation p { + color: #333; + margin-bottom: 0; + padding: 5px; +} + +#errorExplanation ul li { + font-size: 12px; + list-style: square; +} +