Organizando plugins e gems.

This commit is contained in:
2009-07-16 11:51:47 -03:00
parent 4e22c87074
commit dcfc38eb09
506 changed files with 10538 additions and 45562 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,86 @@
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 }
format.rss do
headers['Content-Type'] = 'application/rss+xml'
session[:special] = '$2 off your next purchase'
session[:special_user_id] = @user.id
head :ok
end
end
end
def show
@post = @user.posts.find(params[:id])
@false_flag = false
respond_to do |format|
format.html { render :layout => 'wide' }
format.xml { render :xml => @post.to_xml }
end
end
def new
@post = @user.posts.build
render :layout => false
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 user_post_url(@post.user, @post) }
format.xml { head :created, :location => user_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 user_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 user_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,84 @@
class UsersController < ApplicationController
filter_parameter_logging :ssn
# 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,7 @@
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
validates_uniqueness_of :title, :scope => [:addressable_type, :addressable_id]
validates_length_of :zip, :minimum => 5
validates_numericality_of :zip
end

View File

@@ -0,0 +1,3 @@
class Flea < ActiveRecord::Base
has_and_belongs_to_many :dogs
end

View File

@@ -0,0 +1,4 @@
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => "User"
end

View File

@@ -0,0 +1,10 @@
module Pets
class Dog < ActiveRecord::Base
belongs_to :user, :foreign_key => :owner_id
belongs_to :address, :dependent => :destroy
has_many :treats
has_and_belongs_to_many :fleas, :join_table => :fleas
validates_presence_of :treats, :fleas
validates_presence_of :owner_id
end
end

View File

@@ -0,0 +1,12 @@
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
has_many :through_tags, :through => :taggings, :source => :tag
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,12 @@
class Product < ActiveRecord::Base
validates_presence_of :title
validates_inclusion_of :price, :in => 0..99, :unless => :tangible
validates_format_of :size, :with => /^\d+\D+$/, :unless => :tangible
validates_presence_of :price, :if => :tangible
validates_inclusion_of :price, :in => 1..9999, :if => :tangible
validates_inclusion_of :weight, :in => 1..100, :if => :tangible
validates_format_of :size, :with => /.+x.+x.+/, :if => :tangible
validates_length_of :size, :in => 5..20, :if => :tangible
end

View File

@@ -0,0 +1,8 @@
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
validates_length_of :name, :minimum => 2
attr_accessible :name
end

View File

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

View File

@@ -0,0 +1,3 @@
class Treat < ActiveRecord::Base
end

View File

@@ -0,0 +1,29 @@
class User < ActiveRecord::Base
has_many :posts
has_many :dogs, :foreign_key => :owner_id, :class_name => "Pets::Dog"
has_many :friendships
has_many :friends, :through => :friendships
has_one :address, :as => :addressable, :dependent => :destroy
named_scope :old, :conditions => "age > 50"
named_scope :eighteen, :conditions => { :age => 18 }
named_scope :recent, lambda {|count| { :limit => count } }
def self.recent_via_method(count)
scoped(:limit => count)
end
attr_protected :password
attr_readonly :name
validates_format_of :email, :with => /\w*@\w*.com/
validates_length_of :email, :in => 1..100
validates_numericality_of :age, :greater_than_or_equal_to => 1,
:less_than_or_equal_to => 100
validates_acceptance_of :eula
validates_uniqueness_of :email, :scope => :name, :case_sensitive => false
validates_length_of :ssn, :is => 9, :message => "Social Security Number is not the right length"
validates_numericality_of :ssn
end

View File

@@ -0,0 +1,19 @@
<!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" />
<meta name="description" content="Posts, posts and more posts" />
<meta name='keywords' content='posts' />
<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 @@
<html><%= yield %></html>

View File

@@ -0,0 +1,27 @@
<h1>Editing post</h1>
<%= error_messages_for :post %>
<% form_for(:post, :url => user_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', user_post_path(@post.user, @post) %> |
<%= link_to 'Back', user_posts_path(@post.user) %>

View File

@@ -0,0 +1,25 @@
<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', user_post_path(post.user, post) %></td>
<td><%= link_to 'Edit', edit_user_post_path(post.user, post) %></td>
<td><%= link_to 'Destroy', user_post_path(post.user, post), :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New post', new_user_post_path(post.user) %>

View File

@@ -0,0 +1,26 @@
<h1>New post</h1>
<%= error_messages_for :post %>
<% form_for(:post, :url => user_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', user_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_user_post_path(@post.user, @post) %> |
<%= link_to 'Back', user_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 %>