From 34ad7336d68873ca62cb6b4ef5f473e1d893b785 Mon Sep 17 00:00:00 2001 From: Jason LaPier Date: Wed, 23 Nov 2011 16:03:15 -0800 Subject: [PATCH] allow category show page to redirect to a ContentPage --- app/controllers/categories_controller.rb | 8 +++---- app/models/category.rb | 2 ++ app/views/categories/_list.html.erb | 2 +- app/views/categories/edit.html.erb | 4 ++++ app/views/categories/show.html.erb | 21 +++++++++++++++++++ ...direct_to_content_page_id_to_categories.rb | 9 ++++++++ public/stylesheets/application.css | 1 + 7 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20111123232639_add_redirect_to_content_page_id_to_categories.rb diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 4790ebc..279e8d6 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -18,10 +18,8 @@ def index # GET /categories/1.xml def show @category = Category.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @category } + if @category.redirect_to_content_page and params[:no_redirect].blank? + redirect_to @category.redirect_to_content_page end end @@ -55,7 +53,7 @@ def update respond_to do |format| if @category.update_attributes(params[:category]) flash[:notice] = 'Category was successfully updated.' - format.html { redirect_to(@category) } + format.html { redirect_to(category_path(@category, :no_redirect => 1)) } format.xml { head :ok } else format.html { render :action => "edit" } diff --git a/app/models/category.rb b/app/models/category.rb index bf70ac9..7f79723 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -3,6 +3,8 @@ class Category < ActiveRecord::Base has_many :blog_posts, :class_name => 'Blog::Post' belongs_to :parent, :class_name => 'Category' has_many :children, :class_name => 'Category', :foreign_key => 'parent_id' + belongs_to :redirect_to_content_page, :class_name => 'ContentPage', + :foreign_key => 'redirect_to_content_page_id' validates_presence_of :name validates_uniqueness_of :name searchable_by :name diff --git a/app/views/categories/_list.html.erb b/app/views/categories/_list.html.erb index 435f0c9..2ef9f5d 100644 --- a/app/views/categories/_list.html.erb +++ b/app/views/categories/_list.html.erb @@ -2,7 +2,7 @@ <% categories.each do |category| -%>
  • <%= category.name %>
    -
    <%= link_to pluralize(category.content_pages.count, 'page'), category %>
    +
    <%= link_to pluralize(category.content_pages.count, 'page'), category_path(category, :no_redirect => 1) %>
    <%= link_to 'edit', edit_category_path(category) %>
    <%= link_to 'delete', category_path(category), :method => :delete %>
    <% child_cats = @categories.select { |cat| category.id == cat.parent_id } %> diff --git a/app/views/categories/edit.html.erb b/app/views/categories/edit.html.erb index 611288c..0368271 100644 --- a/app/views/categories/edit.html.erb +++ b/app/views/categories/edit.html.erb @@ -5,6 +5,10 @@

    Category: <%= f.text_field :name %>
    Parent: <%= f.select :parent_id, @categories.map {|c| [c.name, c.id]}, :include_blank => true %>
    + instead of showing a boring table when you land on this category page, you can redirect to an actual content page
    + Redirect to: <%= f.select :redirect_to_content_page_id, + ContentPage.order("name").map {|cp| [cp.name, cp.id]}, + :include_blank => true %>
    <%= f.submit 'save category' %>

    <% end %> diff --git a/app/views/categories/show.html.erb b/app/views/categories/show.html.erb index 1fd1f06..1e8bd9a 100644 --- a/app/views/categories/show.html.erb +++ b/app/views/categories/show.html.erb @@ -39,7 +39,28 @@ <%= render :partial => 'blog/posts/lead_in', :collection => @category.blog_posts, :as => :post %> <% end %> + + + <% if is_admin? %> + <% if @category.redirect_to_content_page %> +

    + Note: This category page redirects to: + <%= link_to @category.redirect_to_content_page.name, + @category.redirect_to_content_page %> +

    + <% else %> +

    Want this page to redirect to a real page?

    + <% end %> + <%= form_for(@category) do |f| %> +

    + Redirect to: <%= f.select :redirect_to_content_page_id, + ContentPage.order("name").map {|cp| [cp.name, cp.id]}, + :include_blank => true %> + <%= f.submit 'set' %> +

    + <% end %> +