Skip to content

Commit

Permalink
allow category show page to redirect to a ContentPage
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapier committed Nov 24, 2011
1 parent d335a31 commit 34ad733
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
8 changes: 3 additions & 5 deletions app/controllers/categories_controller.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions app/models/category.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/categories/_list.html.erb
Expand Up @@ -2,7 +2,7 @@
<% categories.each do |category| -%>
<li id="cat_<%= category.id %>">
<div class="category_name"><%= category.name %></div>
<div class="category_pages"><%= link_to pluralize(category.content_pages.count, 'page'), category %></div>
<div class="category_pages"><%= link_to pluralize(category.content_pages.count, 'page'), category_path(category, :no_redirect => 1) %></div>
<div class="category_edit"><%= link_to 'edit', edit_category_path(category) %></div>
<div class="category_del"><%= link_to 'delete', category_path(category), :method => :delete %></div>
<% child_cats = @categories.select { |cat| category.id == cat.parent_id } %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/categories/edit.html.erb
Expand Up @@ -5,6 +5,10 @@
<p>
Category: <%= f.text_field :name %><br/>
Parent: <%= f.select :parent_id, @categories.map {|c| [c.name, c.id]}, :include_blank => true %><br/>
<em>instead of showing a boring table when you land on this category page, you can redirect to an actual content page</em><br/>
Redirect to: <%= f.select :redirect_to_content_page_id,
ContentPage.order("name").map {|cp| [cp.name, cp.id]},
:include_blank => true %><br/>
<%= f.submit 'save category' %>
</p>
<% end %>
21 changes: 21 additions & 0 deletions app/views/categories/show.html.erb
Expand Up @@ -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 %>
<p>
<em>Note:</em> This category page redirects to:
<%= link_to @category.redirect_to_content_page.name,
@category.redirect_to_content_page %>
</p>
<% else %>
<p><em>Want this page to redirect to a real page?</em></p>
<% end %>
<%= form_for(@category) do |f| %>
<p>
Redirect to: <%= f.select :redirect_to_content_page_id,
ContentPage.order("name").map {|cp| [cp.name, cp.id]},
:include_blank => true %>
<%= f.submit 'set' %>
</p>
<% end %>

<p class="admin_links">
<span class="fake_button">
<%= link_to 'List of Categories', categories_path %> |
Expand Down
@@ -0,0 +1,9 @@
class AddRedirectToContentPageIdToCategories < ActiveRecord::Migration
def self.up
add_column :categories, :redirect_to_content_page_id, :integer
end

def self.down
remove_column :categories, :redirect_to_content_page_id
end
end
1 change: 1 addition & 0 deletions public/stylesheets/application.css
Expand Up @@ -302,6 +302,7 @@ ul.events .open:before { content: '- ' }

ul.category_list {
list-style-type: none;
margin-left: 20px;
}

ul.category_list li { }

0 comments on commit 34ad733

Please sign in to comment.