Commit a324c6f
Changed files (4)
app
app/controllers/gyms_controller.rb
@@ -3,15 +3,7 @@ class GymsController < ApplicationController
before_action only: [:index] { @remote_search = true }
def index
- if 'yelp' == params[:source]
- @gyms = Gym.search_with(params)
- else
- @gyms = Gym.
- includes(:location).
- search_with(params).
- closest_to(current_session.location).
- order(:name)
- end
+ @gyms = Gym.closest_to(current_session.location).search_with(params)
end
def new
app/models/gym.rb
@@ -32,22 +32,15 @@ class Gym < ActiveRecord::Base
per_page: (params[:per_page] || 20).to_i
)
else
- search(params[:q])
+ includes(:location).search(params[:q]).order(:name)
end
else
- all
+ includes(:location).order(:name)
end
end
- 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: per_page,
- offset: (page * per_page) - per_page,
- term: q,
- })
- results.businesses.map do |result|
+ def self.search_yelp(q: 'gym', categories: ['gyms'], city: , page: 1, per_page: 20)
+ Search.yelp(q, categories, city, page, per_page) do |result|
Gym.new(
name: result.name,
location_attributes: {
app/models/search.rb
@@ -0,0 +1,11 @@
+class Search
+ def self.yelp(q, categories = [], city, page, per_page, &block)
+ city = city.present? ? city : 'Calgary'
+ Yelp.client.search(city, {
+ category_filter: categories.present? ? categories.join(',') : 'all',
+ limit: per_page,
+ offset: (page * per_page) - per_page,
+ term: q,
+ }).businesses.map(&block)
+ end
+end
app/views/gyms/_index.html.erb
@@ -8,7 +8,7 @@
<i class="fa fa-map-marker" aria-hidden="true"></i>
<% end %>
</td>
- <% if !gym.persisted? %>
+ <% if gym.new_record? %>
<td>
<%= form_for(gym) do |form| %>
<%= form.hidden_field :name %>