Commit eaf2f8a
Changed files (7)
app
db
spec
models
app/controllers/gyms_controller.rb
@@ -35,6 +35,7 @@ class GymsController < ApplicationController
def secure_params
params.require(:gym).permit(
:name,
+ :yelp_id,
location_attributes: [:address, :city, :region, :country, :postal_code]
)
end
app/models/gym.rb
@@ -46,6 +46,7 @@ class Gym < ActiveRecord::Base
Search.yelp(q, categories, city, page, per_page) do |result|
Gym.new(
name: result.name,
+ yelp_id: result.id,
location_attributes: {
address: result.location.address.first,
city: result.location.city,
@@ -60,6 +61,7 @@ class Gym < ActiveRecord::Base
end
def duplicate?(distance: 0.1)
+ return true if yelp_id.present? && Gym.where.not(id: id).exists?(yelp_id: yelp_id)
Gym.
closest_to(location, distance: distance).
where.not(id: id).
app/views/gyms/_index.html.erb
@@ -8,7 +8,7 @@
<tbody>
<% @gyms.each do |gym| %>
<tr>
- <td><%= gym.name %></td>
+ <td><%= gym.new_record? ? gym.name : link_to(gym.name, gym_path(gym)) %></td>
<td>
<%= gym.full_address %>
<%= link_to gym.location.try(:url) do %>
@@ -19,6 +19,7 @@
<% if gym.new_record? && !gym.duplicate? %>
<%= form_for(gym) do |form| %>
<%= form.hidden_field :name %>
+ <%= form.hidden_field :yelp_id %>
<%= form.fields_for :location do |location_form| %>
<%= location_form.hidden_field :address %>
<%= location_form.hidden_field :city %>
app/views/gyms/show.html.erb
@@ -1,4 +1,3 @@
-
<div class="row">
<div class="large-12columns">
<%= react_component('Comment', { author: 'mo', body: 'nice', rank: 5 }) %>
db/migrate/20160515023751_add_yelp_id_to_gyms.rb
@@ -0,0 +1,6 @@
+class AddYelpIdToGyms < ActiveRecord::Migration
+ def change
+ add_column :gyms, :yelp_id, :string
+ add_index :gyms, :yelp_id
+ end
+end
db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160514150039) do
+ActiveRecord::Schema.define(version: 20160515023751) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -45,8 +45,11 @@ ActiveRecord::Schema.define(version: 20160514150039) do
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "yelp_id"
end
+ add_index "gyms", ["yelp_id"], name: "index_gyms_on_yelp_id", using: :btree
+
create_table "locations", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
t.uuid "locatable_id"
t.string "locatable_type"
spec/models/gym_spec.rb
@@ -129,7 +129,16 @@ describe Gym do
expect(subject.duplicate?).to be_truthy
end
+ it 'returns true when another gym has the same yelp id' do
+ subject.yelp_id = "hello-world"
+ subject.save!
+ other = create(:gym, yelp_id: subject.yelp_id)
+
+ expect(subject.duplicate?).to be_truthy
+ end
+
it "returns false when no dups are found" do
+ subject.yelp_id = "hello-world"
subject.location = create(:portland)
subject.save!
expect(subject.duplicate?).to be_falsey