From a4c900af4de3e1c53113aa646e8f8ec20601c0d5 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 22 May 2009 14:43:52 +0800 Subject: [PATCH] Add Page position and parent_page_id --- app/controllers/admin/pages_controller.rb | 6 ++++-- app/models/page.rb | 22 ++++++++++++++++++++- app/views/admin/pages/_form.html.erb | 19 ++++++++++++++++++ app/views/admin/pages/edit.html.erb | 15 +------------- app/views/admin/pages/index.html.erb | 24 +++++++++++++++++++++-- app/views/admin/pages/new.html.erb | 17 +++------------- 6 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 app/views/admin/pages/_form.html.erb diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index a44d1491..35634aef 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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,7 +25,9 @@ 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 format.xml { render :xml => @pages } diff --git a/app/models/page.rb b/app/models/page.rb index a34097fb..4ed3ff24 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb new file mode 100644 index 00000000..f180be1a --- /dev/null +++ b/app/views/admin/pages/_form.html.erb @@ -0,0 +1,19 @@ +

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

+ +

+<%= f.label :layout_name, "Layout Name" %> +<%= f.text_field :layout_name, :class => 'text' %> +

+ +

+<%= f.label :content, "Content" %> +<%= f.text_area :content, :size => '100x30' %> +

+ +

+<%= f.label :is_published, "Is Published" %> +<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No +

\ No newline at end of file diff --git a/app/views/admin/pages/edit.html.erb b/app/views/admin/pages/edit.html.erb index ec97d1f7..04b7aea6 100644 --- a/app/views/admin/pages/edit.html.erb +++ b/app/views/admin/pages/edit.html.erb @@ -3,20 +3,7 @@ <% form_for @page, :url => admin_page_path(@page) do |f| %> <%= f.error_messages %> -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

- -

- <%= f.label :layout_name, "Layout Name" %> - <%= f.text_field :layout_name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

+ <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Update' %> diff --git a/app/views/admin/pages/index.html.erb b/app/views/admin/pages/index.html.erb index 889f1fe8..fb13788b 100644 --- a/app/views/admin/pages/index.html.erb +++ b/app/views/admin/pages/index.html.erb @@ -2,14 +2,34 @@ + + + + + + + <% @pages.each do |page| %> - - + + + + + + <% end %>
NameChildrenLayoutParentPositionPublished?Action
<%= 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 %>
    + <% page.children.each do |child| %> +
  • <%= child.name %>
  • + <% end -%> +
<%= page.layout_name %> + <%= page.parent_page_id %><%= page.position %><%= page.is_published.to_s %> + <%= 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 %> +
diff --git a/app/views/admin/pages/new.html.erb b/app/views/admin/pages/new.html.erb index 7a1390fb..1f865e2b 100644 --- a/app/views/admin/pages/new.html.erb +++ b/app/views/admin/pages/new.html.erb @@ -2,25 +2,14 @@ <% form_for :page, :url => admin_pages_path do |f| %> <%= f.error_messages %> - -

- <%= f.label :name, "Name" %> - <%= f.text_field :name, :class => 'text' %> -

+ <%= f.hidden_field :parent_page_id %> -

- <%= f.label :layout_name, "Layout Name" %> - <%= f.text_field :layout_name, :class => 'text' %> -

- -

- <%= f.label :content, "Content" %> - <%= f.text_area :content, :size => '100x30' %> -

+ <%= render :partial => "form", :locals => { :f => f } %>

<%= f.submit 'Create' %>

+ <% end %> <%= link_to 'Back', admin_pages_path %> \ No newline at end of file