Commit 13ea080

mo khan <mo@mokhan.ca>
2016-05-03 03:40:14
add search box and load results via UJS.
1 parent 6f13b84
app/controllers/gyms_controller.rb
@@ -1,4 +1,6 @@
 class GymsController < ApplicationController
+  before_action :provide_search_path
+
   def index
     @gyms = Gym.closest_to(current_session.location).includes(:location)
   end
@@ -6,7 +8,7 @@ class GymsController < ApplicationController
   def new
     @gym = Gym.new
     @gym.build_location
-    @countries = Carmen::Country.all.map { |x| [x.name, x.code] }
+    @countries = Carmen::Country.all.sort_by(&:name).map { |x| [x.name, x.code] }
   end
 
   def create
@@ -27,4 +29,8 @@ class GymsController < ApplicationController
       location_attributes: [:address, :city, :region, :country, :postal_code]
     )
   end
+
+  def provide_search_path
+    @search_path = gyms_path
+  end
 end
app/helpers/application_helper.rb
@@ -3,7 +3,11 @@ module ApplicationHelper
     secure_host = "https://secure.gravatar.com/avatar"
     options = "s=#{size}&d=mm"
     image_tag "#{secure_host}/#{user.gravatar_id}?#{options}",
-              alt: user.username,
-              class: "gravatar"
+      alt: user.username,
+      class: "gravatar"
+  end
+
+  def search_path
+    @search_path || dashboard_path
   end
 end
app/views/gyms/_index.html.erb
@@ -0,0 +1,9 @@
+<table>
+  <% @gyms.each do |gym| %>
+    <tr>
+    <td><%= gym.name %></td>
+    <td><%= gym.location.try(:address) %>, <%= gym.location.try(:city) %>, <%= gym.location.try(:region) %>, <%= gym.location.try(:country) %></td>
+    <td><%= link_to t('.view_map'), gym.location.try(:url) %></td>
+  </tr>
+<% end %>
+</table>
app/views/gyms/index.html.erb
@@ -5,14 +5,8 @@
 <div class="row">
   <div class="large-12 columns">
     <h1><%= t(".title") %></h1>
-    <table>
-      <% @gyms.each do |gym| %>
-        <tr>
-        <td><%= gym.name %></td>
-        <td><%= gym.location.try(:address) %>, <%= gym.location.try(:city) %>, <%= gym.location.try(:region) %>, <%= gym.location.try(:country) %></td>
-        <td><%= link_to t('.view_map'), gym.location.try(:url) %></td>
-      </tr>
-    <% end %>
-    </table>
+    <div id="results">
+      <%= render partial: 'index' %>
+    </div>
   </div>
 </div>
app/views/gyms/index.js.erb
@@ -0,0 +1,1 @@
+$('#results').html('<%= escape_javascript(render partial: "index") %>');
app/views/layouts/application.html.erb
@@ -32,6 +32,11 @@
                 <!-- Right Nav Section -->
                 <ul class="right">
                   <% if feature_available?(:gym) %>
+                  <li>
+                    <%= form_tag search_path, method: :get, remote: true do %>
+                      <%= search_field_tag :q, params[:q], placeholder: t('.search') %>
+                    <% end %>
+                  </li>
                   <li class="divider"></li>
                   <li><%= link_to t(".gyms"), gyms_path %></li>
                   <% end %>
config/locales/en.yml
@@ -30,6 +30,7 @@ en:
           log_out: "Log out"
           profile_edit: "Edit profile"
           profile_view: "View profile"
+      search: Search
       title: Stronglifters
   gyms:
     index: