Permalink
Browse files

adding episode 214

  • Loading branch information...
1 parent 3ddedc5 commit 9d83b14ad522b98501e8e99fce8fa3064372a9a3 @ryanb committed May 17, 2010
Showing with 9,843 additions and 0 deletions.
  1. +11 −0 episode-214/README
  2. +3 −0 episode-214/todo/.gitignore
  3. +4 −0 episode-214/todo/README
  4. +10 −0 episode-214/todo/Rakefile
  5. +4 −0 episode-214/todo/app/controllers/abingo_dashboard_controller.rb
  6. +29 −0 episode-214/todo/app/controllers/application_controller.rb
  7. +44 −0 episode-214/todo/app/controllers/projects_controller.rb
  8. +22 −0 episode-214/todo/app/controllers/sessions_controller.rb
  9. +36 −0 episode-214/todo/app/controllers/tasks_controller.rb
  10. +17 −0 episode-214/todo/app/controllers/users_controller.rb
  11. +2 −0 episode-214/todo/app/helpers/abingo_dashboard_helper.rb
  12. +3 −0 episode-214/todo/app/helpers/application_helper.rb
  13. +23 −0 episode-214/todo/app/helpers/layout_helper.rb
  14. +2 −0 episode-214/todo/app/helpers/projects_helper.rb
  15. +2 −0 episode-214/todo/app/helpers/sessions_helper.rb
  16. +2 −0 episode-214/todo/app/helpers/tasks_helper.rb
  17. +2 −0 episode-214/todo/app/helpers/users_helper.rb
  18. +3 −0 episode-214/todo/app/models/project.rb
  19. +3 −0 episode-214/todo/app/models/task.rb
  20. +38 −0 episode-214/todo/app/models/user.rb
  21. +22 −0 episode-214/todo/app/views/layouts/application.html.erb
  22. +8 −0 episode-214/todo/app/views/projects/_form.html.erb
  23. +8 −0 episode-214/todo/app/views/projects/edit.html.erb
  24. +16 −0 episode-214/todo/app/views/projects/index.html.erb
  25. +5 −0 episode-214/todo/app/views/projects/new.html.erb
  26. +17 −0 episode-214/todo/app/views/projects/show.html.erb
  27. +15 −0 episode-214/todo/app/views/sessions/new.html.erb
  28. +13 −0 episode-214/todo/app/views/tasks/_form.html.erb
  29. +4 −0 episode-214/todo/app/views/tasks/edit.html.erb
  30. +4 −0 episode-214/todo/app/views/tasks/new.html.erb
  31. +30 −0 episode-214/todo/app/views/users/new.html.erb
  32. +110 −0 episode-214/todo/config/boot.rb
  33. +19 −0 episode-214/todo/config/database.yml
  34. +67 −0 episode-214/todo/config/environment.rb
  35. +17 −0 episode-214/todo/config/environments/development.rb
  36. +22 −0 episode-214/todo/config/environments/production.rb
  37. +22 −0 episode-214/todo/config/environments/test.rb
  38. +10 −0 episode-214/todo/config/initializers/inflections.rb
  39. +5 −0 episode-214/todo/config/initializers/mime_types.rb
  40. +15 −0 episode-214/todo/config/initializers/new_rails_defaults.rb
  41. +13 −0 episode-214/todo/config/routes.rb
  42. +12 −0 episode-214/todo/db/migrate/20080823221824_create_projects.rb
  43. +14 −0 episode-214/todo/db/migrate/20080823223136_create_tasks.rb
  44. +15 −0 episode-214/todo/db/migrate/20100517011209_create_users.rb
  45. +31 −0 episode-214/todo/db/migrate/20100517014823_abingo_migration_1_0.rb
  46. +58 −0 episode-214/todo/db/schema.rb
  47. +2 −0 episode-214/todo/doc/README_FOR_APP
  48. +49 −0 episode-214/todo/lib/authentication.rb
  49. +22 −0 episode-214/todo/lib/tasks/application.rake
  50. +30 −0 episode-214/todo/public/404.html
  51. +30 −0 episode-214/todo/public/422.html
  52. +30 −0 episode-214/todo/public/500.html
  53. +10 −0 episode-214/todo/public/dispatch.cgi
  54. +24 −0 episode-214/todo/public/dispatch.fcgi
  55. +10 −0 episode-214/todo/public/dispatch.rb
  56. 0 episode-214/todo/public/favicon.ico
  57. +2 −0 episode-214/todo/public/javascripts/application.js
  58. +963 −0 episode-214/todo/public/javascripts/controls.js
  59. +973 −0 episode-214/todo/public/javascripts/dragdrop.js
  60. +1,128 −0 episode-214/todo/public/javascripts/effects.js
  61. +4,320 −0 episode-214/todo/public/javascripts/prototype.js
  62. +5 −0 episode-214/todo/public/robots.txt
  63. +80 −0 episode-214/todo/public/stylesheets/application.css
  64. +3 −0 episode-214/todo/script/about
  65. +3 −0 episode-214/todo/script/console
  66. +3 −0 episode-214/todo/script/dbconsole
  67. +3 −0 episode-214/todo/script/destroy
  68. +3 −0 episode-214/todo/script/generate
  69. +3 −0 episode-214/todo/script/performance/benchmarker
  70. +3 −0 episode-214/todo/script/performance/profiler
  71. +3 −0 episode-214/todo/script/performance/request
  72. +3 −0 episode-214/todo/script/plugin
  73. +3 −0 episode-214/todo/script/process/inspector
  74. +3 −0 episode-214/todo/script/process/reaper
  75. +3 −0 episode-214/todo/script/process/spawner
  76. +3 −0 episode-214/todo/script/runner
  77. +3 −0 episode-214/todo/script/server
  78. +5 −0 episode-214/todo/test/fixtures/projects.yml
  79. +9 −0 episode-214/todo/test/fixtures/tasks.yml
  80. +12 −0 episode-214/todo/test/fixtures/users.yml
  81. +8 −0 episode-214/todo/test/functional/abingo_dashboard_controller_test.rb
  82. +54 −0 episode-214/todo/test/functional/projects_controller_test.rb
  83. +22 −0 episode-214/todo/test/functional/sessions_controller_test.rb
  84. +44 −0 episode-214/todo/test/functional/tasks_controller_test.rb
  85. +21 −0 episode-214/todo/test/functional/users_controller_test.rb
  86. +38 −0 episode-214/todo/test/test_helper.rb
  87. +4 −0 episode-214/todo/test/unit/helpers/abingo_dashboard_helper_test.rb
  88. +7 −0 episode-214/todo/test/unit/project_test.rb
  89. +7 −0 episode-214/todo/test/unit/task_test.rb
  90. +86 −0 episode-214/todo/test/unit/user_test.rb
  91. +20 −0 episode-214/todo/vendor/plugins/abingo/MIT-LICENSE
  92. +109 −0 episode-214/todo/vendor/plugins/abingo/README
  93. +23 −0 episode-214/todo/vendor/plugins/abingo/Rakefile
  94. +14 −0 episode-214/todo/vendor/plugins/abingo/generators/abingo_migration/abingo_migration_generator.rb
  95. +31 −0 episode-214/todo/vendor/plugins/abingo/generators/abingo_migration/templates/abingo_migration.rb
  96. +5 −0 episode-214/todo/vendor/plugins/abingo/init.rb
  97. +1 −0 episode-214/todo/vendor/plugins/abingo/install.rb
  98. +225 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo.rb
  99. +23 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/alternative.rb
  100. +25 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/controller/dashboard.rb
  101. +9 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/conversion_rate.rb
  102. +99 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/experiment.rb
  103. +13 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/rails/controller/dashboard.rb
  104. +90 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/statistics.rb
  105. +43 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/views/dashboard/_experiment.erb
  106. +20 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo/views/dashboard/index.erb
  107. +28 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo_sugar.rb
  108. +42 −0 episode-214/todo/vendor/plugins/abingo/lib/abingo_view_helper.rb
  109. +11 −0 episode-214/todo/vendor/plugins/abingo/strip.rb
  110. +4 −0 episode-214/todo/vendor/plugins/abingo/tasks/abingo_tasks.rake
  111. +135 −0 episode-214/todo/vendor/plugins/abingo/test/abingo_test.rb
  112. +3 −0 episode-214/todo/vendor/plugins/abingo/test/test_helper.rb
  113. +1 −0 episode-214/todo/vendor/plugins/abingo/uninstall.rb
