Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. scope :activity, -> (activity_id) { where activity_id: activity_id }
  2. scope :city, -> (city_id) { where city_id: city_id }
  3.  
  4. def self.created_between(start_time, end_time)
  5. where('starts_at >= ? AND ends_at <= ?', start_time, end_time)
  6. end
  7.  
  8. def index
  9. @activities = Activity.order(:name)
  10. @cities = City.order(:name)
  11.  
  12. @schedules = Schedule.where(nil)
  13. @schedules = @schedules.activity(params[:activity_id]) if params[:activity_id].present?
  14. @schedules = @schedules.city(params[:city_id]) if params[:city_id].present?
  15. @schedules = @schedules.created_between(start_time, end_time) if params[:starts_at].present?
  16. end
  17.  
  18. private
  19.  
  20. def start_time
  21. date = params[:starts_at]
  22. starting_hour = params[:date][:starting_hour]
  23. starting_time = [date, starting_hour].join(" ")
  24. Time.zone.parse(starting_time)
  25. end
  26.  
  27. def end_time
  28. date = params[:starts_at]
  29. ending_hour = params[:date][:ending_hour]
  30. ending_time = [date, ending_hour].join(" ")
  31. Time.zone.parse(ending_time)
  32. end
  33.  
  34. <%= form_tag schedules_path, method: 'get' do %>
  35.  
  36. <%= collection_select nil, :city_id, @cities, :id, :name, include_blank: 'All', selected: params[:city_id] %>
  37.  
  38. <%= collection_select nil, :activity_id, @activities, :id, :name, include_blank: 'All', selected: params[:activity_id] %>
  39.  
  40. <%= select_tag :starts_at, options_for_select([
  41. ['Today', Time.zone.today],
  42. ['Tomorrow', Time.zone.tomorrow],
  43. [(Time.zone.today + 2).strftime("%d %b"), Time.zone.today + 2],
  44. [(Time.zone.today + 3).strftime("%d %b"), Time.zone.today + 3],
  45. [(Time.zone.today + 4).strftime("%d %b"), Time.zone.today + 4],
  46. [(Time.zone.today + 5).strftime("%d %b"), Time.zone.today + 5],
  47. [(Time.zone.today + 6).strftime("%d %b"), Time.zone.today + 6]
  48. ], selected: params[:starts_at]), include_blank: true %>
  49.  
  50. <%= select_hour Time.zone.now, field_name: "starting_hour", start_hour: 7, end_hour: 23, ampm: true %>
  51.  
  52. <%= select_hour Time.zone.now, field_name: "ending_hour", start_hour: 7, end_hour: 23, ampm: true %>
  53.  
  54. <%= submit_tag "Filter" %>
  55. <% end %>
  56.  
  57. undefined method `[]' for nil:NilClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement