diff options
| author | mo khan <mo@mokhan.ca> | 2016-05-03 13:53:47 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2016-05-03 13:53:47 -0600 |
| commit | 440bb11416e2335cbbb4abdfc2ea60f9c32ed84f (patch) | |
| tree | 18183543df17d01dfb7a9dce4010e870b97ef1cc | |
| parent | c9b35df01922631815d79afb6bf496d40dfd932f (diff) | |
allow for multiple filters when searching for a gym to add.
| -rw-r--r-- | app/controllers/application_controller.rb | 2 | ||||
| -rw-r--r-- | app/controllers/gyms_controller.rb | 7 | ||||
| -rw-r--r-- | app/models/gym.rb | 28 | ||||
| -rw-r--r-- | app/views/gyms/new.html.erb | 12 | ||||
| -rwxr-xr-x | bin/yelp | 2 | ||||
| -rw-r--r-- | spec/controllers/gyms_controller_spec.rb | 6 | ||||
| -rw-r--r-- | spec/models/gym_spec.rb | 2 | ||||
| -rw-r--r-- | spec/support/pages/new_gym_page.rb | 2 |
8 files changed, 38 insertions, 23 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 976e38e..bd923d6 100644 --- a/app/controllers/application_controller.rb +++ b/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 diff --git a/app/controllers/gyms_controller.rb b/app/controllers/gyms_controller.rb index eca211b..5921966 100644 --- a/app/controllers/gyms_controller.rb +++ b/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 diff --git a/app/models/gym.rb b/app/models/gym.rb index 263972e..e087e49 100644 --- a/app/models/gym.rb +++ b/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: { diff --git a/app/views/gyms/new.html.erb b/app/views/gyms/new.html.erb index 450bcc3..a63c8ac 100644 --- a/app/views/gyms/new.html.erb +++ b/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> @@ -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 diff --git a/spec/controllers/gyms_controller_spec.rb b/spec/controllers/gyms_controller_spec.rb index 77dcd15..70e4496 100644 --- a/spec/controllers/gyms_controller_spec.rb +++ b/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 diff --git a/spec/models/gym_spec.rb b/spec/models/gym_spec.rb index 68bf23e..e55de2e 100644 --- a/spec/models/gym_spec.rb +++ b/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"]) diff --git a/spec/support/pages/new_gym_page.rb b/spec/support/pages/new_gym_page.rb index 2a0e90f..ccbbaf6 100644 --- a/spec/support/pages/new_gym_page.rb +++ b/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 |
