Commit bcb6845

mo khan <mo@mokhan.ca>
2016-05-28 04:36:51
hack out some ractive code to select a home gym.
1 parent 41de54c
Changed files (7)
app/assets/javascripts/templates/home_gym.ractive
@@ -0,0 +1,29 @@
+  <fieldset>
+    <legend>City</legend>
+    <input type="text" name="city" value='{{city}}' />
+  </fieldset>
+  <fieldset>
+    <legend>Gym</legend>
+    <input type="text" name="q" value='{{gym}}' />
+  </fieldset>
+
+  <button on-click='search' class="button">Search</button>
+
+<div id="results">
+<table>
+  <tbody>
+{{#each gyms}}
+    <tr>
+      <td>{{name}}</td>
+      <td>
+        {{full_address}}
+        <a href={{map_url}}><i class="fa fa-map-marker" aria-hidden=true></i></a>
+      </td>
+      <td>
+        <button on-click="choose" class="button tiny">My Home Gym</button>
+      </td>
+    </tr>
+{{/each}}
+  </tbody>
+</table>
+</div>
app/assets/javascripts/views/home_gym.js.coffee
@@ -0,0 +1,32 @@
+Stronglifters.HomeGym = Ractive.extend
+  template: RactiveTemplates["templates/home_gym"]
+  oninit: ->
+    @set(city: 'Calgary')
+    @set(gyms: [])
+    @on 'search', (event) -> @search()
+    @on 'choose', (event) -> @choose(event.context)
+
+  search: ->
+    $.getJSON @buildUrl(), (data) =>
+      @displayResults(data)
+
+  choose: (gym) ->
+    $('#home_gym_name').html(gym.name)
+    $('#home_gym_yelp_id').val(gym.yelp_id)
+    @closeModal()
+
+  displayResults: (data) ->
+    @set(gyms: data)
+
+  buildUrl: ->
+    params = [
+      "q=#{@get('gym')}",
+      "categories[]=gyms",
+      "categories[]=stadiumsarenas",
+      "city=#{@get('city')}",
+      "source=yelp",
+    ]
+    "/gyms?#{params.join("&")}"
+
+  closeModal: ->
+    $('#homeGymModal').foundation('reveal', 'close')
app/controllers/profiles_controller.rb
@@ -12,7 +12,12 @@ class ProfilesController < ApplicationController
 
   def update
     profile = current_user.profile
-    profile.update_attributes(profile_params)
+    ActiveRecord::Base.transaction do
+      if params[:home_gym_yelp_id].present?
+        profile.home_gym = Gym.create_from_yelp!(params[:home_gym_yelp_id])
+      end
+      profile.update(profile_params)
+    end
     flash[:notice] = t("profiles.edit.profile_update_success")
     redirect_to profile_path(profile)
   end
app/models/gym.rb
@@ -44,22 +44,30 @@ class Gym < ActiveRecord::Base
 
   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,
-        yelp_id: result.id,
-        location_attributes: {
-          address: result.location.address.first,
-          city: result.location.city,
-          postal_code: result.location.postal_code,
-          region: result.location.state_code,
-          country: result.location.country_code,
-          latitude: result.location.coordinate.try(:latitude),
-          longitude: result.location.coordinate.try(:longitude),
-        }
-      )
+      map_from(result)
     end
   end
 
+  def self.map_from(result)
+    Gym.new(
+      name: result.name,
+      yelp_id: result.id,
+      location_attributes: {
+        address: result.location.address.first,
+        city: result.location.city,
+        postal_code: result.location.postal_code,
+        region: result.location.state_code,
+        country: result.location.country_code,
+        latitude: result.location.coordinate.try(:latitude),
+        longitude: result.location.coordinate.try(:longitude),
+      }
+    )
+  end
+
+  def self.create_from_yelp!(id)
+    Gym.find_by(yelp_id: id) || Gym.map_from(::Yelp.client.business(id).business).save!
+  end
+
   def self.import(city, pages: 5)
     return if city.blank?
     return [] if Rails.env.test?
app/models/profile.rb
@@ -2,6 +2,7 @@ class Profile < ActiveRecord::Base
   belongs_to :user
   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
app/views/gyms/index.json.jbuilder
@@ -1,6 +1,7 @@
 json.array! @gyms do |gym|
+  json.yelp_id gym.yelp_id
   json.name gym.name
   json.full_address gym.full_address
-  json.latitude gym.latitude
-  json.longitude gym.longitude
+  json.latitude gym.location.latitude
+  json.longitude gym.location.longitude
 end
app/views/profiles/edit.html.erb
@@ -21,7 +21,26 @@
         <legend><%= Profile.human_attribute_name(:time_zone) %></legend>
         <%= f.time_zone_select(:time_zone, ActiveSupport::TimeZone.us_zones) %>
       </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) %>
+        <a href="#" data-reveal-id="homeGymModal">Choose Home Gym</a>
+      </fieldset>
       <%= f.submit t(".save"), class: "button"  %>
     <% end %>
   </div>
 </div>
+
+<div id="homeGymModal" class="reveal-modal" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
+  <h2 id="modalTitle"><%= t('.title') %></h2>
+  <p class="lead"><%= t('.lead') %></p>
+  <div id="gym-search"></div>
+  <a class="close-reveal-modal" aria-label="Close">&#215;</a>
+</div>
+
+<script type="text/javascript" charset="utf-8">
+    new Stronglifters.HomeGym({
+      el: 'gym-search',
+    })
+</script>