Commit 2eebbd3

mo khan <mo@mokhan.ca>
2016-07-04 20:52:06
hound happy.
1 parent 2bb11c6
app/models/location.rb
@@ -1,5 +1,5 @@
 class Location < ApplicationRecord
-  belongs_to :locatable, polymorphic: true
+  belongs_to :locatable, polymorphic: true, optional: true
   before_save :assign_coordinates
   acts_as_mappable default_units: :kms,
     distance_field_name: :distance,
app/models/profile.rb
@@ -1,6 +1,6 @@
 class Profile < ApplicationRecord
   belongs_to :user
-  belongs_to :gym
+  belongs_to :gym, optional: true
   enum social_tolerance: { low: 0, medium: 1, high: 2 }
   enum gender: { female: 1, male: 2, transgender: 3, other: 0 }
 
app/models/program.rb
@@ -1,7 +1,7 @@
 class Program < ApplicationRecord
   STRONG_LIFTS = "StrongLifts 5×5"
   has_many :exercises, through: :routines
-  has_many :routines
+  has_many :routines, inverse_of: :program
   has_many :recommendations, through: :routines
 
   before_save do
app/models/progress.rb
@@ -19,6 +19,6 @@ class Progress
   end
 
   def status
-    "#{to_sets.join("/")} @ #{max_weight} lbs"
+    "#{to_sets.join('/')} @ #{max_weight} lbs"
   end
 end
app/models/routine.rb
@@ -1,6 +1,6 @@
 class Routine < ApplicationRecord
   belongs_to :program
-  has_many :recommendations
+  has_many :recommendations, inverse_of: :routine
   has_many :exercises, through: :recommendations
 
   def slug
app/models/user.rb
@@ -1,9 +1,9 @@
 class User < ApplicationRecord
   include Flippable
   has_secure_password
-  has_many :workouts
+  has_many :workouts, inverse_of: :user
   has_many :exercise_sets, through: :workouts
-  has_many :user_sessions, dependent: :destroy
+  has_many :user_sessions, dependent: :destroy, inverse_of: :user
   has_one :profile
   has_many :received_emails
   USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
app/models/workout.rb
@@ -3,7 +3,7 @@ class Workout < ApplicationRecord
   belongs_to :routine
   has_one :program, through: :routine
   has_many :exercises, through: :exercise_sets
-  has_many :exercise_sets, dependent: :destroy
+  has_many :exercise_sets, dependent: :destroy, inverse_of: :workout
   accepts_nested_attributes_for :exercise_sets
   delegate :name, to: :routine
   alias_method :sets, :exercise_sets
@@ -27,7 +27,10 @@ class Workout < ApplicationRecord
         target_repetitions: recommendation.repetitions
       )
     end
-    exercise_set.update!(actual_repetitions: repetitions, target_weight: target_weight)
+    exercise_set.update!(
+      actual_repetitions: repetitions,
+      target_weight: target_weight
+    )
     exercise_set
   end
 
config/environments/development.rb
@@ -1,9 +1,9 @@
 Rails.application.configure do
   # Settings specified here will take precedence over those in config/application.rb.
 
-  # In the development environment your application's code is reloaded on
+  # In the development environment your application"s code is reloaded on
   # every request. This slows down response time but is perfect for development
-  # since you don't have to restart the web server when you make code changes.
+  # since you don"t have to restart the web server when you make code changes.
   config.cache_classes = false
 
   # Do not eager load code on boot.
@@ -13,12 +13,12 @@ Rails.application.configure do
   config.consider_all_requests_local = true
 
   # Enable/disable caching. By default caching is disabled.
-  if Rails.root.join('tmp/caching-dev.txt').exist?
+  if Rails.root.join("tmp/caching-dev.txt").exist?
     config.action_controller.perform_caching = true
 
     config.cache_store = :memory_store
     config.public_file_server.headers = {
-      'Cache-Control' => 'public, max-age=172800'
+      "Cache-Control" => "public, max-age=172800"
     }
   else
     config.action_controller.perform_caching = false
@@ -26,7 +26,7 @@ Rails.application.configure do
     config.cache_store = :null_store
   end
 
-  # Don't care if the mailer can't send.
+  # Don"t care if the mailer can"t send.
   config.action_mailer.raise_delivery_errors = false
   config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"
 
config/environments/production.rb
@@ -16,7 +16,7 @@ Rails.application.configure do
 
   # Disable serving static files from the `/public` folder by default since
   # Apache or NGINX already handles this.
-  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
+  config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
 
   # Compress JavaScripts and CSS.
   config.assets.js_compressor = :uglifier
@@ -31,13 +31,13 @@ Rails.application.configure do
   config.action_controller.asset_host = ENV["ASSET_HOST"]
 
   # Specifies the header that your server uses for sending files.
-  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
-  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
+  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
+  # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX
 
   # Mount Action Cable outside main process or domain
   # config.action_cable.mount_path = nil
-  # config.action_cable.url = 'wss://example.com/cable'
-  # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
+  # config.action_cable.url = "wss://example.com/cable"
+  # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ]
 
   # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
   # config.force_ssl = true
@@ -72,8 +72,8 @@ Rails.application.configure do
   config.log_formatter = ::Logger::Formatter.new
 
   # Use a different logger for distributed setups.
