forked from ryanb/railscasts-episodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
108 changed files
with
9,697 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Railscasts Episode #170: OpenID with Authlogic | ||
|
||
http://railscasts.com/episodes/170 | ||
|
||
Commands | ||
|
||
sudo gem install ruby-openid authlogic-oid | ||
script/plugin install git://github.com/rails/open_id_authentication.git | ||
rake open_id_authentication:db:create | ||
script/generate migration add_openid_identifier_to_users openid_identifier:string | ||
rake db:migrate |
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 | 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Railscasts Example Blog 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 | 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' |
26 changes: 26 additions & 0 deletions
26
episode-170/blog/app/controllers/application_controller.rb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 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 | ||
protect_from_forgery | ||
|
||
# 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 | ||
|
||
helper_method :current_user | ||
|
||
private | ||
|
||
def current_user_session | ||
return @current_user_session if defined?(@current_user_session) | ||
@current_user_session = UserSession.find | ||
end | ||
|
||
def current_user | ||
return @current_user if defined?(@current_user) | ||
@current_user = current_user_session && current_user_session.record | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class ArticlesController < ApplicationController | ||
def index | ||
@articles = Article.find(:all) | ||
end | ||
|
||
def show | ||
@article = Article.find(params[:id]) | ||
@comment = Comment.new(:article => @article) | ||
end | ||
|
||
def new | ||
@article = Article.new | ||
end | ||
|
||
def create | ||
@article = Article.new(params[:article]) | ||
if @article.save | ||
flash[:notice] = "Successfully created article." | ||
redirect_to @article | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
|
||
def edit | ||
@article = Article.find(params[:id]) | ||
end | ||
|
||
def update | ||
@article = Article.find(params[:id]) | ||
if @article.update_attributes(params[:article]) | ||
flash[:notice] = "Successfully updated article." | ||
redirect_to @article | ||
else | ||
render :action => 'edit' | ||
end | ||
end | ||
|
||
def destroy | ||
@article = Article.find(params[:id]) | ||
@article.destroy | ||
flash[:notice] = "Successfully destroyed article." | ||
redirect_to articles_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class CommentsController < ApplicationController | ||
def new | ||
@comment = Comment.new | ||
end | ||
|
||
def create | ||
@comment = Comment.new(params[:comment]) | ||
if @comment.save | ||
flash[:notice] = "Successfully created comment." | ||
redirect_to article_url(@comment.article_id) | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
|
||
def edit | ||
@comment = Comment.find(params[:id]) | ||
end | ||
|
||
def update | ||
@comment = Comment.find(params[:id]) | ||
if @comment.update_attributes(params[:comment]) | ||
flash[:notice] = "Successfully updated comment." | ||
redirect_to article_url(@comment.article_id) | ||
else | ||
render :action => 'edit' | ||
end | ||
end | ||
|
||
def destroy | ||
@comment = Comment.find(params[:id]) | ||
@comment.destroy | ||
flash[:notice] = "Successfully destroyed comment." | ||
redirect_to article_url(@comment.article_id) | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
episode-170/blog/app/controllers/user_sessions_controller.rb
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class UserSessionsController < ApplicationController | ||
def new | ||
@user_session = UserSession.new | ||
end | ||
|
||
def create | ||
@user_session = UserSession.new(params[:user_session]) | ||
@user_session.save do |result| | ||
if result | ||
flash[:notice] = "Successfully logged in." | ||
redirect_to root_url | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@user_session = UserSession.find | ||
@user_session.destroy | ||
flash[:notice] = "Successfully logged out." | ||
redirect_to root_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class UsersController < ApplicationController | ||
def new | ||
@user = User.new | ||
end | ||
|
||
def create | ||
@user = User.new(params[:user]) | ||
@user.save do |result| | ||
if result | ||
flash[:notice] = "Registration successful." | ||
redirect_to root_url | ||
else | ||
render :action => 'new' | ||
end | ||
end | ||
end | ||
|
||
def edit | ||
@user = current_user | ||
end | ||
|
||
def update | ||
@user = current_user | ||
@user.attributes = params[:user] | ||
@user.save do |result| | ||
if result | ||
flash[:notice] = "Successfully updated profile." | ||
redirect_to root_url | ||
else | ||
render :action => 'edit' | ||
end | ||
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 | 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ArticlesHelper | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module CommentsHelper | ||
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 | 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UserSessionsHelper | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Article < ActiveRecord::Base | ||
has_many :comments, :dependent => :destroy | ||
validates_presence_of :name, :content | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Comment < ActiveRecord::Base | ||
belongs_to :article | ||
validates_presence_of :author_name, :content | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class User < ActiveRecord::Base | ||
acts_as_authentic do |c| | ||
c.openid_required_fields = [:nickname, :email] | ||
end | ||
|
||
private | ||
|
||
def map_openid_registration(registration) | ||
self.email = registration["email"] if email.blank? | ||
self.username = registration["nickname"] if username.blank? | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class UserSession < Authlogic::Session::Base | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<%= error_messages_for :article %> | ||
<% form_for @article do |f| %> | ||
<p> | ||
<%= f.label :name %><br /> | ||
<%= f.text_field :name %> | ||
</p> | ||
<p> | ||
<%= f.label :content %><br /> | ||
<%= f.text_area :content %> | ||
</p> | ||
<p> | ||
<%= f.label :author_name %><br /> | ||
<%= f.text_field :author_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% title "Edit Article" %> | ||
|
||
<%= render :partial => 'form' %> | ||
|
||
<p> | ||
<%= link_to "Show", @article %> | | ||
<%= link_to "View All", articles_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<% title "Articles" %> | ||
|
||
<div id="articles"> | ||
<% for article in @articles %> | ||
<h2> | ||
<%= link_to h(article.name), article %> | ||
<span class="comments">(<%= pluralize(article.comments.size, 'comment') %>)</span> | ||
</h2> | ||
<div class="author">from <%=h article.author_name %> on <%= article.created_at.strftime('%b %d, %Y') %></div> | ||
<div class="content"><%= simple_format(article.content) %></div> | ||
<% end %> | ||
</div> |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% title "New Article" %> | ||
|
||
<%= render :partial => 'form' %> | ||
|
||
<p><%= link_to "Back to List", articles_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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<% title @article.name %> | ||
|
||
<p class="author"><em>from <%=h @article.author_name %></em></p> | ||
|
||
<%=simple_format @article.content %> | ||
|
||
<p><%= link_to "Back to Articles", articles_path %></p> | ||
|
||
<% unless @article.comments.empty? %> | ||
<h2><%= pluralize(@article.comments.size, 'comment') %></h2> | ||
|
||
<div id="comments"> | ||
<% for comment in @article.comments %> | ||
<div class="comment"> | ||
<strong><%= link_to_unless comment.site_url.blank?, h(comment.author_name), h(comment.site_url) %></strong> | ||
<em>on <%= comment.created_at.strftime('%b %d, %Y at %H:%M') %></em> | ||
<%=simple_format comment.content %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
|
||
<h3>Add your comment:</h3> | ||
<%= render :partial => 'comments/form' %> |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<%= error_messages_for :comment %> | ||
<% form_for @comment do |f| %> | ||
<%= f.hidden_field :article_id %> | ||
<p> | ||
<%= f.label :author_name, 'Name' %><br /> | ||
<%= f.text_field :author_name %> | ||
</p> | ||
<p> | ||
<%= f.label :site_url, 'Website URL' %><br /> | ||
<%= f.text_field :site_url %> | ||
</p> | ||
<p> | ||
<%= f.label :content, 'Comment' %><br /> | ||
<%= f.text_area :content, :rows => '12', :cols => 35 %> | ||
</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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% title "Edit Comment" %> | ||
|
||
<%= render :partial => 'form' %> | ||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% title "New Comment" %> | ||
|
||
<%= render :partial => 'form' %> | ||
|
Oops, something went wrong.