Re-namespacing a bit to clear out some fairly old stuff from the top level
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
before_filter :set_user, :count_visits
|
||||
protect_from_forgery
|
||||
|
||||
private
|
||||
def count_visits
|
||||
value = (cookies[:visits] || '0').to_i
|
||||
cookies[:visits] = (value + 1).to_s
|
||||
@visits = cookies[:visits]
|
||||
end
|
||||
|
||||
protected
|
||||
def set_user
|
||||
@user = User.find(session[:id]) if @user.nil? && session[:id]
|
||||
end
|
||||
|
||||
def login_required
|
||||
return true if @user
|
||||
access_denied
|
||||
return false
|
||||
end
|
||||
|
||||
def access_denied
|
||||
session[:return_to] = request.fullpath
|
||||
flash[:error] = 'Oops. You need to login before you can view that page.'
|
||||
redirect_to :controller => 'user', :action => 'login'
|
||||
end
|
||||
end
|
@@ -0,0 +1,18 @@
|
||||
class FooController < ApplicationController
|
||||
layout :figure_out_layout
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def pretty
|
||||
end
|
||||
|
||||
def figure_out_layout
|
||||
if action_name =~ /pretty/
|
||||
'pretty'
|
||||
else
|
||||
'standard'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,5 @@
|
||||
class HelloController < ApplicationController
|
||||
def world
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,6 @@
|
||||
class IndexController < ApplicationController
|
||||
def index
|
||||
session[:first_time] ||= Time.now
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,20 @@
|
||||
require 'sha1'
|
||||
|
||||
class ListController < ApplicationController
|
||||
def index
|
||||
@list = [1, "string", :symbol, ['list']]
|
||||
end
|
||||
|
||||
def shopping_list
|
||||
@list = [ListItem.new(4, 'aspirin'), ListItem.new(199, 'succotash')]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
class ListItem
|
||||
attr_accessor :name, :id
|
||||
def initialize(id, name)
|
||||
@id, @name = id, name
|
||||
end
|
||||
end
|
@@ -0,0 +1,25 @@
|
||||
$one = 1
|
||||
|
||||
class NovelController < ApplicationController
|
||||
$two = 2
|
||||
|
||||
def index
|
||||
@title = 'Shattered View: a Novel on Rails'
|
||||
one_plus_one = 1 + 1
|
||||
increment_counter one_plus_one
|
||||
end
|
||||
|
||||
def helper_method
|
||||
@help_message = "I see you've come to me for help."
|
||||
end
|
||||
|
||||
def increment_counter(by)
|
||||
@counter ||= 0
|
||||
@counter += by
|
||||
end
|
||||
|
||||
def sequel
|
||||
$three = 3
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,5 @@
|
||||
class PeopleController < ApplicationController
|
||||
def list
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,8 @@
|
||||
class StatusController < ApplicationController
|
||||
def index
|
||||
@title = "System Status"
|
||||
time = Time.now
|
||||
@time = time
|
||||
@ps = `ps aux`
|
||||
end
|
||||
end
|
@@ -0,0 +1,28 @@
|
||||
class UserController < ApplicationController
|
||||
before_filter :login_required, :only => :my_account
|
||||
|
||||
def login
|
||||
@user = User.new
|
||||
@user.username = params[:username]
|
||||
end
|
||||
|
||||
def process_login
|
||||
if user = User.authenticate(params[:user])
|
||||
session[:id] = user.id
|
||||
redirect_to session[:return_to] || '/'
|
||||
else
|
||||
flash[:error] = 'Invalid login.'
|
||||
redirect_to :action => 'login', :username => params[:user][:username]
|
||||
end
|
||||
end
|
||||
|
||||
def logout
|
||||
reset_session
|
||||
flash[:message] = 'Logged out.'
|
||||
redirect_to :action => 'login'
|
||||
end
|
||||
|
||||
def my_account
|
||||
end
|
||||
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
module ApplicationHelper
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
module FooHelper
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
module IndexHelper
|
||||
end
|
@@ -0,0 +1,9 @@
|
||||
require 'sha1'
|
||||
|
||||
|
||||
module ListHelper
|
||||
def create_li(item, i)
|
||||
%{<li class="#{ i % 2 == 0 ? 'even' : 'odd' }">#{i}:
|
||||
#{SHA1.new(item.object_id.to_s)}</li>}
|
||||
end
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
module PeopleHelper
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
module UserHelper
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
class Person < ActiveRecord::Base
|
||||
end
|
32
oldstuff/RubyFun/cookbook/015/hodgepodge/app/models/user.rb
Normal file
32
oldstuff/RubyFun/cookbook/015/hodgepodge/app/models/user.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
require 'sha1'
|
||||
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessor :password
|
||||
attr_protected :hashed_password
|
||||
validates_uniqueness_of :username
|
||||
validates_confirmation_of :password, :if => lambda { |user|
|
||||
user.new_record? or not user.password.blank? }
|
||||
validates_length_of :password, :within => 5..40, :if => lambda { |user|
|
||||
user.new_record? or not user.password.blank? }
|
||||
|
||||
def self.hashed(str)
|
||||
SHA1.new(str).to_s
|
||||
end
|
||||
|
||||
def self.authenticate(user_info)
|
||||
user = find_by_username(user_info[:username])
|
||||
if user && user.hashed_password == hashed(user_info[:password])
|
||||
return user
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
before_save :update_password
|
||||
|
||||
def update_password
|
||||
if not password.blank?
|
||||
self.hashed_password = self.class.hashed(password)
|
||||
end
|
||||
end
|
||||
end
|
@@ -0,0 +1,2 @@
|
||||
<h1>Foo#count</h1>
|
||||
<p>Find me in app/views/foo/count.html.erb</p>
|
@@ -0,0 +1,2 @@
|
||||
<h1>Foo#index</h1>
|
||||
<p>Find me in app/views/foo/index.html.erb</p>
|
@@ -0,0 +1,2 @@
|
||||
<h1>Foo#pretty</h1>
|
||||
<p>Find me in app/views/foo/pretty.html.erb</p>
|
@@ -0,0 +1,11 @@
|
||||
<h1>Several increasingly silly ways of displaying “Hello world!”:</h1>
|
||||
|
||||
<p><%= "Hello world!" %></p>
|
||||
<p><%= "Hello" + " world!" %></p>
|
||||
<p><%= w = "world"
|
||||
"Hello #{w}!" %></p>
|
||||
<p><%= 'H' + ?e.chr + ('l' * 2) %><%= ('o word!').gsub('d', 'ld')%></p>
|
||||
|
||||
<% hello = "Hello" %>
|
||||
<% world = "world!" %>
|
||||
<%= hello %> <%= world %>
|
@@ -0,0 +1,4 @@
|
||||
<p>You first visited this site on <%= session[:first_time] %>.</p>
|
||||
<p>That was <%= time_ago_in_words session[:first_time] %> ago.</p>
|
||||
|
||||
<p>You have visited this website's pages <%= @visits %> time(s).</p>
|
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>BAR :: My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>COUNT :: My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Pretty Website - <%= @title %></title>
|
||||
<style type="text/css">
|
||||
body {
|
||||
background: #f99;
|
||||
color: #222;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Plain Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,4 @@
|
||||
<%= form_tag :action => 'new' do %>
|
||||
Item: <%= text_field "item", "value" %>

|
||||
<%= submit_tag "Add new item" %>
|
||||
<% end %>
|
@@ -0,0 +1,5 @@
|
||||
<ul style="font-family:monospace;">
|
||||
<% @list.each_with_index do |item, i| %>
|
||||
<%= create_li(item, i).html_safe %>
|
||||
<% end %>
|
||||
</ul>
|
@@ -0,0 +1,11 @@
|
||||
<h2>My shopping list</h2>
|
||||
|
||||
<ul>
|
||||
<% @list.each do |item| %>
|
||||
<li><%= item.name %>
|
||||
<%= link_to 'Delete', {:action => 'delete', :id => item.id} %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<%= render :partial => 'new_item_form' %>
|
@@ -0,0 +1,6 @@
|
||||
<h1><%= @title %></h1>
|
||||
|
||||
<p>I looked up, but saw only the number <%= @counter %></p>
|
||||
|
||||
<p>“What are you doing here?” I asked sharply. “Was it
|
||||
<%= @counter.succ %> who sent you?”</p>
|
@@ -0,0 +1,2 @@
|
||||
<p>Here they come, the counting numbers,
|
||||
<%= $one %>, <%= $two %>, <%= $three %>.</p>
|
@@ -0,0 +1,6 @@
|
||||
<!-- list.rhtml -->
|
||||
<ul>
|
||||
<% Person.find(:all).each do |person| %>
|
||||
<li>Name: <%= person.name %>, Email: <%= person.email %></li>
|
||||
<% end %>
|
||||
</ul>
|
@@ -0,0 +1,2 @@
|
||||
<h1>Processes running at <%= @time %></h1>
|
||||
<pre><%= @ps %></pre>
|
@@ -0,0 +1,14 @@
|
||||
<% if @flash %>
|
||||
<% if @flash[:message] %>
|
||||
<div id="flash-message"><%= @flash[:message] %></div>
|
||||
<% end %>
|
||||
<% if @flash[:error] %>
|
||||
<div id="flash-error"><%= @flash[:error] %></div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= form_tag :action => 'process_login' do %>
|
||||
<label for="username">Username:</label> <%= text_field "user", "username" %>

|
||||
<label for="password">Password:</label> <%= password_field "user", "password" %>

|
||||
<%= submit_tag %>
|
||||
<% end %>
|
@@ -0,0 +1,2 @@
|
||||
<h1>User#logout</h1>
|
||||
<p>Find me in app/views/user/logout.html.erb</p>
|
@@ -0,0 +1,3 @@
|
||||
<h1>Account Info</h1>
|
||||
|
||||
<p>Your username is <%= User.find(session[:id]).username %>
|
@@ -0,0 +1,2 @@
|
||||
<h1>User#process_login</h1>
|
||||
<p>Find me in app/views/user/process_login.html.erb</p>
|
Reference in New Issue
Block a user