-  # require 'syslog/logger'
-  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
+  # require "syslog/logger"
+  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")
 
   if ENV["RAILS_LOG_TO_STDOUT"].present?
     logger           = ActiveSupport::Logger.new(STDOUT)
config/environments/test.rb
@@ -1,10 +1,10 @@
 Rails.application.configure do
   # Settings specified here will take precedence over those in config/application.rb.
 
-  # The test environment is used exclusively to run your application's
+  # The test environment is used exclusively to run your application"s
   # test suite. You never need to work with it otherwise. Remember that
   # your test database is "scratch space" for the test suite and is wiped
-  # and recreated between test runs. Don't rely on the data there!
+  # and recreated between test runs. Don"t rely on the data there!
   config.cache_classes = true
 
   # Do not eager load code on boot. This avoids loading your whole application
@@ -15,7 +15,7 @@ Rails.application.configure do
   # Configure public file server for tests with Cache-Control for performance.
   config.public_file_server.enabled = true
   config.public_file_server.headers = {
-    'Cache-Control' => 'public, max-age=3600'
+    "Cache-Control" => "public, max-age=3600"
   }
 
   # Show full error reports and disable caching.
config/initializers/new_framework_defaults.rb
@@ -15,7 +15,7 @@ Rails.application.config.action_controller.forgery_protection_origin_check = tru
 ActiveSupport.to_time_preserves_timezone = true
 
 # Require `belongs_to` associations by default. Previous versions had false.
-#Rails.application.config.active_record.belongs_to_required_by_default = true
+Rails.application.config.active_record.belongs_to_required_by_default = true
 
 # Do not halt callback chains when a callback returns false. Previous versions had true.
 ActiveSupport.halt_callback_chains_on_return_false = false
config/boot.rb
@@ -1,3 +1,3 @@
-ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
+ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
 
-require 'bundler/setup' # Set up gems listed in the Gemfile.
+require "bundler/setup" # Set up gems listed in the Gemfile.
config/environment.rb
@@ -1,5 +1,5 @@
 # Load the Rails application.
-require_relative 'application'
+require_relative "application"
 
 # Initialize the Rails application.
 Rails.application.initialize!
spec/controllers/profiles_controller_spec.rb
@@ -45,7 +45,9 @@ describe ProfilesController do
 
     describe "#update" do
       it "updates the user profile" do
-        patch :update, params: { id: user.to_param, profile: { gender: "male" } }
+        patch :update, params: {
+          id: user.to_param, profile: { gender: "male" }
+        }
         user.reload
         expect(user.profile.male?).to be_truthy
         expect(response).to redirect_to(profile_path(user.profile))
@@ -54,7 +56,9 @@ describe ProfilesController do
       it 'saves the users home gym' do
         gym = create(:gym)
 
-        patch :update, params: { id: user.to_param, profile: { gym_id: gym.id } }
+        patch :update, params: {
+          id: user.to_param, profile: { gym_id: gym.id }
+        }
 
         expect(user.reload.profile.gym).to eql(gym)
       end
spec/controllers/sessions_controller_spec.rb
@@ -23,29 +23,39 @@ describe SessionsController do
 
     context "when credentials are correct" do
       it "logs you in with email" do
-        post :create, params: { user: { username: user.email, password: "password" } }
+        post :create, params: {
+          user: { username: user.email, password: "password" }
+        }
         expect(session[:user_id]).to eql(UserSession.last.id)
       end
 
       it "logs you in with username" do
-        post :create, params: { user: { username: user.username, password: "password" } }
+        post :create, params: {
+          user: { username: user.username, password: "password" }
+        }
         expect(session[:user_id]).to eql(UserSession.last.id)
       end
 
       it "redirects to the dashboard" do
-        post :create, params: { user: { username: user.username, password: "password" } }
+        post :create, params: {
+          user: { username: user.username, password: "password" }
+        }
         expect(response).to redirect_to(dashboard_path)
       end
     end
 
     context "when credentials are incorrect" do
       it "displays errors" do
-        post :create, params: { user: { username: user.username, password: "wrong" } }
+        post :create, params: {
+          user: { username: user.username, password: "wrong" }
+        }
         expect(flash[:warning]).to_not be_empty
       end
 
       it "redirects to the login page" do
-        post :create, params: { user: { username: user.username, password: "wrong" } }
+        post :create, params: {
+          user: { username: user.username, password: "wrong" }
+        }
         expect(response).to redirect_to(new_session_path)
       end
     end
spec/models/exercise_set_spec.rb
@@ -21,7 +21,7 @@ describe ExerciseSet do
 
     it "returns all sets for the exercise only" do
       squat_set = create(:work_set, exercise: squat)
-      dip_set = create(:work_set, exercise: dip)
+      _ = create(:work_set, exercise: dip)
 
       expect(ExerciseSet.for(squat)).to match_array([squat_set])
     end
spec/factories.rb
@@ -7,7 +7,7 @@ FactoryGirl.define do
     association :workout
     target_repetitions { rand(12) }
     target_weight { rand(400) }
-    factory :work_set, class: 'WorkSet'
+    factory :work_set, class: "WorkSet"
   end
   factory :program do
     name { FFaker::Internet.user_name }