summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2016-05-28 08:38:43 -0600
committermo khan <mo@mokhan.ca>2016-05-28 08:38:43 -0600
commit8be0dbbbafecb2065b2dbbb161dd6f31fe51ceb8 (patch)
tree3911765c72e06fe7401735f009985a8888e293d7
parentcd79878b91a6b2bffde89cf34774b62a0e4dbc62 (diff)
update gyms#create to create a gym using the yelp id.
-rw-r--r--app/controllers/gyms_controller.rb16
-rw-r--r--app/controllers/profiles_controller.rb9
-rw-r--r--app/models/gym.rb2
-rw-r--r--app/models/profile.rb1
-rw-r--r--app/views/gyms/_gym.jbuilder5
-rw-r--r--app/views/gyms/create.json.jbuilder3
-rw-r--r--app/views/gyms/index.json.jbuilder8
-rw-r--r--app/views/profiles/edit.html.erb4
-rw-r--r--spec/controllers/gyms_controller_spec.rb22
-rw-r--r--spec/controllers/profiles_controller_spec.rb5
10 files changed, 53 insertions, 22 deletions
diff --git a/app/controllers/gyms_controller.rb b/app/controllers/gyms_controller.rb
index ddb74d2..b5da8c8 100644
--- a/app/controllers/gyms_controller.rb
+++ b/app/controllers/gyms_controller.rb
@@ -21,9 +21,13 @@ class GymsController < ApplicationController
end
def create
- @gym = Gym.new(secure_params)
+ @gym = build_gym
+
if @gym.save
- redirect_to gyms_path(q: @gym.name)
+ respond_to do |format|
+ format.html { redirect_to gyms_path(q: @gym.name) }
+ format.js { render @gym }
+ end
else
flash[:error] = @gym.errors.full_messages
render :new
@@ -39,4 +43,12 @@ class GymsController < ApplicationController
location_attributes: [:address, :city, :region, :country, :postal_code]
)
end
+
+ def build_gym
+ if params[:yelp_id].present?
+ @gym = Gym.create_from_yelp!(params[:yelp_id])
+ else
+ @gym = Gym.new(secure_params)
+ end
+ end
end
diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index e56a45a..687962d 100644
--- a/app/controllers/profiles_controller.rb
+++ b/app/controllers/profiles_controller.rb
@@ -12,12 +12,7 @@ class ProfilesController < ApplicationController
def update
profile = current_user.profile
- ActiveRecord::Base.transaction do
- if params[:home_gym_yelp_id].present?
- profile.gym = Gym.create_from_yelp!(params[:home_gym_yelp_id])
- end
- profile.update(profile_params)
- end
+ profile.update(profile_params)
flash[:notice] = t("profiles.edit.profile_update_success")
redirect_to profile_path(profile)
end
@@ -25,6 +20,6 @@ class ProfilesController < ApplicationController
private
def profile_params
- params.require(:profile).permit(:gender, :social_tolerance, :time_zone)
+ params.require(:profile).permit(:gender, :social_tolerance, :time_zone, :gym_id)
end
end
diff --git a/app/models/gym.rb b/app/models/gym.rb
index e0e998c..f484e23 100644
--- a/app/models/gym.rb
+++ b/app/models/gym.rb
@@ -65,7 +65,7 @@ class Gym < ActiveRecord::Base
end
def self.create_from_yelp!(id)
- Gym.find_by(yelp_id: id) || Gym.map_from(::Yelp.client.business(id).business).save!
+ Gym.find_by(yelp_id: id) || Gym.map_from(::Yelp.client.business(id).business)
end
def self.import(city, pages: 5)
diff --git a/app/models/profile.rb b/app/models/profile.rb
index ed4681d..ccab7c4 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -3,7 +3,6 @@ class Profile < ActiveRecord::Base
belongs_to :gym
enum social_tolerance: { low: 0, medium: 1, high: 2 }
enum gender: { female: 0, male: 1, transgender: 2, other: nil }
- attr_accessor :home_gym
def to_param
user.username
diff --git a/app/views/gyms/_gym.jbuilder b/app/views/gyms/_gym.jbuilder
new file mode 100644
index 0000000..61f5127
--- /dev/null
+++ b/app/views/gyms/_gym.jbuilder
@@ -0,0 +1,5 @@
+json.yelp_id gym.yelp_id
+json.name gym.name
+json.full_address gym.full_address
+json.latitude gym.location.latitude
+json.longitude gym.location.longitude
diff --git a/app/views/gyms/create.json.jbuilder b/app/views/gyms/create.json.jbuilder
new file mode 100644
index 0000000..7adc17a
--- /dev/null
+++ b/app/views/gyms/create.json.jbuilder
@@ -0,0 +1,3 @@
+json.gym do
+ json.partial! 'gym', gym: @gym
+end
diff --git a/app/views/gyms/index.json.jbuilder b/app/views/gyms/index.json.jbuilder
index ed542d7..c73bfb2 100644
--- a/app/views/gyms/index.json.jbuilder
+++ b/app/views/gyms/index.json.jbuilder
@@ -1,7 +1,3 @@
-json.array! @gyms do |gym|
- json.yelp_id gym.yelp_id
- json.name gym.name
- json.full_address gym.full_address
- json.latitude gym.location.latitude
- json.longitude gym.location.longitude
+json.gyms @gyms do |gym|
+ json.partial! 'gym', gym: gym
end
diff --git a/app/views/profiles/edit.html.erb b/app/views/profiles/edit.html.erb
index 0ec12ac..47eee14 100644
--- a/app/views/profiles/edit.html.erb
+++ b/app/views/profiles/edit.html.erb
@@ -23,8 +23,8 @@
</fieldset>
<fieldset>
<legend><%= Profile.human_attribute_name(:home_gym) %></legend>
- <p id="home_gym_name"><%= @profile.home_gym %></p>
- <%= hidden_field_tag(:home_gym_yelp_id) %>
+ <p id="home_gym_name"><%= @profile.gym.try(:name) %></p>
+ <%= f.hidden_field(:gym_id) %>
<a href="#" data-reveal-id="homeGymModal">Choose Home Gym</a>
</fieldset>
<%= f.submit t(".save"), class: "button" %>
diff --git a/spec/controllers/gyms_controller_spec.rb b/spec/controllers/gyms_controller_spec.rb
index 34df58c..d3cac2f 100644
--- a/spec/controllers/gyms_controller_spec.rb
+++ b/spec/controllers/gyms_controller_spec.rb
@@ -82,6 +82,28 @@ describe GymsController do
expect(gym.location.country).to eql("CA")
expect(gym.location.postal_code).to eql("T2M 0L4")
end
+
+ end
+
+ context "with the yelp id" do
+ render_views
+
+ let(:yelp_id) { 'sait-campus-centre-calgary'}
+ let(:gym) { build(:gym, yelp_id: yelp_id) }
+
+ before :each do
+ allow(Gym).to receive(:create_from_yelp!).
+ with(yelp_id).
+ and_return(gym)
+ end
+
+ it 'returns a json response' do
+ xhr :post, :create, yelp_id: yelp_id
+ json = JSON.parse(response.body)
+ expect(json['yelp_id']).to eql(yelp_id)
+ expect(json['name']).to eql(gym.name)
+ expect(json['full_address']).to eql(gym.full_address)
+ end
end
context "invalid params" do
diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb
index 0c9e57a..bc9d66d 100644
--- a/spec/controllers/profiles_controller_spec.rb
+++ b/spec/controllers/profiles_controller_spec.rb
@@ -52,11 +52,10 @@ describe ProfilesController do
end
it 'saves the users home gym' do
- gym = create(:gym, yelp_id: 'sait-campus-centre-calgary')
+ gym = create(:gym)
patch :update, id: user.to_param,
- profile: { time_zone: 'Alaska' },
- home_gym_yelp_id: gym.yelp_id
+ profile: { time_zone: 'Alaska', gym_id: gym.id }
expect(user.reload.profile.gym).to eql(gym)
end