Skip to content

Instantly share code, notes, and snippets.

@diabolo
Created March 11, 2010 10:50
Show Gist options
  • Save diabolo/329036 to your computer and use it in GitHub Desktop.
Save diabolo/329036 to your computer and use it in GitHub Desktop.
class CategoriesController < ApplicationController
before_filter :find_category, :only => [:show]
before_filter :ensure_current_post_url, :only => :show
def show
@products = @category.products.paginate :page => params[:page], :order => Product.pagination_ordering(session[:prefs][:product_sort]) if @category
end
private
def find_category
@category = Category.find(params[:id]) if params[:id]
end
def ensure_current_post_url
redirect_to @category, :status => :moved_permanently unless @category.friendly_id_status.best?
end
end
class Category < ActiveRecord::Base
acts_as_tree :order => "name"
has_many :category_products
has_many :products, :through => :category_products
has_friendly_id :name, :use_slug => true
validates_presence_of :name
validates_uniqueness_of :name, :scope => :parent_id
validate :parent_category_is_not_self
def root?
parent.nil?
end
private
def parent_category_is_not_self
errors.add(:parent_id, "Category can not be it's own parent") if parent_id == id and not id.nil?
end
end
Given there is a category # features/step_definitions/general.rb:48
And it has name set to "Tapes & Adhesives" # features/step_definitions/general.rb:62
When I view the url "/categories/tapes-adhesives.html" # features/step_definitions/general.rb:92
Then I should see the category page for "Tapes & Adhesives" # features/step_definitions/seo.steps.rb:1
Given there is a category # features/step_definitions/general.rb:48
And it has name set to "Tapes Adhesives" # features/step_definitions/general.rb:62
When I view the url "/categories/tapes-adhesives--2.html" # features/step_definitions/general.rb:92
10 redirects to the same URL ("http://www.example.com/categories/tapes-adhesives--2.html") (Webrat::InfiniteRedirectError)
(eval):2:in `visit'
./features/step_definitions/general.rb:93:in `/^I view the url "([^\"]*)"$/'
features/seo/url.feature:35:in `When I view the url "/categories/tapes-adhesives--2.html"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment