Created
April 16, 2009 05:47
-
-
Save rubysolo/96249 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=================== | |
# routes: | |
map.named_filters ':category_slug/*filters' | |
#=================== | |
# controller | |
before_filter :load_category, :only => 'show' | |
before_filter :apply_filters, :only => 'show' | |
def show | |
@products = @category.products | |
@applied_filters.each do |descriptor, option| | |
# TODO : actual filtering. named_scope, maybe? | |
end | |
# other metadata stuff, blah, blah. | |
end | |
def load_category | |
@category = Category.find_by_slug(params[:category_slug]) | |
end | |
# grab descriptors / options from filters | |
def apply_filters | |
@applied_filters = {} | |
params[:filters].in_groups_of(2) do |(descriptor_slug, option_slug)| | |
descriptor = @category.descriptors.find_by_slug(descriptor_slug) | |
option = descriptor && descriptor.options.find_by_slug(option_slug) | |
@applied_filters[descriptor] = option if option | |
end | |
end | |
#=================== | |
# helper | |
# add a descriptor / option pair to the filterset. if the descriptor is already | |
# active, replaces the active value with this new one. | |
def faceted_link(option, descriptors_to_remove=[]) | |
filters_for_url = @active_filters.dup | |
filters_for_url[option.descriptor] = option | |
# now canonicalize the URL | |
"/#{@category.slug}" + filters_for_url.sort { |a,b| | |
# a and b are each a [descriptor, option] pair. sort by descriptor position | |
a.first.position <=> b.first.position | |
}.reject {|descriptor, option| | |
descriptors_to_remove.include? descriptor | |
}.map {|descriptor, option| "/#{descriptor.slug}/#{option.slug}" }.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment