Commit c9b35df
Changed files (7)
app
controllers
helpers
models
views
config
locales
spec
controllers
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
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
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
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>
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 %>
config/locales/en.yml
@@ -20,6 +20,7 @@
# available at http://guides.rubyonrails.org/i18n.html.
en:
+ save: Save
search: Search
layouts:
application:
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