Initial import

This commit is contained in:
2008-03-02 16:04:34 -03:00
commit 5e4951fa47
798 changed files with 59730 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
# 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
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_rails_root_session_id'
def ensure_logged_in
unless session[:logged_in]
respond_to do |accepts|
accepts.html do
flash[:error] = 'What do you think you\'re doing?'
redirect_to '/'
end
accepts.xml do
headers["Status"] = "Unauthorized"
headers["WWW-Authenticate"] = %(Basic realm="Web Password")
render :text => "Couldn't authenticate you", :status => '401 Unauthorized'
end
end
return false
end
return true
end
end

View File

@@ -0,0 +1,78 @@
class PostsController < ApplicationController
before_filter :ensure_logged_in
before_filter :load_user
def index
@posts = @user.posts
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @posts.to_xml }
end
end
def show
@post = @user.posts.find(params[:id])
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @post.to_xml }
end
end
def new
@post = @user.posts.build
end
def edit
@post = @user.posts.find(params[:id])
end
def create
@post = @user.posts.build(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to post_url(@post.user, @post) }
format.xml { head :created, :location => post_url(@post.user, @post) }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors.to_xml }
end
end
end
def update
@post = @user.posts.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to post_url(@post.user, @post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors.to_xml }
end
end
end
def destroy
@post = @user.posts.find(params[:id])
@post.destroy
flash[:notice] = "Post was removed"
respond_to do |format|
format.html { redirect_to posts_url(@post.user) }
format.xml { head :ok }
end
end
private
def load_user
@user = User.find(params[:user_id])
end
end

View File

@@ -0,0 +1,81 @@
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.find(:all)
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @users.to_xml }
end
end
# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @user.to_xml }
end
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1;edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to user_url(@user) }
format.xml { head :created, :location => user_url(@user) }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors.to_xml }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to user_url(@user) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors.to_xml }
end
end
end
# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:notice] = "User was removed"
respond_to do |format|
format.html { redirect_to users_url }
format.xml { head :ok }
end
end
end

View File

@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end

View File

@@ -0,0 +1,2 @@
module PostsHelper
end

View File

@@ -0,0 +1,2 @@
module UsersHelper
end

View File

@@ -0,0 +1,3 @@
class Dog < ActiveRecord::Base
belongs_to :user, :foreign_key => :owner_id
end

View File

@@ -0,0 +1,11 @@
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :owner, :foreign_key => :user_id, :class_name => 'User'
has_many :taggings
has_many :tags, :through => :taggings
validates_uniqueness_of :title
validates_presence_of :title
validates_presence_of :body, :message => 'Seriously... wtf'
validates_numericality_of :user_id
end

View File

@@ -0,0 +1,4 @@
class Tag < ActiveRecord::Base
has_many :taggings
has_many :posts, :through => :taggings
end

View File

@@ -0,0 +1,4 @@
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end

View File

@@ -0,0 +1,9 @@
class User < ActiveRecord::Base
has_many :posts
has_many :dogs, :foreign_key => :owner_id
attr_protected :password
validates_format_of :email, :with => /\w*@\w*.com/
validates_length_of :email, :in => 1..100
validates_inclusion_of :age, :in => 1..100
end

View File

@@ -0,0 +1,17 @@
<!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>Posts: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>

View File

@@ -0,0 +1,17 @@
<!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>Users: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>

View File

@@ -0,0 +1,27 @@
<h1>Editing post</h1>
<%= error_messages_for :post %>
<% form_for(:post, :url => post_path(@post.user, @post), :html => { :method => :put }) do |f| %>
<p>
<b>User</b><br />
<%= f.text_field :user_id %>
</p>
<p>
<b>Title</b><br />
<%= f.text_field :title %>
</p>
<p>
<b>Body</b><br />
<%= f.text_area :body %>
</p>
<p>
<%= submit_tag "Update" %>
</p>
<% end %>
<%= link_to 'Show', post_path(@post.user, @post) %> |
<%= link_to 'Back', posts_path(@post.user) %>

View File

@@ -0,0 +1,24 @@
<h1>Listing posts</h1>
<table>
<tr>
<th>User</th>
<th>Title</th>
<th>Body</th>
</tr>
<% for post in @posts %>
<tr>
<td><%=h post.user_id %></td>
<td><%=h post.title %></td>
<td><%=h post.body %></td>
<td><%= link_to 'Show', post_path(post.user, post) %></td>
<td><%= link_to 'Edit', edit_post_path(post.user, post) %></td>
<td><%= link_to 'Destroy', post_path(post.user, post), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New post', new_post_path(post.user) %>

View File

@@ -0,0 +1,26 @@
<h1>New post</h1>
<%= error_messages_for :post %>
<% form_for(:post, :url => posts_path(@user)) do |f| %>
<p>
<b>User</b><br />
<%= f.text_field :user_id %>
</p>
<p>
<b>Title</b><br />
<%= f.text_field :title %>
</p>
<p>
<b>Body</b><br />
<%= f.text_area :body %>
</p>
<p>
<%= submit_tag "Create" %>
</p>
<% end %>
<%= link_to 'Back', posts_path(@user) %>

View File

@@ -0,0 +1,18 @@
<p>
<b>User:</b>
<%=h @post.user_id %>
</p>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
<p>
<b>Body:</b>
<%=h @post.body %>
</p>
<%= link_to 'Edit', edit_post_path(@post.user, @post) %> |
<%= link_to 'Back', posts_path(@post.user) %>

View File

@@ -0,0 +1,22 @@
<h1>Editing user</h1>
<%= error_messages_for :user %>
<% form_for(:user, :url => user_path(@user), :html => { :method => :put }) do |f| %>
<p>
<b>Email</b><br />
<%= f.text_field :email %>
</p>
<p>
<b>Age</b><br />
<%= f.text_field :age %>
</p>
<p>
<%= submit_tag "Update" %>
</p>
<% end %>
<%= link_to 'Show', user_path(@user) %> |
<%= link_to 'Back', users_path %>

View File

@@ -0,0 +1,22 @@
<h1>Listing users</h1>
<table>
<tr>
<th>Email</th>
<th>Age</th>
</tr>
<% for user in @users %>
<tr>
<td><%=h user.email %></td>
<td><%=h user.age %></td>
<td><%= link_to 'Show', user_path(user) %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user_path(user), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New user', new_user_path %>

View File

@@ -0,0 +1,21 @@
<h1>New user</h1>
<%= error_messages_for :user %>
<% form_for(:user, :url => users_path) do |f| %>
<p>
<b>Email</b><br />
<%= f.text_field :email %>
</p>
<p>
<b>Age</b><br />
<%= f.text_field :age %>
</p>
<p>
<%= submit_tag "Create" %>
</p>
<% end %>
<%= link_to 'Back', users_path %>

View File

@@ -0,0 +1,13 @@
<p>
<b>Email:</b>
<%=h @user.email %>
</p>
<p>
<b>Age:</b>
<%=h @user.age %>
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>