Last active
August 29, 2015 13:56
-
-
Save os6sense/8934371 to your computer and use it in GitHub Desktop.
Controller, params and scopes
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
class ProjectsController < ApplicationController | |
7 | |
8 def index | |
9 @projects = nil | |
10 | |
11 if params[:studio_id] && !params[:studio_id].empty? | |
12 @projects = Project.studio(params[:studio_id]) | |
13 end | |
14 | |
15 # past, present, future | |
16 # It would have been nice to be able to combine these but rails scoping | |
17 # prohibits (cant "or" the scopes) it without more or less custom | |
18 # generating the SQL... | |
19 if (ppf = params[:ppf]) | |
20 case ppf | |
21 when "today_pending" | |
22 @projects = @projects ? @projects.today_pending : Project.today_pending | |
23 when "today" | |
24 @projects = @projects ? @projects.today : Project.today | |
25 when "past" | |
26 @projects = @projects ? @projects.past : Project.past | |
27 when "present" | |
28 @projects = @projects ? @projects.present : Project.present | |
29 when "future" | |
30 @projects = @projects ? @projects.future : Project.future | |
31 end | |
32 end | |
33 | |
34 # If we havent got our result set by now, get everything | |
35 @projects = Project.all if !@projects | |
36 | |
37 # Paginate our output | |
38 @projects = @projects.paginate(:page => params[:page], :per_page => 10) | |
39 | |
40 # and we need to customise our json output hence no _simple_response | |
41 respond_to do |format| | |
42 format.html | |
43 format.json { | |
44 render :json => @projects.to_json(:include => :bookings) | |
45 } | |
46 end | |
47 end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment