Skip to content

Instantly share code, notes, and snippets.

@screamingmunch
Last active December 19, 2015 18:49
Show Gist options
  • Select an option

  • Save screamingmunch/6001424 to your computer and use it in GitHub Desktop.

Select an option

Save screamingmunch/6001424 to your computer and use it in GitHub Desktop.
Auth & Auth, User Sign-in/up/out, Sessions & Cookies, Bootstrap, Secure Password, Site navigation

##Project Week Cont'ed:

modeling.rb
Direct link
#http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
class Patient
  belongs_to :primary_care_physician, :class_name => "Physician"
end

class PrimaryCarePhysician < ActiveRecord::Base
  # This is not what you want
end

class Physician < ActiveRecord::Base
  # Physician can be "referred to" as a "primary care physician" by a patient
end

patient.primary_care_physician

# has_many(:association_model, { :through => :listings })
# has_many(:association_model, through: :listings)
# has_many :association_model, through: :listings

class Rental < ActiveRecord::Base

  # belong_to :lender, :class_name => "User"
  belong_to :borrower, :class_name => "User"
  belong_to :listing

  # This... (http://api.rubyonrails.org/classes/Module.html#method-i-delegate)
  delegate :lender, :to => :listing, :allow_nil => true

  # ...is the same as this...
  # def lender
  #   if listing.nil?
  #     nil
  #   else
  #     listing.user
  #   end
  # end

end

rental.borrower #=> <User#...>

# Columns for the reviews table:
#   - id (integer)
#   - name (string)
#   - category (string)
#   - description (text)
#   - image (string)
class Equipment < ActiveRecord::Base
  has_many :listings

  def image_url
    if image
    else
      # return the default path to image based on the category
    end
  end
end

class Listing < ActiveRecord::Base
  belongs_to :user
  belongs_to :equipment
  has_many :blackout_dates
end

class User < ActiveRecord::Base
  has_many :listings
  has_many :equipments, :through => :listing
  has_many :reviews
end

user.listings #=> [<Listing#...>, ...]
user.equipments #=> [<Equipment#...>, ...]

# app/models/review.rb
# Columns for the reviews table:
#   - id (integer)
#   - star_rating (float)
#   - author_id (integer)
#   - user_id (integer)
#   - rental_id (integer)
#   - comments (text)
class Review < ActiveRecord::Base
  belongs_to :author, :class_name => "User"
  belongs_to :user
  belongs_to :rental
end

# app/models/blackout_date.rb
# Columns for the blackout_dates table:
#   - id (integer)
#   - date (date)
#   - listing_id (integer)
#   - reason (string) (e.g., "reserved", "vacation")
class BlackoutDate < ActiveRecord::Base
  belongs_to :listing

  delegate :user, :to => :listing, :allow_nil => true
end

Authentication & Authorization

What do you need to authenticate?

User Sign-In/Up/Out

Sessions & Cookies

session scope (router redirects to session, user)

Bootsrap

Secure Password

password digest

in terminal: rails g migration add_password_digest_to_users

Site Navigation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment