Skip to content

Instantly share code, notes, and snippets.

@farski
Created March 14, 2011 02:40
Show Gist options
  • Save farski/868679 to your computer and use it in GitHub Desktop.
Save farski/868679 to your computer and use it in GitHub Desktop.
def index
@programs = @camp.programs.order('name')
# some of these could be scoped out to make the controller cleaner if the queries are going to show up in other places
# eg
# scope :minage, lambda { |age| where(min_age >= ?, age) }
# @programs = @programs.minage(params[:min_age]) if params[:min_age]
@programs = @programs.where('min_age >= ?', params[:min_age]) if params[:min_age]
@programs = @programs.where('max_age <= ?', params[:max_age]) if params[:max_age]
@programs = @programs.where('min_price >= ?', params[:min_price]) if params[:min_price]
@programs = @programs.where('max_price <= ?', params[:max_price]) if params[:max_price]
@programs = @programs.joins(:sessions).where('sessions.start_date >= ?', params[:start_date].to_date) if params[:start_date]
@programs = @programs.joins(:sessions).where('sessions.end_date <= ?', params[:end_date].to_date) if params[:end_date]
@programs = @programs.send(params[:availablity].to_sym) if ['open', 'closed'].include?(params[:availablity])
if @programs
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @programs }
end
else
respond_to do |format|
format.html { render :action => "index", :status => :not_found }
format.xml { render :xml => @programs.errors, :status => :bad_request }
end
end
end
# you only need lambda in scopes if there's a variable inside the scope
# This is here so we don't repeat ourselves in the open/closed scopes
scope :availablity, joins(:program_populations).group("programs.id")
scope :open, where('program_populations.current_population < program_populations.max_population')
scope :closed, where('program_populations.current_population >= program_populations.max_population')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment