Add Page position and parent_page_id

This commit is contained in:
Wen-Tien Chang 2009-05-22 14:43:52 +08:00
parent 96f186a71b
commit a4c900af4d
6 changed files with 70 additions and 33 deletions

View File

@ -5,7 +5,7 @@ class Admin::PagesController < ApplicationController
# GET /pages
# GET /pages.xml
def index
@pages = Page.all
@pages = Page.all( :conditions => { :parent_page_id => "root" } )
respond_to do |format|
format.html # index.html.erb
@ -25,6 +25,8 @@ class Admin::PagesController < ApplicationController
# GET /pages/new.xml
def new
@page = Page.new
@page.is_published = true
@page.parent_page_id = params[:parent_page_id]
respond_to do |format|
format.html # new.html.erb

View File

@ -1,16 +1,23 @@
class Page < CouchFoo::Base
property :name, String
property :parent_name, String
property :parent_page_id, String
property :content, String
property :layout_id, String
property :layout_name, String
property :position, Integer
property :is_published, Boolean
belongs_to :layout
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
validates_presence_of :name
validates_presence_of :position
before_save :setup_layout_id
before_validation :setup_default_value
default_sort :position
def setup_layout_id
if self.layout_name.blank?
@ -20,4 +27,17 @@ class Page < CouchFoo::Base
end
end
protected
def setup_default_value
unless self.position
max_page = Page.last( :use_key => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
unless self.parent_page_id
self.parent_page_id = "root"
end
end
end

View File

@ -0,0 +1,19 @@
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name, :class => 'text' %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>
</p>
<p>
<%= f.label :is_published, "Is Published" %>
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
</p>

View File

@ -3,20 +3,7 @@
<% form_for @page, :url => admin_page_path(@page) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name, :class => 'text' %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>
</p>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Update' %>

View File

@ -2,14 +2,34 @@
<table>
<tr>
<th>Name</th>
<th>Children</th>
<th>Layout</th>
<th>Parent</th>
<th>Position</th>
<th>Published?</th>
<th>Action</th>
</tr>
<% @pages.each do |page| %>
<tr>
<td><%= link_to page.name, admin_page_path(page) %></td>
<td><%= link_to 'Edit', edit_admin_page_path(page) %></td>
<td><%= link_to 'Destroy', admin_page_path(page), :confirm => 'Are you sure?', :method => :delete %></td>
<td><ul>
<% page.children.each do |child| %>
<li><%= child.name %></li>
<% end -%>
</td>
<td><%= page.layout_name %></th>
<td><%= page.parent_page_id %></td>
<td><%= page.position %></td>
<td><%= page.is_published.to_s %></td>
<td>
<%= link_to 'Edit', edit_admin_page_path(page) %> |
<%= link_to 'Add Child', new_admin_page_path( :parent_page_id => page.id ) %> |
<%= link_to 'Destroy', admin_page_path(page), :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>
<% end %>
</table>

View File

@ -2,25 +2,14 @@
<% form_for :page, :url => admin_pages_path do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :parent_page_id %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name, :class => 'text' %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>
</p>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', admin_pages_path %>