Skip to content

Instantly share code, notes, and snippets.

@parthnaik
Last active August 29, 2015 14:01
Show Gist options
  • Save parthnaik/eda6472421065e8d5cf2 to your computer and use it in GitHub Desktop.
Save parthnaik/eda6472421065e8d5cf2 to your computer and use it in GitHub Desktop.
Phase 2 Assessment
#if using has_secure_password in model
#use password_digest and confirm_password in migration
#otherwise password_hash in migration
#CONTOLLER
get '/' do
# Look in app/views/index.erb
erb :index
end
post '/' do
if User.authenticate(params[:username], params[:password])
@user = User.find_by_username(params[:username])
session[:user_id] = @user.id #creates session
redirect "/user/#{current_user.id}"
else
redirect '/'
end
end
#MODEL
class User < ActiveRecord::Base
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(pass)
@entered_password = pass
@password = Password.create(pass)
self.password_hash = @password
end
def self.authenticate(email, password)
user = User.find_by_email(email)
return user if user && (user.password == password)
nil # either invalid email or wrong password
end
end
#INDEX VIEW
<div class="container">
<div id="sign_in">
<h1>Sign in</h1>
<form method="post" action="/">
<div class="username">
<label>username</label></br>
<input type="text" name="username">
</div>
<div class="password">
<label>password</label></br>
<input type="password" name="password">
</div>
<div class="submit">
<input type="submit" value="Submit" class="submit_button">
</div>
<h2>or</h2>
<div id="create">
<a href="/user/new">Create Account</a>
</div>
</form>
</div>
</div>
#CREATE ACCOUNT VIEW
<div class="container">
<div id="create_account">
<h1>Create account</h1>
<form method="post" action="/create_account">
<div class="username">
<label>username</label></br>
<input type="text" name="user[username]">
</div>
<div class="password"
<label>password</label></br>
<input type="password" name="user[password]">
</div>
<div class="submit">
<input type="submit" value="Create" class="submit_button">
</div>
</form>
</div>
</div>
# HELPER
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end

For a Shirt ...

  1. which user designed it?
  2. how many times has it been sold?
  3. which users have purchased it?

For a Purchase ...

  1. which user made the purchase?
  2. which shirt was purchased?

For a User ...

  1. which shirts did it design?
  2. how many shirts has it purchased?
  3. which shirts has it purchased?
  4. how many times have shirts that it's designed been sold?
  5. which users have bought shirts that it's designed

For a Shirt ...

  1. belongs_to :designer, class_name: 'User' # 3, 6
  2. has_many :sales, class_name: 'Purchase' # 4, 9
  3. has_many :purchasers, through: :salea # 5, 10

For a Purchase ...

  1. belongs_to :purchaser, class_name: 'User' # 1
  2. belongs_to :shirt # 2, 4

For a User ...

  1. has_many :purchases, foreign_key: :purchaser_id # 7
  2. has_many :designed_shirts, class_name: 'Shirt', foreign_key: :designer_id # 3, 6
  3. has_many :purchased_shirts, through: :purchases, source: :shirt # 5, 8
  4. has_many :sales, through: :designed_shirts #9
  5. has_many :clients, through: :sales, source: :purchasers # 10
class Shirt < ActiveRecord::Base
belongs_to :designer, class_name: User # 3, 6
has_many :sales, class_name: Purchase # 2, 4, 9, 10
has_many :purchasers, through: :sales # 1, 5
end
class Purchase < ActiveRecord::Base
belongs_to :shirt # 2, 4, 7, 8
belongs_to :purchaser, class_name: User # 1
end
class User < ActiveRecord::Base
has_many :designed_shirts, class_name: Shirt, foreign_key: :designer_id # 3, 6
has_many :purchases, foreign_key: :purchaser_id
has_many :purchased_shirts, through: :purchases, source: :shirt # 5, 7, 8
has_many :sales, through: :designed_shirts # 9
has_many :clients, through: :sales, source: :purchaser # 10
end

Link to Skeletons:

####Joe's Sinatra Skeleton https://github.com/jbwilmoth/skeletor_sin

##Forms ####Post

    <form method="post" action="#">
      <input type="text" name="#" placeholder="#">
      <input type="email" name="#" placeholder="Email">
      <input type="password" name="#" placeholder="Password">
      <input type="submit" value="#">
    </form>

Post with username and password in labels

   <form method="post" action="#">
     <label>username: <input type="text" name="username" placeholder="Username"></label>
     <label>Password: <input type="password" name="password" placeholder="Password"></label>
     <input type="submit" value="#">
   </form>

####Get

    <form method="get" action="#">
      <input type="text" name="#" placeholder="#">
      <input type="#" name="#" placeholder="#">
      <input type="submit" value="#">
    </form>

####Update (with email edit as example)

    <form method="post" action="/#/edit">
      <input type="hidden" name="_method" value="put">
      <input type="email" name="email" value="<%=####@user.email####%>">
      <input type="submit" value="update">
    </form>

####Delete

    <form method="post" action="####/delete/<%[email protected]%>####">
        <input type="hidden" name="_method" value="delete">
        <input type="submit" value="####">
    </form>

####Error Spans

    <!--Error Spans:-->
    <!--Add inside forms if needed-->
    <span id="email-errors" class="errors"></span>
    <span id="entered_password-errors" class="errors"></span>

Ajax

####With Success Alerts

    $.ajax({
      type: ,
      url: ,
      data:
    })
    .done(function(){
    })
    .fail(function(){
    });

##Tables

<table>
  <tr>
    <th>Name</th>
    <th>Skill</th>
    <th>Other Data</th>
    <th>Other Data</th>
  </tr>
    <%####@users.each do |user|####%>
  <tr>
    <td><%####=user.name####%></td>
    <%end%>
  </tr>
</table>

##OOJS https://github.com/nighthawks-2014/phase-2-mock-assessment/blob/jaimin2663_practice/part-4-oojs/source/zoo.js

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