Permalink
Browse files

adding forum template application

  • Loading branch information...
1 parent 0a0338a commit edcee4095fa7c7bb626a91385469cafbd30d8b4e @ryanb committed Jul 4, 2009
Showing with 8,624 additions and 0 deletions.
  1. +3 −0 templates/forum/.gitignore
  2. +1 −0 templates/forum/README
  3. +10 −0 templates/forum/Rakefile
  4. +11 −0 templates/forum/app/controllers/application_controller.rb
  5. +46 −0 templates/forum/app/controllers/forums_controller.rb
  6. +22 −0 templates/forum/app/controllers/sessions_controller.rb
  7. +16 −0 templates/forum/app/controllers/users_controller.rb
  8. +3 −0 templates/forum/app/helpers/application_helper.rb
  9. +2 −0 templates/forum/app/helpers/forums_helper.rb
  10. +23 −0 templates/forum/app/helpers/layout_helper.rb
  11. +2 −0 templates/forum/app/helpers/sessions_helper.rb
  12. +2 −0 templates/forum/app/helpers/users_helper.rb
  13. +2 −0 templates/forum/app/models/forum.rb
  14. +38 −0 templates/forum/app/models/user.rb
  15. +12 −0 templates/forum/app/views/forums/_form.html.erb
  16. +8 −0 templates/forum/app/views/forums/edit.html.erb
  17. +20 −0 templates/forum/app/views/forums/index.html.erb
  18. +5 −0 templates/forum/app/views/forums/new.html.erb
  19. +12 −0 templates/forum/app/views/forums/show.html.erb
  20. +32 −0 templates/forum/app/views/layouts/application.html.erb
  21. +15 −0 templates/forum/app/views/sessions/new.html.erb
  22. +28 −0 templates/forum/app/views/users/new.html.erb
  23. +110 −0 templates/forum/config/boot.rb
  24. +22 −0 templates/forum/config/database.yml
  25. +41 −0 templates/forum/config/environment.rb
  26. +17 −0 templates/forum/config/environments/development.rb
  27. +28 −0 templates/forum/config/environments/production.rb
  28. +28 −0 templates/forum/config/environments/test.rb
  29. +7 −0 templates/forum/config/initializers/backtrace_silencers.rb
  30. +10 −0 templates/forum/config/initializers/inflections.rb
  31. +5 −0 templates/forum/config/initializers/mime_types.rb
  32. +19 −0 templates/forum/config/initializers/new_rails_defaults.rb
  33. +15 −0 templates/forum/config/initializers/session_store.rb
  34. +5 −0 templates/forum/config/locales/en.yml
  35. +12 −0 templates/forum/config/routes.rb
  36. +15 −0 templates/forum/db/migrate/20090704165649_create_users.rb
  37. +9 −0 templates/forum/db/migrate/20090704165807_add_admin_to_users.rb
  38. +13 −0 templates/forum/db/migrate/20090704171256_create_forums.rb
  39. +31 −0 templates/forum/db/schema.rb
  40. +2 −0 templates/forum/doc/README_FOR_APP
  41. +48 −0 templates/forum/lib/authentication.rb
  42. 0 templates/forum/log/.gitignore
  43. +30 −0 templates/forum/public/404.html
  44. +30 −0 templates/forum/public/422.html
  45. +30 −0 templates/forum/public/500.html
  46. 0 templates/forum/public/favicon.ico
  47. +2 −0 templates/forum/public/javascripts/application.js
  48. +963 −0 templates/forum/public/javascripts/controls.js
  49. +973 −0 templates/forum/public/javascripts/dragdrop.js
  50. +1,128 −0 templates/forum/public/javascripts/effects.js
  51. +4,320 −0 templates/forum/public/javascripts/prototype.js
  52. +5 −0 templates/forum/public/robots.txt
  53. +108 −0 templates/forum/public/stylesheets/application.css
  54. +4 −0 templates/forum/script/about
  55. +3 −0 templates/forum/script/console
  56. +3 −0 templates/forum/script/dbconsole
  57. +3 −0 templates/forum/script/destroy
  58. +3 −0 templates/forum/script/generate
  59. +3 −0 templates/forum/script/performance/benchmarker
  60. +3 −0 templates/forum/script/performance/profiler
  61. +3 −0 templates/forum/script/plugin
  62. +3 −0 templates/forum/script/runner
  63. +3 −0 templates/forum/script/server
  64. +5 −0 templates/forum/test/fixtures/forums.yml
  65. +12 −0 templates/forum/test/fixtures/users.yml
  66. +54 −0 templates/forum/test/functional/forums_controller_test.rb
  67. +22 −0 templates/forum/test/functional/sessions_controller_test.rb
  68. +21 −0 templates/forum/test/functional/users_controller_test.rb
  69. +9 −0 templates/forum/test/performance/browsing_test.rb
  70. +38 −0 templates/forum/test/test_helper.rb
  71. +7 −0 templates/forum/test/unit/forum_test.rb
  72. +86 −0 templates/forum/test/unit/user_test.rb
  73. 0 templates/forum/tmp/.gitignore
  74. 0 templates/forum/vendor/.gitignore