View
@@ -0,0 +1,11 @@
+Railscasts Episode #214: A/B Testing with A/Bingo
+
+http://railscasts.com/episodes/214
+
+Commands
+
+ script/plugin install git://git.bingocardcreator.com/abingo.git
+ script/generate abingo_migration
+ rake db:migrate
+ script/generate controller abingo_dashboard
+ script/plugin install --force git://github.com/ryanb/abingo.git
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -0,0 +1,4 @@
+Railscasts Example To Do App
+--
+
+To setup the app, just run `rake setup`.
@@ -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,4 @@
+class AbingoDashboardController < ApplicationController
+ # TODO add some authorization
+ include Abingo::Controller::Dashboard
+end
@@ -0,0 +1,29 @@
+# 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
+ before_filter :set_abingo_identity
+
+ # See ActionController::RequestForgeryProtection for details
+ # Uncomment the :secret if you're not using the cookie session store
+ protect_from_forgery # :secret => '9ae178eac1fbbd4062a93c8c558a12ef'
+
+ # 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
+ private
+
+ def set_abingo_identity
+ if request.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)\b/i
+ Abingo.identity = "robot"
+ elsif current_user
+ Abingo.identity = current_user.id
+ else
+ session[:abingo_identity] ||= rand(10 ** 10)
+ Abingo.identity = session[:abingo_identity]
+ end
+ end
+end
@@ -0,0 +1,44 @@
+class ProjectsController < ApplicationController
+ def index
+ @projects = Project.find(:all)
+ end
+
+ def show
+ @project = Project.find(params[:id])
+ end
+
+ def new
+ @project = Project.new
+ end
+
+ def create
+ @project = Project.new(params[:project])
+ if @project.save
+ flash[:notice] = "Successfully created project."
+ redirect_to @project
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @project = Project.find(params[:id])
+ end
+
+ def update
+ @project = Project.find(params[:id])
+ if @project.update_attributes(params[:project])
+ flash[:notice] = "Successfully updated project."
+ redirect_to @project
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @project = Project.find(params[:id])
+ @project.destroy
+ flash[:notice] = "Successfully destroyed project."
+ redirect_to projects_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_target_or_default(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,36 @@
+class TasksController < ApplicationController
+ def new
+ @task = Task.new(:project_id => params[:project_id])
+ end
+
+ def create
+ @task = Task.new(params[:task])
+ if @task.save
+ flash[:notice] = "Successfully created task."
+ redirect_to @task.project
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @task = Task.find(params[:id])
+ end
+
+ def update
+ @task = Task.find(params[:id])
+ if @task.update_attributes(params[:task])
+ flash[:notice] = "Successfully updated task."
+ redirect_to @task.project
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @task = Task.find(params[:id])
+ @task.destroy
+ flash[:notice] = "Successfully destroyed task."
+ redirect_to @task.project
+ end
+end
@@ -0,0 +1,17 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ bingo! "signup"
+ 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,2 @@
+module AbingoDashboardHelper
+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,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 ProjectsHelper
+end
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,2 @@
+module TasksHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
@@ -0,0 +1,3 @@
+class Project < ActiveRecord::Base
+ has_many :tasks
+end
@@ -0,0 +1,3 @@
+class Task < ActiveRecord::Base
+ belongs_to :project
+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
+
+ 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,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>
@@ -0,0 +1,8 @@
+<%= error_messages_for :project %>
+<% form_for @project do |f| %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Project" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @project %> |
+ <%= link_to "View All", projects_path %>
+</p>
@@ -0,0 +1,16 @@
+<% title "Projects" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ </tr>
+ <% for project in @projects %>
+ <tr>
+ <td><%= link_to h(project.name), project %></td>
+ <td><%= link_to "Edit", edit_project_path(project) %></td>
+ <td><%= link_to "Destroy", project, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Project", new_project_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Project" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", projects_path %></p>
@@ -0,0 +1,17 @@
+<% title @project.name %>
+
+<h2>Tasks</h2>
+<ul>
+ <% for task in @project.tasks %>
+ <li>
+ <%=h task.name %>
+ (<%= link_to "Edit", edit_task_path(task) %> |
+ <%= link_to "Destroy", task, :confirm => "Are you sure?", :method => :delete %>)
+ </li>
+ <% end %>
+</ul>
+
+<p>
+ <%= link_to "New Task", new_task_path(:project_id => @project) %> |
+ <%= link_to "View All Projects", projects_path %>
+</p>
@@ -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,13 @@
+<%= error_messages_for :task %>
+<% form_for @task do |f| %>
+ <%= f.hidden_field :project_id %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.check_box :complete %>
+ <%= f.label :complete %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,4 @@
+<% title "Edit Task" %>
+
+<%= render :partial => 'form' %>
+
Oops, something went wrong.

0 comments on commit 9d83b14

Please sign in to comment.