Skip to content

Instantly share code, notes, and snippets.

@gerhard
Created January 6, 2009 15:33
Show Gist options
  • Select an option

  • Save gerhard/43855 to your computer and use it in GitHub Desktop.

Select an option

Save gerhard/43855 to your computer and use it in GitHub Desktop.
<% @organisations_by_letter.each do |letter| -%>
<li><%= link_to_unless(letter == params[:id], letter, organisation_url(letter)) do |name|
link_to(name, organisation_url(letter), :class => "active")
end %></li>
<% end -%>
class Organisation < ActiveRecord::Base
# provided through utility_scopes gem http://github.com/yfactorial/utility_scopes/tree/master
# Rails 2.2 EDGE has default scopes
ordered_by "name"
# default_limit '1000'
named_scope :letter, lambda { |letter| {:conditions => conditions(letter) }
def self.conditions(letter)
if letter == "Other"
"name REGEXP '^[[:digit:]]|^[[:space:]]|^[[:punct:]]'"
elsif !letter.blank?
["name like ?", letter + "%"]
end
end
end
class OrganisationController< ApplicationController
def index
@organisations_by_letter = ("A".."Z").to_a << "Other" # to generate the letters list
params[:id] = "A" if params[:id].nil? || !@organisations_by_letter.include?(params[:id])
@organisations = Organisation.ordered.letter(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment