Commit 2d99c23

mo khan <mo@mokhan.ca>
2016-05-07 13:49:45
hound happy.
1 parent bacc7f4
app/controllers/gyms_controller.rb
@@ -12,7 +12,9 @@ class GymsController < ApplicationController
   def new
     @gym = Gym.new
     @gym.build_location
-    @countries = Carmen::Country.all.sort_by(&:name).map { |x| [x.name, x.code] }
+    @countries = Carmen::Country.all.sort_by(&:name).map do |x|
+      [x.name, x.code]
+    end
   end
 
   def create
app/controllers/sessions_controller.rb
@@ -1,6 +1,9 @@
 class SessionsController < PublicController
   def create
-    if user_session = User.login(params[:user][:username], params[:user][:password])
+    if user_session = User.login(
+        params[:user][:username],
+        params[:user][:password]
+    )
       session[:user_id] = user_session.access(request)
       redirect_to dashboard_path
     else
app/models/gym.rb
@@ -15,10 +15,12 @@ class Gym < ActiveRecord::Base
   end
 
   scope :search, ->(query) do
-    sql = "UPPER(gyms.name) LIKE :query" +
-      " OR UPPER(locations.city) LIKE :query" +
-      " OR UPPER(locations.region) LIKE :query" +
-      " OR UPPER(locations.country) LIKE :query"
+    sql = [
+      "UPPER(gyms.name) LIKE :query",
+      "OR UPPER(locations.city) LIKE :query",
+      "OR UPPER(locations.region) LIKE :query",
+      "OR UPPER(locations.country) LIKE :query"
+    ].join(' ')
     joins(:location).where(sql, { query: "%#{query.upcase}%" })
   end
 
app/models/location.rb
@@ -7,7 +7,12 @@ class Location < ActiveRecord::Base
     lng_column_name: :longitude
 
   def full_address
-    "#{try(:address)}, #{try(:city)}, #{try(:region)}, #{try(:country)}"
+    [
+      try(:address),
+      try(:city),
+      try(:region),
+      try(:country)
+    ].join(", ")
   end
 
   def coordinates
@@ -46,7 +51,8 @@ class Location < ActiveRecord::Base
   private
 
   def assign_coordinates
-    return if self.latitude.present? || self.longitude.present?
-    self.latitude, self.longitude = Location.from(address, city, region, country)
+    return if latitude.present? || longitude.present?
+    self.latitude, self.longitude =
+      Location.from(address, city, region, country)
   end
 end
spec/controllers/gyms_controller_spec.rb
@@ -10,8 +10,8 @@ describe GymsController do
   end
 
   describe "#index" do
-    let!(:sait) { create(:gym, name: "sait", location: create(:portland)) }
-    let!(:world_health) { create(:gym, name: "world health", location: create(:portland)) }
+    let!(:sait) { create(:portland_gym, name: "sait") }
+    let!(:world_health) { create(:portland_gym, name: "world health") }
 
     it "returns a list of gyms" do
       get :index
spec/features/gyms_spec.rb
@@ -9,8 +9,12 @@ feature "Gyms", type: :feature do
 
   feature "viewing gyms" do
     subject { GymsPage.new }
-    let!(:calgary_gym) { create(:gym, name: "sait", location: create(:calgary)) }
-    let!(:edmonton_gym) { create(:gym, name: "nait", location: create(:edmonton)) }
+    let!(:calgary_gym) do
+      create(:gym, name: "sait", location: create(:calgary))
+    end
+    let!(:edmonton_gym) do
+      create(:gym, name: "nait", location: create(:edmonton))
+    end
 
     it "loads the gyms closest to you" do
       subject.visit_page
@@ -28,7 +32,9 @@ feature "Gyms", type: :feature do
     end
 
     describe "search" do
-      let!(:other_calgary_gym) { create(:gym, name: "world health", location: create(:calgary)) }
+      let!(:other_calgary_gym) do 
+        create(:gym, name: "world health", location: create(:calgary))
+      end
 
       it "returns gyms that match the search criteria", js: true do
         subject.visit_page
spec/models/gym_spec.rb
@@ -51,9 +51,9 @@ describe Gym do
   end
 
   describe ".search_with" do
-    let!(:calgary_gym) { create(:gym, name: "SAIT", location: create(:calgary)) }
-    let!(:edmonton_gym) { create(:gym, name: "NAIT", location: create(:edmonton)) }
-    let!(:portland_gym) { create(:gym, name: "24 Hour Fitness", location: create(:portland)) }
+    let!(:calgary_gym) { create(:calgary_gym, name: "SAIT") }
+    let!(:edmonton_gym) { create(:edmonton_gym, name: "NAIT") }
+    let!(:portland_gym) { create(:portland_gym, name: "24 Hour Fitness") }
 
     it "returns all gyms" do
       results = Gym.search_with({})
@@ -100,7 +100,7 @@ describe Gym do
       expect(Gym.search_yelp(
         q: "SAIT",
         city: "Calgary",
-        categories: ["gyms", "stadiumsarenas"]
+        categories: %w{gyms stadiumsarenas},
       ).map(&:name)).to match_array(["Sait Campus Centre"])
     end
   end
@@ -110,10 +110,12 @@ describe Gym do
 
     it "returns the full address" do
       subject.location = location
-      expected = "#{location.address}, " +
-        "#{location.city}, " +
-        "#{location.region}, " +
-        "#{location.country}"
+      expected = [
+        location.address,
+        location.city,
+        location.region,
+        location.country
+      ].join(", ")
       expect(subject.full_address).to eql(expected)
     end
   end
spec/models/location_spec.rb
@@ -52,8 +52,9 @@ describe Location do
 
   describe "#coordinates" do
     it "returns the lat/long" do
-      subject.latitude, subject.longitude = rand(90.0), rand(180.0)
-      expect(subject.coordinates).to eql([
+      subject.latitude = rand(90.0)
+      subject.longitude = rand(180.0)
+      expect(subject.coordinates).to match_array([
         subject.latitude,
         subject.longitude
       ])
spec/support/http_authentication.rb
@@ -1,5 +1,5 @@
 module HttpAuthentication
-  def http_login(user, user_session = 
+  def http_login(user, user_session =
                  build(:user_session, id: SecureRandom.uuid, user: user))
     allow(controller).to receive(:current_user).and_return(user)
     allow(controller).to receive(:current_session).and_return(user_session)
spec/factories.rb
@@ -56,6 +56,15 @@ FactoryGirl.define do
   factory :gym do
     name { FFaker::Internet.user_name }
     association :location
+    factory :calgary_gym do
+      location { create(:calgary) }
+    end
+    factory :edmonton_gym do
+      location { create(:edmonton) }
+    end
+    factory :portland_gym do
+      location { create(:portland) }
+    end
   end
 
   factory :user_session, class: UserSession do