From 4807066c6807c322e55e92369d80dbc9fdce1796 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 1 Jun 2009 10:54:16 +0800 Subject: [PATCH] Add announcement model --- app/controllers/announcements_controller.rb | 85 ++++++ app/controllers/pages_controller.rb | 5 +- app/helpers/announcements_helper.rb | 2 + app/models/announcement.rb | 16 ++ app/models/page.rb | 2 +- app/views/admin/pages/_form.html.erb | 7 +- app/views/announcements/edit.html.erb | 22 ++ app/views/announcements/index.html.erb | 18 ++ app/views/announcements/new.html.erb | 21 ++ app/views/announcements/show.html.erb | 3 + config/routes.rb | 4 + public/index.html | 275 -------------------- 12 files changed, 182 insertions(+), 278 deletions(-) create mode 100644 app/controllers/announcements_controller.rb create mode 100644 app/helpers/announcements_helper.rb create mode 100644 app/models/announcement.rb create mode 100644 app/views/announcements/edit.html.erb create mode 100644 app/views/announcements/index.html.erb create mode 100644 app/views/announcements/new.html.erb create mode 100644 app/views/announcements/show.html.erb delete mode 100644 public/index.html diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb new file mode 100644 index 00000000..ca60969e --- /dev/null +++ b/app/controllers/announcements_controller.rb @@ -0,0 +1,85 @@ +class AnnouncementsController < ApplicationController + # GET /announcements + # GET /announcements.xml + def index + @announcements = Announcement.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @announcements } + end + end + + # GET /announcements/1 + # GET /announcements/1.xml + def show + @announcement = Announcement.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @announcement } + end + end + + # GET /announcements/new + # GET /announcements/new.xml + def new + @announcement = Announcement.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @announcement } + end + end + + # GET /announcements/1/edit + def edit + @announcement = Announcement.find(params[:id]) + end + + # POST /announcements + # POST /announcements.xml + def create + @announcement = Announcement.new(params[:announcement]) + + respond_to do |format| + if @announcement.save + flash[:notice] = 'Announcement was successfully created.' + format.html { redirect_to(@announcement) } + format.xml { render :xml => @announcement, :status => :created, :location => @announcement } + else + format.html { render :action => "new" } + format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /announcements/1 + # PUT /announcements/1.xml + def update + @announcement = Announcement.find(params[:id]) + + respond_to do |format| + if @announcement.update_attributes(params[:announcement]) + flash[:notice] = 'Announcement was successfully updated.' + format.html { redirect_to(@announcement) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /announcements/1 + # DELETE /announcements/1.xml + def destroy + @announcement = Announcement.find(params[:id]) + @announcement.destroy + + respond_to do |format| + format.html { redirect_to(announcements_url) } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 2cc43220..5f25040f 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -5,8 +5,11 @@ class PagesController < ApplicationController def show @page = Page.find(:first, :conditions => { :name => params[:page_name], :is_published => true }) - if @page && @page.external_link + if @page && !@page.external_link.blank? redirect_to @page.external_link + elsif @page && @page.use_engine + #model_class = Kernel.const_get( "Announcement" ) # page.use_engine + redirect_to announcements_path elsif @page @layout = @page.layout @page_content = Liquid::Template.parse(@page.content).render diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb new file mode 100644 index 00000000..beb2d039 --- /dev/null +++ b/app/helpers/announcements_helper.rb @@ -0,0 +1,2 @@ +module AnnouncementsHelper +end diff --git a/app/models/announcement.rb b/app/models/announcement.rb new file mode 100644 index 00000000..492ab883 --- /dev/null +++ b/app/models/announcement.rb @@ -0,0 +1,16 @@ +class Announcement < CouchFoo::Base + + property :title, String + property :content, String + + validates_presence_of :title + + def self.load + # hmm.... but one module will has many pages + end + + def to_liquid + { :title => self.title, :content => self.content } + end + +end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index e29f4bd9..d9457726 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -5,7 +5,7 @@ class Page < CouchFoo::Base property :content, String property :layout_id, String property :layout_name, String - property :load_model, String + property :use_engine, String property :external_link, String property :position, Integer property :is_published, Boolean diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index b2268de8..913fd521 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -21,4 +21,9 @@

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

\ No newline at end of file +

+ +

+<%= f.label :use_engine, "Use Engine" %> +<%= f.text_field :use_engine %> +

\ No newline at end of file diff --git a/app/views/announcements/edit.html.erb b/app/views/announcements/edit.html.erb new file mode 100644 index 00000000..cdaddda1 --- /dev/null +++ b/app/views/announcements/edit.html.erb @@ -0,0 +1,22 @@ +

Editing announcement

+ +<% form_for(@announcement) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :title, "Title" %> + <%= f.text_field :title %> +

+ +

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

+ +

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

+<% end %> + +<%= link_to 'Show', @announcement %> | +<%= link_to 'Back', announcements_path %> \ No newline at end of file diff --git a/app/views/announcements/index.html.erb b/app/views/announcements/index.html.erb new file mode 100644 index 00000000..775c1d8a --- /dev/null +++ b/app/views/announcements/index.html.erb @@ -0,0 +1,18 @@ +

Listing announcements

+ + + + + +<% @announcements.each do |announcement| %> + + + + + +<% end %> +
<%= link_to 'Show', announcement %><%= link_to 'Edit', edit_announcement_path(announcement) %><%= link_to 'Destroy', announcement, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New announcement', new_announcement_path %> \ No newline at end of file diff --git a/app/views/announcements/new.html.erb b/app/views/announcements/new.html.erb new file mode 100644 index 00000000..cbd74850 --- /dev/null +++ b/app/views/announcements/new.html.erb @@ -0,0 +1,21 @@ +

New announcement

+ +<% form_for(@announcement) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :title, "Title" %> + <%= f.text_field :title %> +

+ +

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

+ +

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

+<% end %> + +<%= link_to 'Back', announcements_path %> \ No newline at end of file diff --git a/app/views/announcements/show.html.erb b/app/views/announcements/show.html.erb new file mode 100644 index 00000000..dad3840d --- /dev/null +++ b/app/views/announcements/show.html.erb @@ -0,0 +1,3 @@ + +<%= link_to 'Edit', edit_announcement_path(@announcement) %> | +<%= link_to 'Back', announcements_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 357f5d0f..e52642d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ ActionController::Routing::Routes.draw do |map| + map.resources :announcements + + + map.resources :announcements map.namespace :admin do |admin| admin.resources :pages diff --git a/public/index.html b/public/index.html deleted file mode 100644 index 0dd5189f..00000000 --- a/public/index.html +++ /dev/null @@ -1,275 +0,0 @@ - - - - - Ruby on Rails: Welcome aboard - - - - - - -
- - -
- - - - -
-

Getting started

-

Here’s how to get rolling:

- -
    -
  1. -

    Use script/generate to create your models and controllers

    -

    To see all available options, run it without parameters.

    -
  2. - -
  3. -

    Set up a default route and remove or rename this file

    -

    Routes are set up in config/routes.rb.

    -
  4. - -
  5. -

    Create your database

    -

    Run rake db:migrate to create your database. If you're not using SQLite (the default), edit config/database.yml with your username and password.

    -
  6. -
-
-
- - -
- - \ No newline at end of file