THE BASICS

Commands Set #1
==========================================
rails MyMovies
cd MyMovies
script/generate scaffold Movie title:string rating:integer
rake db:migrate
script/server

Code Set #1
==========================================
app/models/movie.rb
------
validates_presence_of :title, :rating
validates_numericality_of :rating, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 5

app/views/movies/edit.html.erb
------
Rating<br />
<%= f.radio_button  :rating, 1 %> <%= f.label :rating_1, "1" %>
<%= f.radio_button  :rating, 2 %> <%= f.label :rating_2, "2" %>
<%= f.radio_button  :rating, 3 %> <%= f.label :rating_3, "3" %>
<%= f.radio_button  :rating, 4 %> <%= f.label :rating_4, "4" %>
<%= f.radio_button  :rating, 5 %> <%= f.label :rating_5, "5" %>








STYLING

Code Set #2
==========================================
app/views/layouts/application.html.erb
------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Movies: <%= controller.action_name %></title>
  <%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<div id="pagewrap">

<%= yield %>

</div>

</body>
</html>


Code Set #3
==========================================
public/stylesheets/style.css
------
html, body {
  background-image: url(/images/bg_body.jpg);
}

#pagewrap {
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  
  background-color: white;
  margin: 0 auto;
  padding: 10px;
  width: 900px;
}

app/views/layouts/application.html.erb
------
Line 8: <%= stylesheet_link_tag 'scaffold', 'style' %>


Code Set #4
==========================================
app/views/movies/index.html.erb
<td><div class="star-<%=h movie.rating %> rating"><%=pluralize movie.rating, "Star" %></div></td>

public/stylesheets/style.css
.rating {
  background: url(/images/stars.png) no-repeat;
  height: 26px;
  overflow: hidden;
  text-indent: -1000px;
}
.star-5 { width: 115px; }
.star-4 { width: 92px; }
.star-3 { width: 69px; }
.star-2 { width: 46px; }
.star-1 { width: 23px; }



USERS

Command Set #2
==========================================
script/plugin install http://github.com/technoweenie/restful-authentication.git restful_authentication
script/generate authenticated user sessions --include-activation
rake db:migrate


Code Set #5
==========================================
app/models/user.rb:20
------
has_many  :movies


app/models/movie.rb:6
------
belongs_to  :user


config/routes.rb:8
------
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate'
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'


config/initializers/mail.rb
------
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
	:address => "mail.example-domain.com",
	:port => 25,
	:domain => "www.example-domain.com",
	:authentication => :login,
	:user_name => "user@example-domain.com",
	:password => "secret"
}


config/environments/production.rb:30
------
SITE_URL  = "noahhendrix.com"


config/envrionments/development.rb:19
------
SITE_URL  = "localhost:3000"

app/models/user_mailer.rb:6
------
@body[:url]  = "http://#{SITE_URL}/activate/#{user.activation_code}"


app/models/user_mailer.rb:13
------
@body[:url]  = "http://#{SITE_URL}/"


app/models/user_mailer.rb:19
------
@from        = "admin@noahhendrix.com"


app/models/user_mailer.rb:6
------
@subject     = "[#{SITE_URL}] "


app/views/layouts/application.html.erb:16
------
<p style="color: red"><%= flash[:alert] %></p>


app/controllers/application_controller.rb:5
------
include AuthenticatedSystem


app/controllers/movies_controller.rb
------
8: @movies = current_user.movies
20: @movie  = current_user.movies.find(params[:id])
42: @movie  = current_user.movies.find(params[:id])
49: @movie  = current_user.movies.create(params[:movie])
67: @movie  = current_user.movies.find(params[:id])
85: @movie  = current_user.movies.find(params[:id])


config/routes.rb:46
------
map.root :controller => "movies"


app/views/sessions/new.html.erb:13
-------
<%= link_to 'Sign Up', :signup %>



CLEAN UP

Code Set #6
==========================================
app/views/layouts/application.html.erb
------
<div class="user">
  <% if logged_in? %>
    <%= link_to "Logout", :logout %>
  <% end %>
</div>


public/stylehseets/style.css
-----
.user { text-align: right; }



RESET

Code Set #7
==========================================
app/controllers/users_controller.rb:32
------
def forgot
  if request.post?
    user = User.find_by_email(params[:user][:email])
    
    respond_to do |format|
      if user
        user.create_reset_code
        flash[:notice] = "Reset code sent to #{user.email}"
        
        format.html { redirect_to login_path }
        format.xml { render :xml => user.email, :status => :created }
      else
        flash[:error] = "#{params[:user][:email]} does not exist in system"
        
        format.html { redirect_to login_path }
        format.xml { render :xml => user.email, :status => :unprocessable_entity }
      end
    end
    
  end
end

def reset
  @user = User.find_by_reset_code(params[:reset_code]) unless params[:reset_code].nil?
  if request.post?
    if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation])
      self.current_user = @user
      @user.delete_reset_code
      flash[:notice] = "Password reset successfully for #{@user.email}"
      redirect_to root_url
    else
      render :action => :reset
    end
  end
end


app/models/user.rb:91
------
#reset methods
def create_reset_code
  @reset = true
  self.attributes = {:reset_code => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )}
  save(false)
end

def recently_reset?
  @reset
end

def delete_reset_code
  self.attributes = {:reset_code => nil}
  save(false)
end


app/views/users/forgot.html.erb
------
<%= error_messages_for :user %>
 
<% form_for :user do |f| -%>
  <p>
    Reset Password Request
  </p>
  

  <p>
    <label for="email">Email</label><br />
    <%= f.text_field :email %>
  </p>


  <p>
    <%= link_to "Cancel", login_path %>
    <%= submit_tag 'Submit' %>
  </p>
<% end -%>


app/views/users/reset.html.erb
------
<%= error_messages_for :user %>
 
<% form_for :user do |f| -%>
  <p>
    Pick a new password for <span><%= @user.email %></span>
  </p>

  <p>
    <label for="password">Password</label><br />
    <%= f.password_field :password %>
  </p>
  
  <p>
    <label for="password">Confirm Password</label><br />
    <%= f.password_field :password_confirmation %>
  </p>


  <p>
    <%= submit_tag 'Reset' %>
  </p>
<% end -%>


app/models/user_mailer.rb
------
def reset_notification(user)
  setup_email(user)
  @subject    += 'Link to reset your password'
  @body[:url]  = "#{SITE_URL}/reset/#{user.reset_code}"
end


app/views/user_mailer/reset_notification.html.erb
------
Request to reset password received for <%= @user.login %>

Visit this url to choose a new password:

<%= @url %>
(Your password will remain the same if no action is taken)


config/routes.rb:12
------
map.forgot    '/forgot', :controller => 'users', :action => 'forgot'
map.reset     'reset/:reset_code', :controller => 'users', :action => 'reset'


app/views/sessions/new.html.erb:14
------
<%= link_to 'Forgot Password?', :forgot %>






Command Set #3
==========================================
script/generate migration add_reset_code_to_user reset_code:string 
rake db:migrate