Commit 11d32a28bae7eafdb807d4c7ae23e2acd93b67ce

ready to test deployment

Commit diff

app/controllers/application.rb

 
22# Likewise, all the methods added will be available for all controllers.
33
44class ApplicationController < ActionController::Base
5 include AuthenticatedSystem
6
57 helper :all # include all helpers, all the time
68
79 # See ActionController::RequestForgeryProtection for details
toggle raw diff

app/controllers/projects_controller.rb

 
11class ProjectsController < ApplicationController
2 before_filter :login_required
3
24 layout "standard"
35
46 undoable_methods
88 # GET /projects
99 # GET /projects.xml
1010 def index
11 @projects = Project.find(:all)
11 @projects = current_user.projects.find(:all)
1212
1313 respond_to do |format|
1414 format.html # index.html.erb
1919 # GET /projects/1
2020 # GET /projects/1.xml
2121 def show
22 @project = Project.find(params[:id])
22 @project = current_user.projects.find(params[:id])
2323
2424 respond_to do |format|
2525 format.html # show.html.erb
3030 # GET /projects/new
3131 # GET /projects/new.xml
3232 def new
33 @project = Project.new
33 @project = current_user.projects.new
3434
3535 respond_to do |format|
3636 format.html # new.html.erb
4040
4141 # GET /projects/1/edit
4242 def edit
43 @project = Project.find(params[:id])
43 @project = current_user.projects.find(params[:id])
4444 end
4545
4646 # POST /projects
4747 # POST /projects.xml
4848 def create
49 @project = Project.new(params[:project])
49 @project = current_user.projects.new(params[:project])
5050
5151 respond_to do |format|
5252 change("create project #{@project.title}", projects_path, projects_path) do
6565 # PUT /projects/1
6666 # PUT /projects/1.xml
6767 def update
68 @project = Project.find(params[:id])
68 @project = current_user.projects.find(params[:id])
6969
7070 respond_to do |format|
7171 change("update project #{@project.title}", edit_project_path(@project), project_path(@project)) do
8484 # DELETE /projects/1
8585 # DELETE /projects/1.xml
8686 def destroy
87 @project = Project.find(params[:id])
87 @project = current_user.projects.find(params[:id])
8888 change("delete project #{@project.title}", project_path(@project), projects_path) do
8989 @project.destroy
9090 end
toggle raw diff

app/controllers/sessions_controller.rb

 
1# This controller handles the login/logout function of the site.
2class SessionsController < ApplicationController
3
4 layout "standard"
5
6 # render new.rhtml
7 def new
8 end
9
10 def create
11 self.current_user = User.authenticate(params[:login], params[:password])
12 if logged_in?
13 if params[:remember_me] == "1"
14 self.current_user.remember_me
15 cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
16 end
17 redirect_back_or_default('/')
18 flash[:notice] = "Logged in successfully"
19 else
20 render :action => 'new'
21 end
22 end
23
24 def destroy
25 self.current_user.forget_me if logged_in?
26 cookies.delete :auth_token
27 reset_session
28 flash[:notice] = "You have been logged out."
29 redirect_back_or_default('/')
30 end
31end
toggle raw diff

app/controllers/tasks_controller.rb

 
11class TasksController < ApplicationController
22 before_filter(:get_project)
3 before_filter(:login_required)
34
45 layout "standard"
56
116116
117117 private
118118 def get_project
119 @project = Project.find(params[:project_id])
119 @project = current_user.projects.find(params[:project_id])
120120 end
121121
122122end
toggle raw diff

app/controllers/users_controller.rb

 
1class UsersController < ApplicationController
2
3 layout "standard"
4
5 # render new.rhtml
6 def new
7 end
8
9 def create
10 cookies.delete :auth_token
11 # protects against session fixation attacks, wreaks havoc with
12 # request forgery protection.
13 # uncomment at your own risk
14 # reset_session
15 @user = User.new(params[:user])
16 @user.save
17 if @user.errors.empty?
18 self.current_user = @user
19 redirect_back_or_default('/')
20 flash[:notice] = "Thanks for signing up!"
21 else
22 render :action => 'new'
23 end
24 end
25
26end
toggle raw diff

app/helpers/session_helper.rb

 
1module SessionHelper
2end
toggle raw diff

app/helpers/users_helper.rb

 
1module UsersHelper
2end
toggle raw diff

app/models/project.rb

 
11class Project < ActiveRecord::Base
2 belongs_to :user
23 has_many :tasks
34 acts_as_undoable
45end
toggle raw diff

app/models/user.rb

 
1require 'digest/sha1'
2class User < ActiveRecord::Base
3 # Virtual attribute for the unencrypted password
4 attr_accessor :password
5
6 validates_presence_of :login
7 validates_presence_of :password, :if => :password_required?
8 validates_presence_of :password_confirmation, :if => :password_required?
9 validates_length_of :password, :within => 3..40, :if => :password_required?
10 validates_confirmation_of :password, :if => :password_required?
11 validates_length_of :login, :within => 3..40
12 validates_uniqueness_of :login, :case_sensitive => false
13 before_save :encrypt_password
14
15 # prevents a user from submitting a crafted form that bypasses activation
16 # anything else you want your user to change should be added here.
17 attr_accessible :login, :password, :password_confirmation
18
19 has_many :projects
20
21 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
22 def self.authenticate(login, password)
23 u = find_by_login(login) # need to get the salt
24 u && u.authenticated?(password) ? u : nil
25 end
26
27 # Encrypts some data with the salt.
28 def self.encrypt(password, salt)
29 Digest::SHA1.hexdigest("--#{salt}--#{password}--")
30 end
31
32 # Encrypts the password with the user salt
33 def encrypt(password)
34 self.class.encrypt(password, salt)
35 end
36
37 def authenticated?(password)
38 crypted_password == encrypt(password)
39 end
40
41 def remember_token?
42 remember_token_expires_at && Time.now.utc < remember_token_expires_at
43 end
44
45 # These create and unset the fields required for remembering users between browser closes
46 def remember_me
47 remember_me_for 2.weeks
48 end
49
50 def remember_me_for(time)
51 remember_me_until time.from_now.utc
52 end
53
54 def remember_me_until(time)
55 self.remember_token_expires_at = time
56 self.remember_token = encrypt("#{login}--#{remember_token_expires_at}")
57 save(false)
58 end
59
60 def forget_me
61 self.remember_token_expires_at = nil
62 self.remember_token = nil
63 save(false)
64 end
65
66 protected
67 # before filter
68 def encrypt_password
69 return if password.blank?
70 self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
71 self.crypted_password = encrypt(password)
72 end
73
74 def password_required?
75 crypted_password.blank? || !password.blank?
76 end
77
78end
toggle raw diff

app/views/layouts/standard.html.erb

 
1313 <h2>Showing off RUR (<a href="http://blog.nanorails.com/rails-undo-redo">Rails Undo Redo</a>)</h2>
1414 </div>
1515
16 <% if flash[:notice] %>
17 <p class="message undo"><%= flash[:notice] %></p>
18 <% end %>
19
1620 <%= @content_for_layout %>
1721
1822<div id="clear"></div>
2727<p class="message"><%= undo_redo_links %></p>
2828<% end %>
2929
30<% if flash["notice"] %>
31<p class="message"><%= flash["notice"] %></p>
32<% end %>
33
3430<div id="footer">
3531Copyright 2008 <a href="http://blog.nanorails.com">nano RAILS</a> | Design by <a href="http://www.minimalistic-design.net">Minimalistic Design</a>
3632</div>
33<!-- Start of StatCounter Code -->
34<script type="text/javascript">
35var sc_project=3439485;
36var sc_invisible=1;
37var sc_partition=38;
38var sc_security="bab9070d";
39</script>
40
41<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/free_hit_counter.html"><img class="statcounter" src="http://c39.statcounter.com/3439485/0/bab9070d/1/" alt="counter" /></a></div></noscript>
42<!-- End of StatCounter Code -->
3743</body>
3844</html>
toggle raw diff

app/views/projects/edit.html.erb

 
2929 <ul>
3030 <li><%= link_to 'Home', root_path %></li>
3131 <li><%= link_to 'About', about_path %></li>
32 <% if logged_in? %>
33 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
34 <% end %>
3235 <li><%= link_to 'Project List', projects_path %></li>
3336 <li><%= link_to 'Show', @project %></li>
3437 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
toggle raw diff

app/views/projects/index.html.erb

 
2626 <ul>
2727 <li><%= link_to 'Home', root_path %></li>
2828 <li><%= link_to 'About', about_path %></li>
29 <% if logged_in? %>
30 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
31 <% end %>
2932 <li><%= link_to 'Project List', projects_path %></li>
3033 <li><%= link_to 'New project', new_project_path %></li>
3134 </ul>
toggle raw diff

app/views/projects/new.html.erb

 
2929 <ul>
3030 <li><%= link_to 'Home', root_path %></li>
3131 <li><%= link_to 'About', about_path %></li>
32 <% if logged_in? %>
33 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
34 <% end %>
3235 <li><%= link_to 'Project List', projects_path %></li>
3336 </ul>
3437</div>
toggle raw diff

app/views/projects/show.html.erb

 
2525 <ul>
2626 <li><%= link_to 'Home', root_path %></li>
2727 <li><%= link_to 'About', about_path %></li>
28 <% if logged_in? %>
29 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
30 <% end %>
2831 <li><%= link_to 'Project List', projects_path %></li>
2932 <li><%= link_to 'Edit', edit_project_path(@project) %></li>
3033 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
toggle raw diff

app/views/sessions/new.html.erb

 
1<div class="middle">
2 <h2>Login to your account</h2>
3 <% form_tag session_path do -%>
4 <p>
5 <b>Login</b><br/>
6 <%= text_field_tag 'login' %>
7 </p>
8
9 <p>
10 <b>Password</b><br/>
11 <%= password_field_tag 'password' %>
12 </p>
13
14 <p>
15 <b>Remember me</b>
16 <%= check_box_tag 'remember_me' %>
17 </p>
18
19 <p><%= submit_tag 'Log in' %></p>
20 <% end -%>
21</div>
22<div class="right">
23 <h2>Navigation</h2>
24 <ul>
25 <li><%= link_to 'Home', root_path %></li>
26 <li><%= link_to 'About', about_path %></li>
27 <% if logged_in? %>
28 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
29 <% else %>
30 <li><%= link_to 'Login', login_path %></li>
31 <li><%= link_to 'Sign Up', signup_path %></li>
32 <% end %>
33 </ul>
34</div>
toggle raw diff

app/views/tasks/edit.html.erb

 
2929 <ul>
3030 <li><%= link_to 'Home', root_path %></li>
3131 <li><%= link_to 'About', about_path %></li>
32 <% if logged_in? %>
33 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
34 <% end %>
3235 <li><%= link_to 'Project List', projects_path %></li>
3336 <li><%= link_to 'Show', project_task_path(@project, @task) %></li>
37 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
3438 <li><%= link_to 'Tasks', project_tasks_path(@project) %></li>
3539 <li><%= link_to 'Project', project_path(@project) %></li>
3640 </ul>
toggle raw diff

app/views/tasks/index.html.erb

 
2626 <ul>
2727 <li><%= link_to 'Home', root_path %></li>
2828 <li><%= link_to 'About', about_path %></li>
29 <% if logged_in? %>
30 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
31 <% end %>
2932 <li><%= link_to 'Project List', projects_path %></li>
3033 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
3134 <li><%= link_to 'Project', project_path(@project) %></li>
toggle raw diff

app/views/tasks/move.html.erb

 
1919 <ul>
2020 <li><%= link_to 'Home', root_path %></li>
2121 <li><%= link_to 'About', about_path %></li>
22 <% if logged_in? %>
23 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
24 <% end %>
2225 <li><%= link_to 'Project List', projects_path %></li>
2326 <li><%= link_to 'Show', project_task_path(@project, @task) %></li>
27 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
2428 <li><%= link_to 'Tasks', project_tasks_path(@project) %></li>
2529 <li><%= link_to 'Project', project_path(@project) %></li>
2630 </ul>
toggle raw diff

app/views/tasks/new.html.erb

 
2929 <ul>
3030 <li><%= link_to 'Home', root_path %></li>
3131 <li><%= link_to 'About', about_path %></li>
32 <% if logged_in? %>
33 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
34 <% end %>
3235 <li><%= link_to 'Project List', projects_path %></li>
3336 <li><%= link_to 'Tasks', project_tasks_path(@project) %></li>
3437 <li><%= link_to 'Project', project_path(@project) %></li>
toggle raw diff

app/views/tasks/show.html.erb

 
2626 <ul>
2727 <li><%= link_to 'Home', root_path %></li>
2828 <li><%= link_to 'About', about_path %></li>
29 <% if logged_in? %>
30 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
31 <% end %>
2932 <li><%= link_to 'Project List', projects_path %></li>
3033 <li><%= link_to 'Edit', edit_project_task_path(@project, @task) %></li>
3134 <li><%= link_to 'Move', move_project_task_path(@project, @task) %></li>
35 <li><%= link_to 'New task', new_project_task_path(@project) %></li>
3236 <li><%= link_to 'Tasks', project_tasks_path(@project) %></li>
3337 <li><%= link_to 'Project', project_path(@project) %></li>
3438 </ul>
toggle raw diff

app/views/users/new.html.erb

 
1<div class="middle">
2 <h2>Register for a new account</h2>
3 <%= error_messages_for :user %>
4 <% form_for :user, :url => users_path do |f| -%>
5 <p><label for="login">Login</label><br/>
6 <%= f.text_field :login %></p>
7
8 <p><label for="email">Email</label><br/>
9 <%= f.text_field :email %></p>
10
11 <p><label for="password">Password</label><br/>
12 <%= f.password_field :password %></p>
13
14 <p><label for="password_confirmation">Confirm Password</label><br/>
15 <%= f.password_field :password_confirmation %></p>
16
17 <p><%= submit_tag 'Sign up' %></p>
18 <% end -%>
19</div>
20
21<div class="right">
22 <h2>Navigation</h2>
23 <ul>
24 <li><%= link_to 'Home', root_path %></li>
25 <li><%= link_to 'About', about_path %></li>
26 <% if logged_in? %>
27 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
28 <% else %>
29 <li><%= link_to 'Login', login_path %></li>
30 <li><%= link_to 'Sign Up', signup_path %></li>
31 <% end %>
32 </ul>
33</div>
toggle raw diff

app/views/welcome/about.html.erb

 
11<div class="middle">
22 <h2>About Rails Undo Redo</h2>
3 <p>Just to fill out empty space in the template I decided to write this and to add one of my previous templates
4 here. There are direct links to view one of my previous templates live and live link to download it also ;)
5 Anyway hope you like both this one and previous one. You can see all of my templates at
6 <a href="http://www.minimalistic-design.net">Minimalistic design</a> live.</p>
3 <p>To learn more about how you can easily implement undo/redo for Active Record and your Ruby and Rails app, visit the Rails Undo Redo (RUR) <a href="http://blog.nanorails.com/rails-undo-redo">Project Page</a>.</p>
74</div>
85
96<div class="right">
88 <ul>
99 <li><%= link_to 'Home', root_path %></li>
1010 <li><%= link_to 'About', about_path %></li>
11 <% if logged_in? %>
12 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
13 <% else %>
14 <li><%= link_to 'Login', login_path %></li>
15 <li><%= link_to 'Sign Up', signup_path %></li>
16 <% end %>
1117 <li><%= link_to 'Project List', projects_path %></li>
1218 <li><%= link_to 'New project', new_project_path %></li>
1319 </ul>
toggle raw diff

app/views/welcome/index.html.erb

 
11<div class="middle">
22 <h2>Home</h2>
3 <p>Just to fill out empty space in the template I decided to write this and to add one of my previous templates
4 here. There are direct links to view one of my previous templates live and live link to download it also ;)
5 Anyway hope you like both this one and previous one. You can see all of my templates at
6 <a href="http://www.minimalistic-design.net">Minimalistic design</a> live.</p>
3 <p>This web application is an experiment to test a few ideas on how to implement Undo Redo for Rails. Give it a <%= link_to("test drive", signup_path) %>.</p>
4 <h2>Key ideas</h2>
5 <ul>
6 <li>Works across all models</li>
7 <li>Capture the list of changed objects (UndoRecord)</li>
8 <li>Group these UndoRecords per User Action (UndoAction)</li>
9 <li>undo and redo are just a simple matter or replaying the UndoRecord in the right order</li>
10 </ul>
11 <p>By using a Rails plugin (or very soon a gem as well), you can very easily transform any Rails Application using Active Record into a full fledged multi level undo/redo application like most desktop application.<p>
12 <br/>
13 <p>Notice: This is a demo application, and as such, do not use to store any data you would not want to lose</p>
714</div>
815
916<div class="right">
1818 <ul>
1919 <li><%= link_to 'Home', root_path %></li>
2020 <li><%= link_to 'About', about_path %></li>
21 <% if logged_in? %>
22 <li><%= link_to "Logout (#{h current_user.login})", logout_path %></li>
23 <% else %>
24 <li><%= link_to 'Login', login_path %></li>
25 <li><%= link_to 'Sign Up', signup_path %></li>
26 <% end %>
2127 <li><%= link_to 'Project List', projects_path %></li>
2228 <li><%= link_to 'New project', new_project_path %></li>
2329 </ul>
toggle raw diff

config/routes.rb

 
11ActionController::Routing::Routes.draw do |map|
2 map.resources :users
3
4 map.resource :session
5
26 map.resources :projects do |task|
37 task.resources :tasks, :member => { :move => :get, :move_to => :post }
48 end
4040 map.undo 'undo', :controller => "welcome", :action => "undo"
4141 map.redo 'redo', :controller => "welcome", :action => "redo"
4242
43 # authentication
44 map.signup '/signup', :controller => 'users', :action => 'new'
45 map.login '/login', :controller => 'sessions', :action => 'new'
46 map.logout '/logout', :controller => 'sessions', :action => 'destroy'
47
4348
4449 # Install the default routes as the lowest priority.
4550 map.connect ':controller/:action/:id'
toggle raw diff

db/migrate/002_create_projects.rb

 
44 t.string :title
55 t.string :notes
6