@@ -0,0 +1,3 @@
+tmp/**/*
+log/*.log
+*.sqlite3
View
@@ -0,0 +1 @@
+Application used in Railscasts episodes.
View
@@ -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'
@@ -0,0 +1,11 @@
+# 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
+ include Authentication
+ helper :all # include all helpers, all the time
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
+
+ # Scrub sensitive parameters from your log
+ # filter_parameter_logging :password
+end
@@ -0,0 +1,46 @@
+class ForumsController < ApplicationController
+ before_filter :admin_required, :except => [:index, :show]
+
+ def index
+ @forums = Forum.all
+ end
+
+ def show
+ @forum = Forum.find(params[:id])
+ end
+
+ def new
+ @forum = Forum.new
+ end
+
+ def create
+ @forum = Forum.new(params[:forum])
+ if @forum.save
+ flash[:notice] = "Successfully created forum."
+ redirect_to @forum
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @forum = Forum.find(params[:id])
+ end
+
+ def update
+ @forum = Forum.find(params[:id])
+ if @forum.update_attributes(params[:forum])
+ flash[:notice] = "Successfully updated forum."
+ redirect_to @forum
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @forum = Forum.find(params[:id])
+ @forum.destroy
+ flash[:notice] = "Successfully destroyed forum."
+ redirect_to forums_url
+ end
+end
@@ -0,0 +1,22 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.authenticate(params[:login], params[:password])
+ if user
+ session[:user_id] = user.id
+ flash[:notice] = "Logged in successfully."
+ redirect_to root_url
+ else
+ flash.now[:error] = "Invalid login or password."
+ render :action => 'new'
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ flash[:notice] = "You have been logged out."
+ redirect_to root_url
+ end
+end
@@ -0,0 +1,16 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ session[:user_id] = @user.id
+ flash[:notice] = "Thank you for signing up! You are now logged in."
+ redirect_to root_url
+ else
+ render :action => 'new'
+ end
+ end
+end
@@ -0,0 +1,3 @@
+# Methods added to this helper will be available to all templates in the application.
+module ApplicationHelper
+end
@@ -0,0 +1,2 @@
+module ForumsHelper
+end
@@ -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
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
@@ -0,0 +1,2 @@
+class Forum < ActiveRecord::Base
+end
@@ -0,0 +1,38 @@
+class User < ActiveRecord::Base
+ # new columns need to be added here to be writable through mass assignment
+ attr_accessible :username, :email, :password, :password_confirmation, :admin
+
+ attr_accessor :password
+ before_save :prepare_password
+
+ validates_presence_of :username
+ validates_uniqueness_of :username, :email, :allow_blank => true
+ validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
+ validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
+ validates_presence_of :password, :on => :create
+ validates_confirmation_of :password
+ validates_length_of :password, :minimum => 4, :allow_blank => true
+
+ # login can be either username or email address
+ def self.authenticate(login, pass)
+ user = find_by_username(login) || find_by_email(login)
+ return user if user && user.matching_password?(pass)
+ end
+
+ def matching_password?(pass)
+ self.password_hash == encrypt_password(pass)
+ end
+
+ private
+
+ def prepare_password
+ unless password.blank?
+ self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
+ self.password_hash = encrypt_password(password)
+ end
+ end
+
+ def encrypt_password(pass)
+ Digest::SHA1.hexdigest([pass, password_salt].join)
+ end
+end
@@ -0,0 +1,12 @@
+<% form_for @forum do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Forum" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @forum %> |
+ <%= link_to "View All", forums_path %>
+</p>
@@ -0,0 +1,20 @@
+<% title "Piano Forums" %>
+
+<div id="forums">
+ <% for forum in @forums %>
+ <div class="forum">
+ <h2><%= link_to h(forum.name), forum %></h2>
+ <p><%=h forum.description %></p>
+ <% if admin? %>
+ <p>
+ <%= link_to "Edit", edit_forum_path(forum) %> |
+ <%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %>
+ </p>
+ <% end %>
+ </div>
+ <% end %>
+</div>
+
+<% if admin? %>
+ <p><%= link_to "New Forum", new_forum_path %></p>
+<% end %>
@@ -0,0 +1,5 @@
+<% title "New Forum" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", forums_path %></p>
@@ -0,0 +1,12 @@
+<% title "Forum" %>
+
+<p>
+ <strong>Name:</strong>
+ <%=h @forum.name %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_forum_path(@forum) %> |
+ <%= link_to "Destroy", @forum, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", forums_path %>
+</p>
@@ -0,0 +1,32 @@
+<!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">
+ <p id="user_info">
+ <% if logged_in? %>
+ Welcome <strong><%=h current_user.username %></strong>! Not you?
+ <%= link_to "Log out", logout_path %>
+ <% else %>
+ <%= link_to "Sign up", signup_path %> or
+ <%= link_to "log in", login_path %>.
+ <% end %>
+ </p>
+
+ <%- 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>
@@ -0,0 +1,15 @@
+<% title "Log in" %>
+
+<p>Don't have an account? <%= link_to "Sign up!", signup_path %></p>
+
+<% form_tag sessions_path do %>
+ <p>
+ <%= label_tag :login, "Username or Email Address" %><br />
+ <%= text_field_tag :login, params[:login] %>
+ </p>
+ <p>
+ <%= label_tag :password %><br />
+ <%= password_field_tag :password %>
+ </p>
+ <p><%= submit_tag "Log in" %></p>
+<% end %>
@@ -0,0 +1,28 @@
+<% title "Sign up" %>
+
+<p>Already have an account? <%= link_to "Log in", login_path %>.</p>
+
+<% form_for @user do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :username %><br />
+ <%= f.text_field :username %>
+ </p>
+ <p>
+ <%= f.label :email, "Email Address" %><br />
+ <%= f.text_field :email %>
+ </p>
+ <p>
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </p>
+ <p>
+ <%= f.label :password_confirmation, "Confirm Password" %><br />
+ <%= f.password_field :password_confirmation %>
+ </p>
+ <p>
+ <%= f.check_box :admin %>
+ <%= f.label :admin %>
+ </p>
+ <p><%= f.submit "Sign up" %></p>
+<% end %>
Oops, something went wrong.

0 comments on commit edcee40

Please sign in to comment.