Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scope :activity, -> (activity_id) { where activity_id: activity_id }
- scope :city, -> (city_id) { where city_id: city_id }
- def self.created_between(start_time, end_time)
- where('starts_at >= ? AND ends_at <= ?', start_time, end_time)
- end
- def index
- @activities = Activity.order(:name)
- @cities = City.order(:name)
- @schedules = Schedule.where(nil)
- @schedules = @schedules.activity(params[:activity_id]) if params[:activity_id].present?
- @schedules = @schedules.city(params[:city_id]) if params[:city_id].present?
- @schedules = @schedules.created_between(start_time, end_time) if params[:starts_at].present?
- end
- private
- def start_time
- date = params[:starts_at]
- starting_hour = params[:date][:starting_hour]
- starting_time = [date, starting_hour].join(" ")
- Time.zone.parse(starting_time)
- end
- def end_time
- date = params[:starts_at]
- ending_hour = params[:date][:ending_hour]
- ending_time = [date, ending_hour].join(" ")
- Time.zone.parse(ending_time)
- end
- <%= form_tag schedules_path, method: 'get' do %>
- <%= collection_select nil, :city_id, @cities, :id, :name, include_blank: 'All', selected: params[:city_id] %>
- <%= collection_select nil, :activity_id, @activities, :id, :name, include_blank: 'All', selected: params[:activity_id] %>
- <%= select_tag :starts_at, options_for_select([
- ['Today', Time.zone.today],
- ['Tomorrow', Time.zone.tomorrow],
- [(Time.zone.today + 2).strftime("%d %b"), Time.zone.today + 2],
- [(Time.zone.today + 3).strftime("%d %b"), Time.zone.today + 3],
- [(Time.zone.today + 4).strftime("%d %b"), Time.zone.today + 4],
- [(Time.zone.today + 5).strftime("%d %b"), Time.zone.today + 5],
- [(Time.zone.today + 6).strftime("%d %b"), Time.zone.today + 6]
- ], selected: params[:starts_at]), include_blank: true %>
- <%= select_hour Time.zone.now, field_name: "starting_hour", start_hour: 7, end_hour: 23, ampm: true %>
- <%= select_hour Time.zone.now, field_name: "ending_hour", start_hour: 7, end_hour: 23, ampm: true %>
- <%= submit_tag "Filter" %>
- <% end %>
- undefined method `[]' for nil:NilClass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement