Commit 440bb11

mo khan <mo@mokhan.ca>
2016-05-03 19:53:47
allow for multiple filters when searching for a gym to add.
1 parent c9b35df
Changed files (8)
app/controllers/application_controller.rb
@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
   before_action :authenticate!
   rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
-  helper_method :current_user, :feature_available?
+  helper_method :current_user, :current_session, :feature_available?
 
   protected
 
app/controllers/gyms_controller.rb
@@ -3,9 +3,8 @@ class GymsController < ApplicationController
   before_action only: [:index] { @remote_search = true }
 
   def index
-    if params[:source] == "yelp"
-      city = current_session.location.try(:city)
-      @gyms = Gym.search_yelp(term: params[:q], city: city)
+    if 'yelp' == params[:source]
+      @gyms = Gym.search_with(params)
     else
       @gyms = Gym.
         includes(:location).
@@ -24,7 +23,7 @@ class GymsController < ApplicationController
   def create
     @gym = Gym.new(secure_params)
     if @gym.save
-      redirect_to gyms_path
+      redirect_to gyms_path(q: @gym.name)
     else
       flash[:error] = @gym.errors.full_messages
       render :new
app/models/gym.rb
@@ -23,21 +23,31 @@ class Gym < ActiveRecord::Base
 
   scope :search_with, ->(params) do
     if params[:q].present?
-      search(params[:q])
+      if "yelp" == params[:source]
+        search_yelp(
+          q: params[:q],
+          categories: params[:categories],
+          city: params[:city],
+          page: (params[:page] || 1).to_i,
+          per_page: (params[:per_page] || 20).to_i
+        )
+      else
+        search(params[:q])
+      end
     else
       all
     end
   end
 
-  def self.search_yelp(term: 'gym', city: , categories: ['gyms'], page: 1, page_size: 20)
-    offset = (page * page_size) - page_size
-    city = city.present? ? city : "Calgary"
-    Yelp.client.search(city, {
+  def self.search_yelp(q: 'gym', categories: ['gyms'], city: "Calgary", page: 1, per_page: 20)
+    city = city.present? ? city : 'Calgary'
+    results = Yelp.client.search(city, {
       category_filter: categories.join(','),
-      limit: page_size,
-      offset: offset,
-      term: term,
-    }).businesses.map do |result|
+      limit: per_page,
+      offset: (page * per_page) - per_page,
+      term: q,
+    })
+    results.businesses.map do |result|
       Gym.new(
         name: result.name,
         location_attributes: {
app/views/gyms/new.html.erb
@@ -5,7 +5,15 @@
 <div class="row">
   <div class="large-6 columns">
     <h1><%= t(:search) %></h1>
-    <%= search_form id: 'yelp-search', path: gyms_path(source: 'yelp'), remote: true %>
+    <%= form_tag gyms_path(source: 'yelp'), method: :get, remote: true do %>
+      <%= hidden_field_tag :city, current_session.location.try(:city) %>
+      <% ["gyms", "stadiumsarenas"].each do |category| %>
+        <%= hidden_field_tag 'categories[]', category %>
+      <% end %>
+      <%= hidden_field_tag :per_page, 10 %>
+      <%= hidden_field_tag :source, 'yelp' %>
+      <%= search_field_tag :q, params[:q], placeholder: t(:search) %>
+    <% end %>
     <div id="results"></div>
   </div>
 
@@ -26,7 +34,7 @@
         <%= location_form.label :postal_code %>
         <%= location_form.text_field :postal_code %>
       <% end %>
-      <%= form.submit %>
+      <%= form.submit t(:save), class: 'button' %>
     <% end %>
   </div>
 </div>
bin/yelp
@@ -7,6 +7,6 @@ cities = ['Calgary', 'Edmonton', 'Portland', 'Victoria', 'Anaheim', 'San Diego',
 cities.each do |city|
   (1..5).each do |page|
     puts "Searching #{city}, page: #{page}"
-    Gym.search_yelp(city: city, page: page).each(&:save!)
+    Gym.search_yelp(q: 'gym', city: city, page: page).each(&:save!)
   end
 end
spec/controllers/gyms_controller_spec.rb
@@ -29,9 +29,7 @@ describe GymsController do
 
     it 'returns matches from yelp' do
       yelp_gym = double
-      allow(Gym).to receive(:search_yelp).
-        with(term: 'sait', city: portland.city).
-        and_return([yelp_gym])
+      allow(Gym).to receive(:search_yelp).and_return([yelp_gym])
       get :index, q: 'sait', source: 'yelp'
 
       expect(assigns(:gyms)).to match_array([yelp_gym])
@@ -70,7 +68,7 @@ describe GymsController do
       end
 
       it 'redirects to the listing page' do
-        expect(response).to redirect_to(gyms_path)
+        expect(response).to redirect_to(gyms_path(q: 'SAIT'))
       end
 
       it 'creates a new gym' do
spec/models/gym_spec.rb
@@ -98,7 +98,7 @@ describe Gym do
 
     it 'finds a college gym' do
       expect(Gym.search_yelp(
-        term: 'SAIT',
+        q: 'SAIT',
         city: "Calgary",
         categories: ["gyms", "stadiumsarenas"]
       ).map(&:name)).to match_array(["Sait Campus Centre"])
spec/support/pages/new_gym_page.rb
@@ -16,7 +16,7 @@ class NewGymPage < PageModel
       fill_in "gym_location_attributes_region", with: region
       select country, from: "gym_location_attributes_country"
       fill_in "gym_location_attributes_postal_code", with: postal_code
-      click_button "Create Gym"
+      click_button "Save"
     end
   end
 end