DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

Belay Board Simple Part 4: Calendar Filtering

Originally the index page showed all the availabilities. A user may want to only view availabilities that they are interested in. The calendar can be filtered by a combination of particular hosts and guests. Futhermore, the calendar can be searched by event title names.

Calendar Filtering for Hosts Users and search by event name

class AvailabilitiesController < ApplicationController
  before_action :set_availability, only: %i[ show edit update destroy ]

  # GET /availabilities or /availabilities.json
  def index
    @selected_host_id = params[:host_id]
    @selected_guest_id = params[:guest_id]
    @event_name_query = params[:event_name]

    @availabilities = Availability.all

    if @selected_host_id.present?
      @availabilities = @availabilities.where(user_id: @selected_host_id)
    end

    if @selected_guest_id.present?
      guest_availabilities = EventRequest.where(user_id: @selected_guest_id, status: 'accepted').pluck(:availability_id)
      @availabilities = @availabilities.where(id: guest_availabilities)
    end

    if @event_name_query.present?
      @availabilities = @availabilities.where('event_name ILIKE ?', "%#{@event_name_query}%")
    end
  end
Enter fullscreen mode Exit fullscreen mode

Collapsible search form.

<div style="text-align: center;">

    <h1>Climb Times</h1>

    <%= link_to '#', class: 'btn btn-primary', data: { bs_toggle: 'collapse', bs_target: '#filterForm' } do %>
      <i class="fas fa-search"></i>
    <% end %>

    <div id="filterForm" class="collapse">
      <%= form_with(url: availabilities_path, method: :get, local: true) do |form| %>
  <div class="form-group">
    <%= form.label :host_id, "Host" %>
    <%= form.collection_select :host_id, [['All Hosts', nil]] + User.all.map { |u| [u.username, u.id] }, :last, :first, 
        selected: @selected_host_id, include_blank: false, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label :guest_id, "Guest" %>
    <%= form.collection_select :guest_id, [['All Guests', nil]] + User.all.map { |u| [u.username, u.id] }, :last, :first, 
        selected: @selected_guest_id, include_blank: false, class: 'form-control' %>
  </div>

        <div class="row justify-content-center">
          <div class="col-md-2">
            <div class="form-group">

              <%= form.text_field :event_name, value: @event_name_query, 
            class: 'form-control', placeholder: ' Search Event Names' %>
            </div>
          </div>
        </div>
        <%= form.submit "Filter", class: 'btn btn-success' %>
      <% end %>
    </div>
  </div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)