Skip to content

Instantly share code, notes, and snippets.

@twinge
Created January 25, 2011 02:44
Show Gist options
  • Select an option

  • Save twinge/794427 to your computer and use it in GitHub Desktop.

Select an option

Save twinge/794427 to your computer and use it in GitHub Desktop.
constraints(SoftwareFilter) do
constraints(CategoriesFilter) do
match ':software/:controller/:category1/:category2/:category3/:category4/c(.:format)', :action => :index
match ':software/:controller/:category1/:category2/:category3/:category4/c(/:id(.:format))', :action => :show
match ':software/:controller/:category1/:category2/:category3/:category4/c(/:action(/:id(.:format)))'
match ':software/:controller/:category1/:category2/:category3/c(.:format)', :action => :index
match ':software/:controller/:category1/:category2/:category3/c(/:id(.:format))', :action => :show
match ':software/:controller/:category1/:category2/:category3/c(/:action(/:id(.:format)))'
match ':software/:controller/:category1/:category2/c(.:format)', :action => :index
match ':software/:controller/:category1/:category2/c(/:id(.:format))', :action => :show
match ':software/:controller/:category1/:category2/c(/:action(/:id(.:format)))'
match ':software/:controller/:category1/c(.:format)', :action => :index
match ':software/:controller/:category1/c(/:id(.:format))', :action => :show
match ':software/:controller/:category1/c(/:action(/:id(.:format)))'
end
end
constraints(CategoriesFilter) do
match ':controller/:category1/:category2/:category3/:category4/c(.:format)', :action => :index
match ':controller/:category1/:category2/:category3/:category4/c(/:id(.:format))', :action => :show
match ':controller/:category1/:category2/:category3/:category4/c(/:action(/:id(.:format)))'
match ':controller/:category1/:category2/:category3/c(.:format)', :action => :index
match ':controller/:category1/:category2/:category3/c(/:id(.:format))', :action => :show
match ':controller/:category1/:category2/:category3/c(/:action(/:id(.:format)))'
match ':controller/:category1/:category2/c(.:format)', :action => :index
match ':controller/:category1/:category2/c(/:id(.:format))', :action => :show
match ':controller/:category1/:category2/c(/:action(/:id(.:format)))'
match ':controller/:category1/c(.:format)', :action => :index
match ':controller/:category1/c(/:id(.:format))', :action => :show
match ':controller/:category1/c(/:action(/:id(.:format)))'
end
class CategoriesFilter
def self.matches?(request)
match = (!request.params[:category1] || (request.params[:category1] && level1.include?(request.params[:category1]))) &&
(!request.params[:category2] || (request.params[:category2] && level2.include?(request.params[:category2]))) &&
(!request.params[:category3] || (request.params[:category3] && level3.include?(request.params[:category3]))) &&
(!request.params[:category4] || (request.params[:category4] && level4.include?(request.params[:category4])))
end
def self.level1
@@level1 ||= level1_cats.collect(&:permalink)
end
def self.level2
@@level2 ||= level2_cats.collect(&:permalink)
end
def self.level3
@@level3 ||= level3_cats.collect(&:permalink)
end
def self.level4
@@level4 ||= level4_cats.collect(&:permalink)
end
def self.level1_cats
Category.roots
end
def self.level2_cats
Category.where(:parent_id => level1_cats.collect(&:id))
end
def self.level3_cats
Category.where(:parent_id => level2_cats.collect(&:id))
end
def self.level4_cats
Category.where(:parent_id => level3_cats.collect(&:id))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment