From e1440a79d86e66c604e45b4a9f69ad5cfd3f31b3 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Thu, 14 Jan 2010 18:30:53 +0800 Subject: [PATCH] Add Item, Page, Component, Link inheritanced models --- .../admin/components_controller.rb | 53 +++++++++++++++++++ app/controllers/admin/items_controller.rb | 17 ++++++ app/controllers/admin/links_controller.rb | 49 +++++++++++++++++ app/controllers/admin/pages_controller.rb | 17 +++--- app/controllers/admin/snippets_controller.rb | 33 +++--------- app/controllers/application_controller.rb | 7 +++ app/models/announcement.rb | 4 +- app/models/component.rb | 8 +++ app/models/item.rb | 34 ++++++++++++ app/models/link.rb | 9 ++++ app/models/page.rb | 42 ++------------- app/models/snippet.rb | 2 + app/views/admin/components/_form.html.erb | 32 +++++++++++ app/views/admin/components/edit.html.erb | 10 ++++ app/views/admin/components/new.html.erb | 11 ++++ app/views/admin/items/_component.html.erb | 12 +++++ app/views/admin/items/_link.html.erb | 12 +++++ app/views/admin/items/_page.html.erb | 12 +++++ app/views/admin/items/_snippets.html.erb | 15 ++++++ app/views/admin/items/index.html.erb | 28 ++++++++++ app/views/admin/layouts/index.html.erb | 6 +-- app/views/admin/links/_form.html.erb | 28 ++++++++++ app/views/admin/links/edit.html.erb | 10 ++++ app/views/admin/links/new.html.erb | 11 ++++ app/views/admin/pages/_form.html.erb | 18 ++----- app/views/admin/pages/edit.html.erb | 7 +-- app/views/admin/pages/index.html.erb | 38 ------------- app/views/admin/pages/new.html.erb | 6 +-- app/views/admin/snippets/_form.html.erb | 3 ++ app/views/admin/snippets/edit.html.erb | 4 +- app/views/admin/snippets/index.html.erb | 18 ------- app/views/admin/snippets/new.html.erb | 4 +- app/views/announcements/show.html.erb | 3 -- app/views/layouts/admin.html.erb | 3 +- config/environment.rb | 6 +++ config/locales/zh_tw.yml | 10 ++-- config/routes.rb | 3 ++ lib/layout_support.rb | 7 +++ 38 files changed, 417 insertions(+), 175 deletions(-) create mode 100644 app/controllers/admin/components_controller.rb create mode 100644 app/controllers/admin/items_controller.rb create mode 100644 app/controllers/admin/links_controller.rb create mode 100644 app/models/component.rb create mode 100644 app/models/item.rb create mode 100644 app/models/link.rb create mode 100644 app/views/admin/components/_form.html.erb create mode 100644 app/views/admin/components/edit.html.erb create mode 100644 app/views/admin/components/new.html.erb create mode 100644 app/views/admin/items/_component.html.erb create mode 100644 app/views/admin/items/_link.html.erb create mode 100644 app/views/admin/items/_page.html.erb create mode 100644 app/views/admin/items/_snippets.html.erb create mode 100644 app/views/admin/items/index.html.erb create mode 100644 app/views/admin/links/_form.html.erb create mode 100644 app/views/admin/links/edit.html.erb create mode 100644 app/views/admin/links/new.html.erb delete mode 100644 app/views/admin/pages/index.html.erb delete mode 100644 app/views/admin/snippets/index.html.erb delete mode 100644 app/views/announcements/show.html.erb create mode 100644 lib/layout_support.rb diff --git a/app/controllers/admin/components_controller.rb b/app/controllers/admin/components_controller.rb new file mode 100644 index 00000000..4cdb5000 --- /dev/null +++ b/app/controllers/admin/components_controller.rb @@ -0,0 +1,53 @@ +class Admin::ComponentsController < ApplicationController + + layout "admin" + before_filter :find_parent_item + + def show + #TODO + end + + def new + @component = Component.new + @component.is_published = true + @component.parent_item_name = @parent_item.name + + @component.engine_name = 'Announcement' # only support Announcement now + end + + def edit + @component = Component.find(params[:id]) + end + + def create + @component = Component.new(params[:component]) + + if @component.save + flash[:notice] = 'Component was successfully created.' + redirect_to admin_items_url( :parent_item_name => @component.parent_item_name ) + else + render :action => "new" + end + end + + def update + @component = Component.find(params[:id]) + + if @component.update_attributes(params[:component]) + flash[:notice] = 'Component was successfully updated.' + redirect_to admin_items_url( :parent_item_name => @component.parent_item_name ) + else + render :action => "edit" + end + end + + def destroy + @component = Component.find(params[:id]) + @component.destroy + + #TODO: destroy engine data + + redirect_to admin_items_url( :parent_item_name => @component.parent_item_name ) + end + +end diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb new file mode 100644 index 00000000..7fe5bc20 --- /dev/null +++ b/app/controllers/admin/items_controller.rb @@ -0,0 +1,17 @@ +class Admin::ItemsController < ApplicationController + + layout "admin" + before_filter :find_parent_item + before_filter :find_snippets, :only => :index + + def index + @items = Item.all( :conditions => { :parent_item_name => @parent_item.name } ) + end + + protected + + def find_snippets + @snippets = Snippet.all( :conditions => { :parent_item_name => @parent_item.name } ) + end + +end diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb new file mode 100644 index 00000000..2757d7bb --- /dev/null +++ b/app/controllers/admin/links_controller.rb @@ -0,0 +1,49 @@ +class Admin::LinksController < ApplicationController + + layout "admin" + before_filter :find_parent_item + + def show + #TODO + end + + def new + @link = Link.new + @link.is_published = true + @link.parent_item_name = @parent_item.name + end + + def edit + @link = Link.find(params[:id]) + end + + def create + @link = Link.new(params[:link]) + + if @link.save + flash[:notice] = 'Link was successfully created.' + redirect_to admin_items_url( :parent_item_name => @link.parent_item_name ) + else + render :action => "new" + end + end + + def update + @link = Link.find(params[:id]) + + if @link.update_attributes(params[:link]) + flash[:notice] = 'Link was successfully updated.' + redirect_to admin_items_url( :parent_item_name => @link.parent_item_name ) + else + render :action => "edit" + end + end + + def destroy + @link = Link.find(params[:id]) + @link.destroy + + redirect_to admin_items_url( :parent_item_name => @link.parent_item_name ) + end + +end diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 5ac939c6..f83fd92c 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -1,21 +1,16 @@ class Admin::PagesController < ApplicationController layout "admin" + before_filter :find_parent_item - def index - @pages = Page.all( :conditions => { :parent_page_name => params[:parent] || "root" } ) - end - def show - @page = Page.find(params[:id]) - - redirect_to "/#{@page.name}" + #TODO end def new @page = Page.new @page.is_published = true - @page.parent_page_name = params[:parent_page_name] + @page.parent_item_name = @parent_item.name end def edit @@ -27,7 +22,7 @@ class Admin::PagesController < ApplicationController if @page.save flash[:notice] = 'Page was successfully created.' - redirect_to admin_pages_url( :parent_page_name => @page.parent_page_name ) + redirect_to admin_items_url( :parent_item_name => @page.parent_item_name ) else render :action => "new" end @@ -38,7 +33,7 @@ class Admin::PagesController < ApplicationController if @page.update_attributes(params[:page]) flash[:notice] = 'Page was successfully updated.' - redirect_to admin_pages_url + redirect_to admin_items_url( :parent_item_name => @page.parent_item_name ) else render :action => "edit" end @@ -48,7 +43,7 @@ class Admin::PagesController < ApplicationController @page = Page.find(params[:id]) @page.destroy - redirect_to admin_pages_url + redirect_to admin_items_url( :parent_item_name => @page.parent_item_name ) end end diff --git a/app/controllers/admin/snippets_controller.rb b/app/controllers/admin/snippets_controller.rb index 026be61e..0929eca3 100644 --- a/app/controllers/admin/snippets_controller.rb +++ b/app/controllers/admin/snippets_controller.rb @@ -1,36 +1,15 @@ class Admin::SnippetsController < ApplicationController layout "admin" + before_filter :find_parent_item - def index - @snippets = Snippet.all - end - def show - @snippet = Snippet.find(params[:id]) - - redirect_to "/#{@snippet.name}" + #TODO end def new @snippet = Snippet.new - - if params[:menu] - @snippet.name = "#{params[:menu]}_nav" - lis = Page.find(:all, :conditions => { :parent_page_id => params[:menu], :is_published => true }, :order => "position" ).map do |page| - if page.external_link.blank? - "
  • #{page.name}
  • \n" - else - "
  • #{page.name}
  • \n" - end - end - - VALID_LOCALES.each do |locale| - @snippet.send( :write_attribute, "content_#{locale}", "" ) - end - - end - + @snippet.parent_item_name = @parent_item.name end def edit @@ -42,7 +21,7 @@ class Admin::SnippetsController < ApplicationController if @snippet.save flash[:notice] = 'Snippet was successfully created.' - redirect_to admin_snippets_url + redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name ) else render :action => "new" end @@ -53,7 +32,7 @@ class Admin::SnippetsController < ApplicationController if @snippet.update_attributes(params[:snippet]) flash[:notice] = 'Snippet was successfully updated.' - redirect_to admin_snippets_url + redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name ) else render :action => "edit" end @@ -63,7 +42,7 @@ class Admin::SnippetsController < ApplicationController @snippet = Snippet.find(params[:id]) @snippet.destroy - redirect_to admin_snippets_url + redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name ) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 34867abd..c5f01694 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -31,6 +31,13 @@ class ApplicationController < ActionController::Base I18n.locale = session[:locale] || I18n.default_locale end + def find_parent_item + @parent_item = Item.find_by_name(params[:parent_item_name] || 'root') + unless @parent_item + @parent_item = Page.create( :name => 'root', :title => t(:homepage), :layout_name => "root" ) + end + end + def require_entry_name render :text => 'missing entry_name' if params[:entry_name].blank? return diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 48808246..e708ce98 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -2,12 +2,12 @@ class Announcement include MongoMapper::Document - key :entry_name, String, :required => true, :index => true + key :component_name, String, :required => true, :index => true key_i18n :title, String key_i18n :content, String def to_liquid - { "entry_name" => self.entry_name, "id" => self.id.to_s, "title" => self.title, "content" => self.content } + { "component_name" => self.entry_name, "id" => self.id.to_s, "title" => self.title, "content" => self.content } end end \ No newline at end of file diff --git a/app/models/component.rb b/app/models/component.rb new file mode 100644 index 00000000..19418096 --- /dev/null +++ b/app/models/component.rb @@ -0,0 +1,8 @@ +class Component < Item + + include LayoutSupport + + key :engine_name, String + key :layout_name, String, :required => true + +end \ No newline at end of file diff --git a/app/models/item.rb b/app/models/item.rb new file mode 100644 index 00000000..2051e607 --- /dev/null +++ b/app/models/item.rb @@ -0,0 +1,34 @@ +class Item + + include MongoMapper::Document + + key :_type, String + + key :name, String, :required => true, :index => true + key :parent_item_name, String, :required => true, :index => true + + key_i18n :title, String, :required => true + + key :position, Integer, :required => true + key :is_published, Boolean, :required => true, :default => true, :index => true + + before_validation :setup_default_value + + def self.find_by_name(item_name) + Item.find(:first, :conditions => { :name => item_name, :is_published => true }) + end + + protected + + def setup_default_value + if self.position.blank? + max_page = Page.find(:last, :order => 'position') + self.position = (max_page)? max_page.position.to_i + 1 : 1 + end + + if self.parent_item_name.blank? + self.parent_item_name = "root" + end + end + +end \ No newline at end of file diff --git a/app/models/link.rb b/app/models/link.rb new file mode 100644 index 00000000..a7fd9676 --- /dev/null +++ b/app/models/link.rb @@ -0,0 +1,9 @@ +class Link < Item + + key :url, String, :required => true + + def link + ApplicationController.helpers.link_to(self.title, self.url) + end + +end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index eb01a554..fe1cbf51 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,40 +1,8 @@ -class Page - include MongoMapper::Document +class Page < Item + + include LayoutSupport - key :name, String, :required => true, :index => true - key :parent_page_name, String, :required => true, :index => true - - key_i18n :title, String, :required => true - key_i18n :content, String - - key :layout_name, String, :required => true - key :external_link, String - key :position, Integer, :required => true - key :is_published, Boolean, :required => true, :default => true, :index => true - - key :component_name, String - - before_validation :setup_default_value - - def self.find_by_name(page_name) - Page.find(:first, :conditions => { :name => page_name, :is_published => true }) - end - - def layout - Layout.find_by_name(self.layout_name) - end - - protected - - def setup_default_value - if self.position.blank? - max_page = Page.find(:last, :order => 'position') - self.position = (max_page)? max_page.position.to_i + 1 : 1 - end - - if self.parent_page_name.blank? - self.parent_page_name = "root" - end - end + key_i18n :content, String + key :layout_name, String, :required => true end \ No newline at end of file diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 68b3c648..f1046528 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -3,6 +3,8 @@ class Snippet include MongoMapper::Document key :name, String, :required => true, :index => true + key :parent_item_name, String, :required => true, :index => true key_i18n :content, String + end \ No newline at end of file diff --git a/app/views/admin/components/_form.html.erb b/app/views/admin/components/_form.html.erb new file mode 100644 index 00000000..71788848 --- /dev/null +++ b/app/views/admin/components/_form.html.erb @@ -0,0 +1,32 @@ +<%= f.error_messages %> +<%= f.hidden_field :parent_item_name %> + +

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

    + +

    +<%= f.label :title, "Title en" %> +<%= f.text_field :title_en, :class => 'text' %> +

    + +

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

    + +

    +<%= f.label :title, "Title zh_tw" %> +<%= f.text_field :title_zh_tw, :class => 'text' %> +

    + +

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

    + +

    +<%= f.label :engine_name, "Choose Engine" %> +<%= f.text_field :engine_name %> +

    \ No newline at end of file diff --git a/app/views/admin/components/edit.html.erb b/app/views/admin/components/edit.html.erb new file mode 100644 index 00000000..6d1e73b1 --- /dev/null +++ b/app/views/admin/components/edit.html.erb @@ -0,0 +1,10 @@ +

    Editing pages

    + +<% form_for @component, :url => admin_component_path(@component) do |f| %> + + <%= render :partial => "form", :locals => { :f => f } %> + +

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

    +<% end %> \ No newline at end of file diff --git a/app/views/admin/components/new.html.erb b/app/views/admin/components/new.html.erb new file mode 100644 index 00000000..237d22fc --- /dev/null +++ b/app/views/admin/components/new.html.erb @@ -0,0 +1,11 @@ +

    <%= t(:new_component, :scope => :admin) %>

    + +<% form_for @component, :url => admin_components_path do |f| %> + + <%= render :partial => "form", :locals => { :f => f } %> + +

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

    + +<% end %> \ No newline at end of file diff --git a/app/views/admin/items/_component.html.erb b/app/views/admin/items/_component.html.erb new file mode 100644 index 00000000..143debc3 --- /dev/null +++ b/app/views/admin/items/_component.html.erb @@ -0,0 +1,12 @@ + + <%= item.class %> + <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%=h item.title %> + <%= item.position %> + <%= item.is_published.to_s %> + + <%= link_to t(:show), admin_component_path(item.name) %> | + <%= link_to t(:edit), edit_admin_component_path(item) %> | + <%= link_to t(:delete), admin_component_path(item), :confirm => 'Are you sure?', :method => :delete %> + + \ No newline at end of file diff --git a/app/views/admin/items/_link.html.erb b/app/views/admin/items/_link.html.erb new file mode 100644 index 00000000..7ac8f619 --- /dev/null +++ b/app/views/admin/items/_link.html.erb @@ -0,0 +1,12 @@ + + <%= item.class %> + <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%=h item.title %> + <%= item.position %> + <%= item.is_published.to_s %> + + <%= link_to t(:show), admin_link_path(item.name) %> | + <%= link_to t(:edit), edit_admin_link_path(item) %> | + <%= link_to t(:delete), admin_link_path(item), :confirm => 'Are you sure?', :method => :delete %> + + \ No newline at end of file diff --git a/app/views/admin/items/_page.html.erb b/app/views/admin/items/_page.html.erb new file mode 100644 index 00000000..8a782a88 --- /dev/null +++ b/app/views/admin/items/_page.html.erb @@ -0,0 +1,12 @@ + + <%= item.class %> + <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%=h item.title %> + <%= item.position %> + <%= item.is_published.to_s %> + + <%= link_to t(:show), admin_page_path(item.name) %> | + <%= link_to t(:edit), edit_admin_page_path(item) %> | + <%= link_to t(:delete), admin_page_path(item), :confirm => 'Are you sure?', :method => :delete %> + + \ No newline at end of file diff --git a/app/views/admin/items/_snippets.html.erb b/app/views/admin/items/_snippets.html.erb new file mode 100644 index 00000000..ef0237f4 --- /dev/null +++ b/app/views/admin/items/_snippets.html.erb @@ -0,0 +1,15 @@ +

    Listing snippets

    + + + + + +<% snippets.each do |snippet| %> + + + + + +<% end %> +
    <%= snippet.name %><%= link_to t(:show), admin_snippet_path(snippet) %> + <%= link_to t(:edit), edit_admin_snippet_path(snippet) %><%= link_to t(:delete), admin_snippet_path(snippet), :confirm => 'Are you sure?', :method => :delete %>
    \ No newline at end of file diff --git a/app/views/admin/items/index.html.erb b/app/views/admin/items/index.html.erb new file mode 100644 index 00000000..fa3c651b --- /dev/null +++ b/app/views/admin/items/index.html.erb @@ -0,0 +1,28 @@ +<% content_for :secondary do %> + +<% end -%> + +

    Listing items: <%= @parent_item.name %>/

    + + + + + + + + + + + +<% @items.each do |item| %> + <%= render :partial => item.class.to_s.downcase, :locals => { :item => item } %> +<% end %> +
    ClassNameTitlePositionPublished?Action
    + + +<%= render :partial => "snippets", :locals => { :snippets => @snippets } %> diff --git a/app/views/admin/layouts/index.html.erb b/app/views/admin/layouts/index.html.erb index 3f3d9e9c..64939353 100644 --- a/app/views/admin/layouts/index.html.erb +++ b/app/views/admin/layouts/index.html.erb @@ -7,12 +7,12 @@ <% @layouts.each do |layout| %> <%= layout.name %> - <%= link_to 'Edit', edit_admin_layout_path(layout) %> - <%= link_to 'Destroy', admin_layout_path(layout), :confirm => 'Are you sure?', :method => :delete %> + <%= link_to t(:edit), edit_admin_layout_path(layout) %> + <%= link_to t(:delete), admin_layout_path(layout), :confirm => 'Are you sure?', :method => :delete %> <% end %>
    -<%= link_to 'New layouts', new_admin_layout_path, :class => 'button positive' %> \ No newline at end of file +<%= link_to t(:new_layout, :scope => :admin), new_admin_layout_path, :class => 'button positive' %> \ No newline at end of file diff --git a/app/views/admin/links/_form.html.erb b/app/views/admin/links/_form.html.erb new file mode 100644 index 00000000..b81d2526 --- /dev/null +++ b/app/views/admin/links/_form.html.erb @@ -0,0 +1,28 @@ +<%= f.error_messages %> +<%= f.hidden_field :parent_item_name %> + +

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

    + +

    +<%= f.label :title, "Title en" %> +<%= f.text_field :title_en, :class => 'text' %> +

    + + +

    +<%= f.label :title, "Title zh_tw" %> +<%= f.text_field :title_zh_tw, :class => 'text' %> +

    + +

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

    + +

    +<%= f.label :url, "URL" %> +<%= f.text_field :url %> +

    \ No newline at end of file diff --git a/app/views/admin/links/edit.html.erb b/app/views/admin/links/edit.html.erb new file mode 100644 index 00000000..09823e95 --- /dev/null +++ b/app/views/admin/links/edit.html.erb @@ -0,0 +1,10 @@ +

    Editing pages

    + +<% form_for @link, :url => admin_link_path(@link) do |f| %> + + <%= render :partial => "form", :locals => { :f => f } %> + +

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

    +<% end %> \ No newline at end of file diff --git a/app/views/admin/links/new.html.erb b/app/views/admin/links/new.html.erb new file mode 100644 index 00000000..780d75e1 --- /dev/null +++ b/app/views/admin/links/new.html.erb @@ -0,0 +1,11 @@ +

    <%= t(:new_link, :scope => :admin) %>

    + +<% form_for @link, :url => admin_links_path do |f| %> + + <%= render :partial => "form", :locals => { :f => f } %> + +

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

    + +<% 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 index b02b387b..1605dd9c 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -1,3 +1,6 @@ +<%= f.error_messages %> +<%= f.hidden_field :parent_item_name %> +

    <%= f.label :name, "Name" %> <%= f.text_field :name, :class => 'text' %> @@ -14,11 +17,6 @@ <%= f.text_field :title_zh_tw, :class => 'text' %>

    -

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

    -

    <%= f.label :layout_name, "Layout Name" %> <%= f.text_field :layout_name, :class => 'text' %> @@ -40,16 +38,6 @@ <%= f.label :is_published, "Is Published" %> <%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No

    - -

    -<%= f.label :component_name, "Component Name" %> -<%= f.text_field :component_name %> -

    - -

    -<%= f.label :external_link, "External Link" %> -<%= f.text_field :external_link %> -

    <% content_for :page_specific_javascript do %>