-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatic import from ryanb/railscasts-episodes
- Loading branch information
0 parents
commit 68d65fb
Showing
97 changed files
with
8,718 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
Railscasts Episode #142: Paypal Notifications | |||
|
|||
http://railscasts.com/episodes/142 | |||
|
|||
Commands | |||
|
|||
script/generate nifty_scaffold payment_notification params:text cart_id:integer status:string transaction_id:string create | |||
|
|||
rake db:migrate | |||
|
|||
curl -d "txn_id=3XC103945N720211C&invoice=923204115&payment_status=Completed" http://localhost:3000/payment_notifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
tmp/* | |||
log/* | |||
*.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
Railscasts Example Store App | |||
-- | |||
|
|||
To setup the app, just run `rake setup`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
# Add your own tasks in files placed in lib/tasks ending in .rake, | |||
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. | |||
|
|||
require(File.join(File.dirname(__FILE__), 'config', 'boot')) | |||
|
|||
require 'rake' | |||
require 'rake/testtask' | |||
require 'rake/rdoctask' | |||
|
|||
require 'tasks/rails' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
# Filters added to this controller apply to all controllers in the application. | |||
# Likewise, all the methods added will be available for all controllers. | |||
|
|||
class ApplicationController < ActionController::Base | |||
helper :all # include all helpers, all the time | |||
|
|||
# See ActionController::RequestForgeryProtection for details | |||
# Uncomment the :secret if you're not using the cookie session store | |||
protect_from_forgery # :secret => '526292d4095d0fd8135a9aa118f5956e' | |||
|
|||
# See ActionController::Base for details | |||
# Uncomment this to filter the contents of submitted sensitive data parameters | |||
# from your application log (in this case, all fields with names like "password"). | |||
# filter_parameter_logging :password | |||
|
|||
def current_cart | |||
if session[:cart_id] | |||
@current_cart ||= Cart.find(session[:cart_id]) | |||
session[:cart_id] = nil if @current_cart.purchased_at | |||
end | |||
if session[:cart_id].nil? | |||
@current_cart = Cart.create! | |||
session[:cart_id] = @current_cart.id | |||
end | |||
@current_cart | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
class CartsController < ApplicationController | |||
def show | |||
@cart = current_cart | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
class CategoriesController < ApplicationController | |||
def index | |||
@categories = Category.find(:all) | |||
end | |||
|
|||
def show | |||
@category = Category.find(params[:id]) | |||
end | |||
|
|||
def new | |||
@category = Category.new | |||
end | |||
|
|||
def create | |||
@category = Category.new(params[:category]) | |||
if @category.save | |||
flash[:notice] = "Successfully created category." | |||
redirect_to @category | |||
else | |||
render :action => 'new' | |||
end | |||
end | |||
|
|||
def edit | |||
@category = Category.find(params[:id]) | |||
end | |||
|
|||
def update | |||
@category = Category.find(params[:id]) | |||
if @category.update_attributes(params[:category]) | |||
flash[:notice] = "Successfully updated category." | |||
redirect_to @category | |||
else | |||
render :action => 'edit' | |||
end | |||
end | |||
|
|||
def destroy | |||
@category = Category.find(params[:id]) | |||
@category.destroy | |||
flash[:notice] = "Successfully destroyed category." | |||
redirect_to categories_url | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
class LineItemsController < ApplicationController | |||
def create | |||
@product = Product.find(params[:product_id]) | |||
@line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price) | |||
flash[:notice] = "Added #{@product.name} to cart." | |||
redirect_to current_cart_url | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
class PaymentNotificationsController < ApplicationController | |||
protect_from_forgery :except => [:create] | |||
|
|||
def create | |||
PaymentNotification.create!(:params => params, :cart_id => params[:invoice], :status => params[:payment_status], :transaction_id => params[:txn_id]) | |||
render :nothing => true | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,44 @@ | |||
class ProductsController < ApplicationController | |||
def index | |||
@products = Product.all(:limit => 10) | |||
end | |||
|
|||
def show | |||
@product = Product.find(params[:id]) | |||
end | |||
|
|||
def new | |||
@product = Product.new | |||
end | |||
|
|||
def create | |||
@product = Product.new(params[:product]) | |||
if @product.save | |||
flash[:notice] = "Successfully created product." | |||
redirect_to @product | |||
else | |||
render :action => 'new' | |||
end | |||
end | |||
|
|||
def edit | |||
@product = Product.find(params[:id]) | |||
end | |||
|
|||
def update | |||
@product = Product.find(params[:id]) | |||
if @product.update_attributes(params[:product]) | |||
flash[:notice] = "Successfully updated product." | |||
redirect_to @product | |||
else | |||
render :action => 'edit' | |||
end | |||
end | |||
|
|||
def destroy | |||
@product = Product.find(params[:id]) | |||
@product.destroy | |||
flash[:notice] = "Successfully destroyed product." | |||
redirect_to products_url | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
# Methods added to this helper will be available to all templates in the application. | |||
module ApplicationHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module CartsHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module CategoriesHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,23 @@ | |||
# These helper methods can be called in your template to set variables to be used in the layout | |||
# This module should be included in all views globally, | |||
# to do so you may need to add this line to your ApplicationController | |||
# helper :layout | |||
module LayoutHelper | |||
def title(page_title, show_title = true) | |||
@content_for_title = page_title.to_s | |||
@show_title = show_title | |||
end | |||
|
|||
def show_title? | |||
@show_title | |||
end | |||
|
|||
def stylesheet(*args) | |||
content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) } | |||
end | |||
|
|||
def javascript(*args) | |||
args = args.map { |arg| arg == :defaults ? arg : arg.to_s } | |||
content_for(:head) { javascript_include_tag(*args) } | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module LineItemsHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module PaymentNotificationsHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module ProductsHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
class Cart < ActiveRecord::Base | |||
has_many :line_items | |||
|
|||
def total_price | |||
# convert to array so it doesn't try to do sum on database directly | |||
line_items.to_a.sum(&:full_price) | |||
end | |||
|
|||
def paypal_url(return_url, notify_url) | |||
values = { | |||
:business => 'seller_1229899173_biz@railscasts.com', | |||
:cmd => '_cart', | |||
:upload => 1, | |||
:return => return_url, | |||
:invoice => id, | |||
:notify_url => notify_url | |||
} | |||
line_items.each_with_index do |item, index| | |||
values.merge!({ | |||
"amount_#{index+1}" => item.unit_price, | |||
"item_name_#{index+1}" => item.product.name, | |||
"item_number_#{index+1}" => item.id, | |||
"quantity_#{index+1}" => item.quantity | |||
}) | |||
end | |||
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
class Category < ActiveRecord::Base | |||
has_many :products | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
class LineItem < ActiveRecord::Base | |||
belongs_to :cart | |||
belongs_to :product | |||
|
|||
def full_price | |||
unit_price * quantity | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
class PaymentNotification < ActiveRecord::Base | |||
belongs_to :cart | |||
serialize :params | |||
after_create :mark_cart_as_purchased | |||
|
|||
private | |||
|
|||
def mark_cart_as_purchased | |||
if status == "Completed" | |||
cart.update_attribute(:purchased_at, Time.now) | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
class Product < ActiveRecord::Base | |||
belongs_to :category | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
<% title "Purchase Successful" %> | |||
|
|||
<p><strong>Thank you for your order!</strong> We will process it within one business day.</p> | |||
<p><%= link_to "Back to Products", products_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
<% title "Shopping Cart" %> | |||
|
|||
<table id="line_items"> | |||
<tr> | |||
<th>Product</th> | |||
<th>Qty</th> | |||
<th class="price">Unit Price</th> | |||
<th class="price">Full Price</th> | |||
</tr> | |||
<% for line_item in @cart.line_items %> | |||
<tr class="<%= cycle :odd, :even %>"> | |||
<td><%=h line_item.product.name %></td> | |||
<td class="qty"><%= line_item.quantity %></td> | |||
<td class="price"><%= number_to_currency(line_item.unit_price) %></td> | |||
<td class="price"><%= number_to_currency(line_item.full_price) %></td> | |||
</tr> | |||
<% end %> | |||
<tr> | |||
<td class="total price" colspan="4"> | |||
Total: <%= number_to_currency @cart.total_price %> | |||
</td> | |||
</tr> | |||
</table> | |||
|
|||
<p> | |||
<%= link_to "Continue Shopping", products_url %> | | |||
<%= link_to "Checkout", @cart.paypal_url(products_url, payment_notifications_url) %> | |||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
<% form_for @category do |f| %> | |||
<p> | |||
<%= f.label :name %><br /> | |||
<%= f.text_field :name %> | |||
</p> | |||
<p><%= f.submit "Submit" %></p> | |||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
<% title "Edit Category" %> | |||
<%= render :partial => 'form' %> | |||
|
|||
<p> | |||
<%= link_to "Show", @category %> | | |||
<%= link_to "View All", categories_path %> | |||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
<% title "Categories" %> | |||
<% for category in @categories %> | |||
<div class="category"> | |||
<h3><%= link_to h(category.name), category %></h3> | |||
<div class="actions"> | |||
<%= link_to "Edit", edit_category_path(category) %> | | |||
<%= link_to "Destroy", category, :confirm => 'Are you sure?', :method => :delete %> | |||
</div> | |||
</div> | |||
<% end %> | |||
|
|||
<p><%= link_to "New Category", new_category_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
<% title "New Category" %> | |||
<%= render :partial => 'form' %> | |||
|
|||
<p><%= link_to "Back to List", categories_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
<% title @category.name %> | |||
|
|||
<ul> | |||
<% for product in @category.products %> | |||
<li><%= link_to h(product.name), product %></li> | |||
<% end %> | |||
</ul> | |||
|
|||
<p> | |||
<%= link_to "Edit", edit_category_path(@category) %> | | |||
<%= link_to "Destroy", @category, :confirm => 'Are you sure?', :method => :delete %> | | |||
<%= link_to "View All", categories_path %> | |||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,22 @@ | |||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |||
<html> | |||
<head> | |||
<title><%= h(yield(:title) || "Untitled") %></title> | |||
<%= stylesheet_link_tag 'application' %> | |||
<%= yield(:head) %> | |||
</head> | |||
<body> | |||
<div id="container"> | |||
<%- flash.each do |name, msg| -%> | |||
<%= content_tag :div, msg, :id => "flash_#{name}" %> | |||
<%- end -%> | |||
<%- if show_title? -%> | |||
<h1><%=h yield(:title) %></h1> | |||
<%- end -%> | |||
<%= yield %> | |||
</div> | |||
</body> | |||
</html> |
Oops, something went wrong.