diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb new file mode 100644 index 00000000..cb57e653 --- /dev/null +++ b/app/controllers/admin/announcements_controller.rb @@ -0,0 +1,66 @@ +class Admin::AnnouncementsController < ApplicationController + + layout "admin" + + def index + @announcements = Announcement.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @announcements } + end + end + + def new + @announcement = Announcement.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @announcement } + end + end + + def edit + @announcement = Announcement.find(params[:id]) + end + + def create + @announcement = Announcement.new(params[:announcement]) + + respond_to do |format| + if @announcement.save + flash[:notice] = 'Announcement was successfully created.' + format.html { redirect_to admin_announcements_path } + 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 + + 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 admin_announcements_path } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } + end + end + end + + def destroy + @announcement = Announcement.find(params[:id]) + @announcement.destroy + + respond_to do |format| + format.html { redirect_to admin_announcements_path } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index ca60969e..ab09b121 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,85 +1,29 @@ class AnnouncementsController < ApplicationController - # GET /announcements - # GET /announcements.xml + def index @announcements = Announcement.all respond_to do |format| - format.html # index.html.erb + format.html { + @page = Page.find_by_name( 'announcement_index') + @page_options = { "announcements" => @announcements.map{ |a| a.to_liquid } } + render_liquid_page + } 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.html{ + @page = Page.find_by_name( 'announcement_show') + @page_options = { 'announcement' => @announcement.to_liquid } + render_liquid_page + } 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/application_controller.rb b/app/controllers/application_controller.rb index 6635a3f4..5abaca55 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,22 @@ -# Filters added to this controller apply to all controllers in the application. -# Likewise, all the methods added will be available for all controllers. - class ApplicationController < ActionController::Base - helper :all # include all helpers, all the time - protect_from_forgery # See ActionController::RequestForgeryProtection for details - # Scrub sensitive parameters from your log - # filter_parameter_logging :password + helper :all + protect_from_forgery + + filter_parameter_logging :password + + Liquid::Template.register_filter(SnippetFilter) + + def render_liquid_page + if @page + @layout = @page.layout + @page_options ||= {} + @page_content = Liquid::Template.parse(@page.content).render(@page_options) + @layout_content = (@page.layout)? @layout.content : "{{page_content}}" + render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) + else + render :text => '404 Not Found' + end + end + end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 5f25040f..1c20647a 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,22 +1,24 @@ class PagesController < ApplicationController - Liquid::Template.register_filter(SnippetFilter) + def index + @page = Page.find_by_name('home') + if @page + render_liquid_page + else + render :text => 'You need a home page' + end + end def show - @page = Page.find(:first, :conditions => { :name => params[:page_name], :is_published => true }) + @page = Page.find_by_name(params[:page_name]) 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 - @layout_content = (@page.layout)? @layout.content : "{{page_content}}" - render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) else - render :text => '404 not found' + render_liquid_page end end diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 492ab883..b6d6c64b 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -1,16 +1,13 @@ +require 'couch_foo' 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 } + { "id" => self.id, "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 d9457726..f7270cd1 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -21,6 +21,12 @@ class Page < CouchFoo::Base default_sort :position + def self.find_by_name(page_name) + Page.find(:first, :conditions => { :name => page_name, :is_published => true }) + end + + protected + def setup_layout_id if self.layout_name.blank? self.layout_id = nil @@ -28,8 +34,6 @@ class Page < CouchFoo::Base self.layout_id = Layout.find_by_name( self.layout_name ).id end end - - protected def setup_default_value if self.position.blank? diff --git a/app/views/announcements/edit.html.erb b/app/views/admin/announcements/edit.html.erb similarity index 70% rename from app/views/announcements/edit.html.erb rename to app/views/admin/announcements/edit.html.erb index cdaddda1..a5c251ff 100644 --- a/app/views/announcements/edit.html.erb +++ b/app/views/admin/announcements/edit.html.erb @@ -1,6 +1,6 @@

Editing announcement

-<% form_for(@announcement) do |f| %> +<% form_for @announcement, :url => admin_announcement_path(@announcement) do |f| %> <%= f.error_messages %>

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

Listing announcements

+ + + + + + + +<% @announcements.each do |announcement| %> + + + + +<% end %> +
TitleActions
<%=h announcement.title %><%= link_to 'Show', announcement_path(announcement) %> | + <%= link_to 'Edit', edit_admin_announcement_path(announcement) %> | + <%= link_to 'Destroy', admin_announcement_path(announcement), :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New announcement', new_admin_announcement_path %> \ No newline at end of file diff --git a/app/views/announcements/new.html.erb b/app/views/admin/announcements/new.html.erb similarity index 70% rename from app/views/announcements/new.html.erb rename to app/views/admin/announcements/new.html.erb index cbd74850..ffeb56d9 100644 --- a/app/views/announcements/new.html.erb +++ b/app/views/admin/announcements/new.html.erb @@ -1,6 +1,6 @@

New announcement

-<% form_for(@announcement) do |f| %> +<% form_for @announcement, :url => admin_announcements_path do |f| %> <%= f.error_messages %>

@@ -18,4 +18,4 @@

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

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/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index faab01cc..a8ae0280 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -15,9 +15,11 @@
<%= yield %> diff --git a/config/routes.rb b/config/routes.rb index e52642d7..9ad056b2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,4 @@ ActionController::Routing::Routes.draw do |map| - map.resources :announcements - map.resources :announcements @@ -8,6 +6,8 @@ ActionController::Routing::Routes.draw do |map| admin.resources :pages admin.resources :layouts admin.resources :snippets + + admin.resources :announcements end # The priority is based upon order of creation: first created -> highest priority. @@ -42,7 +42,7 @@ ActionController::Routing::Routes.draw do |map| # end # You can have the root of your site routed with map.root -- just remember to delete public/index.html. - # map.root :controller => "welcome" + map.root :controller => "pages" # See how all your routes lay out with "rake routes"