From 0a1fed570ec1041a6a0039c7f3965f8c7410c673 Mon Sep 17 00:00:00 2001 From: bsag Date: Sun, 15 Jan 2006 14:38:57 +0000 Subject: [PATCH] Changes to notification about whether login session has timed out and changes to improve compatibility with SQLite and PostgreSQL databases: * Set up notification of the session timing out. If you haven't checked the 'Keep logged in' checkbox at login, a method is run periodically (every 5 minutes) to check whether there is more than 10 minutes remaining on your session. When there's less than 10 minutes left, a red warning box appears dynamically at the top of the page appears to tell you that your session has timed out, and asking you to login again (with a link to the login page). This basically prevents the situation when you return to the browser window after more than an hour has elapsed, and try to add a new item without knowing that your session has timed out. * Changed the find methods that previously used 'done = 0' or 'done = 1' to test for truth or falsity instead. This means that it's compatible with both MySQL (which uses tinyint 0 or 1 values) and SQLite/SQLite3 and PostgreSQL (which use boolean 't' or 'f' values). By using true or false, ActiveRecord translates the values to the correct format depending on which database adapter is being used. git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@178 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/context_controller.rb | 10 +++---- tracks/app/controllers/login_controller.rb | 22 +++++++++++++++ tracks/app/controllers/project_controller.rb | 12 ++++---- tracks/app/controllers/todo_controller.rb | 2 +- tracks/app/models/context.rb | 8 +++--- tracks/app/models/project.rb | 6 ++-- tracks/app/models/todo.rb | 3 +- tracks/app/views/layouts/standard.rhtml | 5 ++++ tracks/app/views/login/check_expiry.rjs | 3 ++ .../app/views/shared/_add_new_item_form.rhtml | 1 + tracks/db/migrate/2_add_user_id.rb | 6 ++-- tracks/db/schema.rb | 28 +++++++++---------- tracks/public/stylesheets/standard.css | 6 ++-- 13 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 tracks/app/views/login/check_expiry.rjs diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/context_controller.rb index e1b653b8..c8a9cf8f 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/context_controller.rb @@ -60,7 +60,7 @@ class ContextController < ApplicationController @saved = @item.save @on_page = "context" if @saved - @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s + @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.context_id IN (?)", @user.id, false, @item.context_id]).size.to_s end return if request.xhr? @@ -91,7 +91,7 @@ class ContextController < ApplicationController @saved = @item.destroy if @saved - @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s + @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.context_id IN (?)", @user.id, false, @item.context_id]).size.to_s end return if request.xhr? @@ -123,8 +123,8 @@ class ContextController < ApplicationController @item.completed = Time.now() # For some reason, the before_save in todo.rb stopped working @saved = @item.save if @saved - @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s - @done_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1 and todos.context_id IN (?)", @user.id, @item.context_id]).size.to_s + @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.context_id IN (?)", @user.id, false, @item.context_id]).size.to_s + @done_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.context_id IN (?)", @user.id, true, @item.context_id]).size.to_s end return if request.xhr? @@ -221,7 +221,7 @@ class ContextController < ApplicationController @projects = @user.projects.collect { |x| x.done? ? nil:x }.compact @contexts = @user.contexts @todos = @user.todos - @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1", @user.id], :include => [:project], :order => "completed DESC") + @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ?", @user.id, true], :include => [:project], :order => "completed DESC") end def init_todos diff --git a/tracks/app/controllers/login_controller.rb b/tracks/app/controllers/login_controller.rb index 394077e2..98496635 100644 --- a/tracks/app/controllers/login_controller.rb +++ b/tracks/app/controllers/login_controller.rb @@ -76,5 +76,27 @@ class LoginController < ApplicationController def welcome end + + def check_expiry + # Gets called by periodically_call_remote to check whether + # the session has timed out yet + unless @session == nil + return if @controller_name == 'feed' or @session['noexpiry'] == "on" + # If the method is called by the feed controller + # (which we don't have under session control) + # or if we checked the box to keep logged in on login + # then the session is not going to get called + if @session + # Get expiry time (allow ten seconds window for the case where we have none) + expiry_time = @session['expiry_time'] || Time.now + 10 + @time_left = expiry_time - Time.now + if @time_left < (10*60) # Session will time out before the next check + @msg = "Session has timed out. Please " + else + @msg = "" + end + end + end + end end diff --git a/tracks/app/controllers/project_controller.rb b/tracks/app/controllers/project_controller.rb index 0d0bd3dc..12aaeb58 100644 --- a/tracks/app/controllers/project_controller.rb +++ b/tracks/app/controllers/project_controller.rb @@ -79,7 +79,7 @@ class ProjectController < ApplicationController @saved = @item.save @on_page = "project" if @saved - @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.project_id IN (?)", @user.id, @item.project_id]).size.to_s + @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s end return if request.xhr? @@ -111,7 +111,7 @@ class ProjectController < ApplicationController @saved = @item.destroy @on_page = "project" if @saved - @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.project_id IN (?)", @user.id, @item.project_id]).size.to_s + @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s end return if request.xhr? @@ -144,8 +144,8 @@ class ProjectController < ApplicationController @saved = @item.save @on_page = "project" if @saved - @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 0 and todos.project_id IN (?)", @user.id, @item.project_id]).size.to_s - @done_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1 and todos.project_id IN (?)", @user.id, @item.project_id]).size.to_s + @down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s + @done_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, true, @item.project_id]).size.to_s end return if request.xhr? @@ -249,10 +249,10 @@ class ProjectController < ApplicationController def init @user = @session['user'] - @projects = @user.projects.collect { |x| x.done? ? nil:x }.compact + @projects = @user.projects @contexts = @user.contexts @todos = @user.todos - @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1", @user.id], :include => [:project], :order => "completed DESC") + @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ?", @user.id, true], :include => [:project], :order => "completed DESC") end def init_todos diff --git a/tracks/app/controllers/todo_controller.rb b/tracks/app/controllers/todo_controller.rb index be54a23a..29441d34 100644 --- a/tracks/app/controllers/todo_controller.rb +++ b/tracks/app/controllers/todo_controller.rb @@ -208,7 +208,7 @@ class TodoController < ApplicationController @projects = @user.projects @contexts = @user.contexts @todos = @user.todos - @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1", @user.id], :include => [:project], :order => "completed DESC") + @done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ?", @user.id, true], :include => [:project], :order => "completed DESC") # for some reason, this generates an error about anil object under 0.14.2 #@done = @todos.collect { |x| x.done? ? x:nil }.compact.sort! {|x,y| y.completed <=> x.completed } end diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index 67b1483b..fa8725b1 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -12,18 +12,18 @@ class Context < ActiveRecord::Base validates_length_of :name, :maximum => 255, :message => "context name must be less than %d" validates_uniqueness_of :name, :message => "already exists", :scope => "user_id" - def self.list_of(hidden=0) + def self.list_of(hidden=false) find(:all, :conditions => [ "hide = ?" , hidden ], :order => "position ASC") end def find_not_done_todos - todos = Todo.find :all, :conditions => "todos.context_id = #{id} AND todos.done = 0", + todos = Todo.find :all, :conditions => ["todos.context_id = #{id} AND todos.done = ?", false], :include => [:context, :project], :order => "due IS NULL, due ASC, created_at ASC" end def find_done_todos - todos = Todo.find :all, :conditions => "todos.context_id = #{id} AND todos.done = 1", + todos = Todo.find :all, :conditions => ["todos.context_id = #{id} AND todos.done = ?", true], :include => [:context, :project], :order => "due IS NULL, due ASC, created_at ASC" end @@ -43,7 +43,7 @@ class Context < ActiveRecord::Base end def hidden? - self.hide == 1 + self.hide == true end end diff --git a/tracks/app/models/project.rb b/tracks/app/models/project.rb index 37d7e5b3..210f21d9 100644 --- a/tracks/app/models/project.rb +++ b/tracks/app/models/project.rb @@ -14,16 +14,16 @@ class Project < ActiveRecord::Base validates_uniqueness_of :name, :message => "already exists", :scope =>"user_id" def self.list_of(isdone=0) - find(:all, :conditions => [ "done = ?" , isdone ], :order => "position ASC") + find(:all, :conditions => [ "done = ?" , true ], :order => "position ASC") end def find_not_done_todos - todos = Todo.find :all, :conditions => "project_id = #{id} AND done = 0", + todos = Todo.find :all, :conditions => ["project_id = #{id} AND done = ?", false], :order => "due IS NULL, due ASC, created_at ASC" end def find_done_todos - todos = Todo.find :all, :conditions => "project_id = #{id} AND done = 1", + todos = Todo.find :all, :conditions => ["project_id = #{id} AND done = ?", true], :order => "due IS NULL, due ASC, created_at ASC" end diff --git a/tracks/app/models/todo.rb b/tracks/app/models/todo.rb index 22a65799..690d2e58 100644 --- a/tracks/app/models/todo.rb +++ b/tracks/app/models/todo.rb @@ -13,8 +13,7 @@ class Todo < ActiveRecord::Base validates_length_of :notes, :maximum => 60000 def self.not_done( id=id ) - self.find(:all, :conditions =>[ "done = 0 AND context_id = ?", id], \ - :order =>"due IS NULL, due ASC, created_at ASC") + self.find(:all, :conditions =>[ "done = ? AND context_id = ?", false, id], :order =>"due IS NULL, due ASC, created_at ASC") end end diff --git a/tracks/app/views/layouts/standard.rhtml b/tracks/app/views/layouts/standard.rhtml index 58b93200..ffa17e38 100644 --- a/tracks/app/views/layouts/standard.rhtml +++ b/tracks/app/views/layouts/standard.rhtml @@ -39,6 +39,11 @@
  • <%= link_to "Logout (#{@session['user']['login']}) »", :controller => "login", :action=>"logout"%>
  • +
    +<% unless @controller_name == 'feed' or @session['noexpiry'] == "on" -%> +<%= periodically_call_remote( :url => {:controller => "login", :action => "check_expiry"}, + :frequency => (5*60)) %> +<% end -%> <%= @content_for_layout %>