diff options
| author | mo khan <mo@mokhan.ca> | 2016-05-03 12:26:37 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2016-05-03 12:26:37 -0600 |
| commit | c9b35df01922631815d79afb6bf496d40dfd932f (patch) | |
| tree | 1888dae00b621447571fa97740750afdfa8b7c74 | |
| parent | 882a106a9730778fa9bdf3e634097a674ced47e2 (diff) | |
allow adding new gyms by searching the yelp api.
| -rw-r--r-- | app/controllers/gyms_controller.rb | 15 | ||||
| -rw-r--r-- | app/helpers/application_helper.rb | 8 | ||||
| -rw-r--r-- | app/models/gym.rb | 7 | ||||
| -rw-r--r-- | app/views/gyms/_index.html.erb | 17 | ||||
| -rw-r--r-- | app/views/gyms/new.html.erb | 8 | ||||
| -rw-r--r-- | config/locales/en.yml | 1 | ||||
| -rw-r--r-- | spec/controllers/gyms_controller_spec.rb | 19 |
7 files changed, 62 insertions, 13 deletions
diff --git a/app/controllers/gyms_controller.rb b/app/controllers/gyms_controller.rb index 53e3d4c..eca211b 100644 --- a/app/controllers/gyms_controller.rb +++ b/app/controllers/gyms_controller.rb @@ -3,11 +3,16 @@ class GymsController < ApplicationController before_action only: [:index] { @remote_search = true } def index - @gyms = Gym. - includes(:location). - search_with(params). - closest_to(current_session.location). - order(:name) + if params[:source] == "yelp" + city = current_session.location.try(:city) + @gyms = Gym.search_yelp(term: params[:q], city: city) + else + @gyms = Gym. + includes(:location). + search_with(params). + closest_to(current_session.location). + order(:name) + end end def new diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 05d4c44..3159bf1 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,8 +7,12 @@ module ApplicationHelper class: "gravatar" end - def search_form(search_path: @search_path || dashboard_path, remote: @remote_search) - form_tag search_path, id: "search-form", method: :get, remote: remote do + def search_form( + id: 'search-form', + path: @search_path || dashboard_path, + remote: @remote_search + ) + form_tag path, id: id, method: :get, remote: remote do search_field_tag :q, params[:q], placeholder: t(:search) end end diff --git a/app/models/gym.rb b/app/models/gym.rb index bfcd769..263972e 100644 --- a/app/models/gym.rb +++ b/app/models/gym.rb @@ -29,8 +29,9 @@ class Gym < ActiveRecord::Base end end - def self.search_yelp(term: 'gym', city: "Calgary", categories: ['gyms'], page: 1, page_size: 20) + 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, { category_filter: categories.join(','), limit: page_size, @@ -51,4 +52,8 @@ class Gym < ActiveRecord::Base ) end end + + def full_address + "#{location.try(:address)}, #{location.try(:city)}, #{location.try(:region)}, #{location.try(:country)}" + end end diff --git a/app/views/gyms/_index.html.erb b/app/views/gyms/_index.html.erb index 4f12503..54ae70e 100644 --- a/app/views/gyms/_index.html.erb +++ b/app/views/gyms/_index.html.erb @@ -2,12 +2,27 @@ <% @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><%= gym.full_address %></td> <td> <%= link_to gym.location.try(:url) do %> <i class="fa fa-map-marker" aria-hidden="true"></i> <% end %> </td> + <% if !gym.persisted? %> + <td> + <%= form_for(gym) do |form| %> + <%= form.hidden_field :name %> + <%= form.fields_for :location do |location_form| %> + <%= location_form.hidden_field :address %> + <%= location_form.hidden_field :city %> + <%= location_form.hidden_field :region %> + <%= location_form.hidden_field :country %> + <%= location_form.hidden_field :postal_code %> + <% end %> + <%= form.submit t(:save), class: "button tiny" %> + <% end %> + </td> + <% end %> </tr> <% end %> </table> diff --git a/app/views/gyms/new.html.erb b/app/views/gyms/new.html.erb index 79e8d0f..450bcc3 100644 --- a/app/views/gyms/new.html.erb +++ b/app/views/gyms/new.html.erb @@ -3,7 +3,13 @@ <% end %> <div class="row"> - <div class="large-12 columns"> + <div class="large-6 columns"> + <h1><%= t(:search) %></h1> + <%= search_form id: 'yelp-search', path: gyms_path(source: 'yelp'), remote: true %> + <div id="results"></div> + </div> + + <div class="large-6 columns"> <h1><%= t(".title") %></h1> <%= form_for(@gym) do |form| %> <%= form.label :name %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 23ab0cd..249f29f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -20,6 +20,7 @@ # available at http://guides.rubyonrails.org/i18n.html. en: + save: Save search: Search layouts: application: diff --git a/spec/controllers/gyms_controller_spec.rb b/spec/controllers/gyms_controller_spec.rb index cbfe366..77dcd15 100644 --- a/spec/controllers/gyms_controller_spec.rb +++ b/spec/controllers/gyms_controller_spec.rb @@ -2,14 +2,16 @@ require 'rails_helper' describe GymsController do let(:user) { create(:user) } + let(:user_session) { create(:user_session, location: portland, user: user) } + let(:portland) { create(:portland) } before :each do - http_login(user) + http_login(user, user_session) end describe "#index" do - let!(:sait) { create(:gym, name: 'sait') } - let!(:world_health) { create(:gym, name: 'world health') } + let!(:sait) { create(:gym, name: 'sait', location: create(:portland)) } + let!(:world_health) { create(:gym, name: 'world health', location: create(:portland)) } it 'returns a list of gyms' do get :index @@ -24,6 +26,17 @@ describe GymsController do expect(assigns(:gyms)).to match_array([sait]) expect(response).to be_ok end + + 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]) + get :index, q: 'sait', source: 'yelp' + + expect(assigns(:gyms)).to match_array([yelp_gym]) + expect(response).to be_ok + end end describe "#new" do |
