Introduce current_user and prefs accessors that replace the @user and @prefs variables that were being used in most places.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@575 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2007-07-30 05:29:18 +00:00
parent ad5fbc7147
commit 30c23fc560
18 changed files with 233 additions and 207 deletions

View file

@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base
helper :application helper :application
include LoginSystem include LoginSystem
helper_method :current_user, :prefs
layout proc{ |controller| controller.mobile? ? "mobile" : "standard" } layout proc{ |controller| controller.mobile? ? "mobile" : "standard" }
@ -94,12 +95,12 @@ class ApplicationController < ActionController::Base
count || 0 count || 0
end end
# Convert a date object to the format specified # Convert a date object to the format specified in the user's preferences
# in config/settings.yml # in config/settings.yml
# #
def format_date(date) def format_date(date)
if date if date
date_format = @user.prefs.date_format date_format = prefs.date_format
formatted_date = date.strftime("#{date_format}") formatted_date = date.strftime("#{date_format}")
else else
formatted_date = '' formatted_date = ''
@ -178,27 +179,27 @@ class ApplicationController < ActionController::Base
def parse_date_per_user_prefs( s ) def parse_date_per_user_prefs( s )
return nil if s.blank? return nil if s.blank?
Date.strptime(s, @user.prefs.date_format) Date.strptime(s, prefs.date_format)
end end
def init_data_for_sidebar def init_data_for_sidebar
@projects = @projects || @user.projects @projects = @projects || current_user.projects
@contexts = @contexts || @user.contexts @contexts = @contexts || current_user.contexts
init_not_done_counts init_not_done_counts
if @prefs.show_hidden_projects_in_sidebar if prefs.show_hidden_projects_in_sidebar
init_project_hidden_todo_counts(['project']) init_project_hidden_todo_counts(['project'])
end end
end end
def init_not_done_counts(parents = ['project','context']) def init_not_done_counts(parents = ['project','context'])
parents.each do |parent| parents.each do |parent|
eval("@#{parent}_not_done_counts = @#{parent}_not_done_counts || Todo.count(:conditions => ['user_id = ? and state = ?', @user.id, 'active'], :group => :#{parent}_id)") eval("@#{parent}_not_done_counts = @#{parent}_not_done_counts || Todo.count(:conditions => ['user_id = ? and state = ?', current_user.id, 'active'], :group => :#{parent}_id)")
end end
end end
def init_project_hidden_todo_counts(parents = ['project','context']) def init_project_hidden_todo_counts(parents = ['project','context'])
parents.each do |parent| parents.each do |parent|
eval("@#{parent}_project_hidden_todo_counts = @#{parent}_project_hidden_todo_counts || Todo.count(:conditions => ['user_id = ? and state = ?', @user.id, 'project_hidden'], :group => :#{parent}_id)") eval("@#{parent}_project_hidden_todo_counts = @#{parent}_project_hidden_todo_counts || Todo.count(:conditions => ['user_id = ? and state = ?', current_user.id, 'project_hidden'], :group => :#{parent}_id)")
end end
end end

View file

@ -10,7 +10,7 @@ class ContextsController < ApplicationController
session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) } session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) }
def index def index
@contexts = @user.contexts @contexts = current_user.contexts
init_not_done_counts(['context']) init_not_done_counts(['context'])
respond_to do |format| respond_to do |format|
format.html &render_contexts_html format.html &render_contexts_html
@ -46,7 +46,7 @@ class ContextsController < ApplicationController
render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400 render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400
return return
end end
@context = @user.contexts.build @context = current_user.contexts.build
params_are_invalid = true params_are_invalid = true
if (params['context'] || (params['request'] && params['request']['context'])) if (params['context'] || (params['request'] && params['request']['context']))
@context.attributes = params['context'] || params['request']['context'] @context.attributes = params['context'] || params['request']['context']
@ -104,7 +104,7 @@ class ContextsController < ApplicationController
# #
def order def order
params["list-contexts"].each_with_index do |id, position| params["list-contexts"].each_with_index do |id, position|
@user.contexts.update(id, :position => position + 1) current_user.contexts.update(id, :position => position + 1)
end end
render :nothing => true render :nothing => true
end end
@ -121,30 +121,31 @@ class ContextsController < ApplicationController
def render_contexts_rss_feed def render_contexts_rss_feed
lambda do lambda do
render_rss_feed_for @contexts, :feed => Context.feed_options(@user), render_rss_feed_for @contexts, :feed => feed_options,
:item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) } } :item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) } }
end end
end end
def render_contexts_atom_feed def render_contexts_atom_feed
lambda do lambda do
render_atom_feed_for @contexts, :feed => Context.feed_options(@user), render_atom_feed_for @contexts, :feed => feed_options,
:item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) }, :item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) },
:author => lambda { |c| nil } } :author => lambda { |c| nil } }
end end
end end
def feed_options
Context.feed_options(current_user)
end
def set_context_from_params def set_context_from_params
@context = @user.contexts.find_by_params(params) @context = current_user.contexts.find_by_params(params)
rescue rescue
@context = nil @context = nil
end end
def init def init
@source_view = params['_source_view'] || 'context' @source_view = params['_source_view'] || 'context'
# If we exclude completed projects, then we can't display them in the sidebar
# if the user sets the preference for them to be shown
# @projects = @user.projects.reject { |x| x.completed? }
init_data_for_sidebar init_data_for_sidebar
end end

View file

@ -17,10 +17,10 @@ class DataController < ApplicationController
def yaml_export def yaml_export
all_tables = {} all_tables = {}
all_tables['todos'] = @user.todos.find(:all) all_tables['todos'] = current_user.todos.find(:all)
all_tables['contexts'] = @user.contexts.find(:all) all_tables['contexts'] = current_user.contexts.find(:all)
all_tables['projects'] = @user.projects.find(:all) all_tables['projects'] = current_user.projects.find(:all)
all_tables['notes'] = @user.notes.find(:all) all_tables['notes'] = current_user.notes.find(:all)
result = all_tables.to_yaml result = all_tables.to_yaml
result.gsub!(/\n/, "\r\n") # TODO: general functionality for line endings result.gsub!(/\n/, "\r\n") # TODO: general functionality for line endings
@ -33,7 +33,7 @@ class DataController < ApplicationController
csv << ["ID", "Context", "Project", "Description", "Notes", csv << ["ID", "Context", "Project", "Description", "Notes",
"Created at", "Due", "Completed at", "User ID", "Show from", "Created at", "Due", "Completed at", "User ID", "Show from",
"state"] "state"]
@user.todos.find(:all, :include => [:context, :project]).each do |todo| current_user.todos.find(:all, :include => [:context, :project]).each do |todo|
# Format dates in ISO format for easy sorting in spreadsheet # Format dates in ISO format for easy sorting in spreadsheet
# Print context and project names for easy viewing # Print context and project names for easy viewing
csv << [todo.id, todo.context.name, csv << [todo.id, todo.context.name,
@ -57,7 +57,7 @@ class DataController < ApplicationController
"Created at", "Updated at"] "Created at", "Updated at"]
# had to remove project include because it's association order is leaking through # had to remove project include because it's association order is leaking through
# and causing an ambiguous column ref even with_exclusive_scope didn't seem to help -JamesKebinger # and causing an ambiguous column ref even with_exclusive_scope didn't seem to help -JamesKebinger
@user.notes.find(:all,:order=>"notes.created_at").each do |note| current_user.notes.find(:all,:order=>"notes.created_at").each do |note|
# Format dates in ISO format for easy sorting in spreadsheet # Format dates in ISO format for easy sorting in spreadsheet
# Print context and project names for easy viewing # Print context and project names for easy viewing
csv << [note.id, note.user_id, csv << [note.id, note.user_id,
@ -71,10 +71,10 @@ class DataController < ApplicationController
def xml_export def xml_export
result = "" result = ""
result << @user.todos.find(:all).to_xml result << current_user.todos.find(:all).to_xml
result << @user.contexts.find(:all).to_xml(:skip_instruct => true) result << current_user.contexts.find(:all).to_xml(:skip_instruct => true)
result << @user.projects.find(:all).to_xml(:skip_instruct => true) result << current_user.projects.find(:all).to_xml(:skip_instruct => true)
result << @user.notes.find(:all).to_xml(:skip_instruct => true) result << current_user.notes.find(:all).to_xml(:skip_instruct => true)
send_data(result, :filename => "tracks_backup.xml", :type => 'text/xml') send_data(result, :filename => "tracks_backup.xml", :type => 'text/xml')
end end

View file

@ -1,7 +1,7 @@
class NotesController < ApplicationController class NotesController < ApplicationController
def index def index
@all_notes = @user.notes @all_notes = current_user.notes
@page_title = "TRACKS::All notes" @page_title = "TRACKS::All notes"
respond_to do |format| respond_to do |format|
format.html format.html
@ -10,14 +10,12 @@ class NotesController < ApplicationController
end end
def show def show
@note = check_user_return_note @note = current_user.notes.find(params['id'])
@page_title = "TRACKS::Note " + @note.id.to_s @page_title = "TRACKS::Note " + @note.id.to_s
end end
# Add a new note to this project
#
def create def create
note = @user.notes.build note = current_user.notes.build
note.attributes = params["new_note"] note.attributes = params["new_note"]
if note.save if note.save
@ -28,34 +26,24 @@ class NotesController < ApplicationController
end end
def destroy def destroy
note = check_user_return_note note = current_user.notes.find(params['id'])
if note.destroy if note.destroy
render :text => '' render :text => ''
else else
notify :warning, "Couldn't delete note \"#{note.id.to_s}\"" notify :warning, "Couldn't delete note \"#{note.id}\""
render :text => '' render :text => ''
end end
end end
def update def update
note = check_user_return_note note = current_user.notes.find(params['id'])
note.attributes = params["note"] note.attributes = params["note"]
if note.save if note.save
render :partial => 'notes', :object => note render :partial => 'notes', :object => note
else else
notify :warning, "Couldn't update note \"#{note.id.to_s}\"" notify :warning, "Couldn't update note \"#{note.id}\""
render :text => '' render :text => ''
end end
end end
protected
def check_user_return_note
note = Note.find_by_id( params['id'] )
if @user == note.user
return note
else
render :text => ''
end
end
end end

View file

@ -2,20 +2,18 @@ class PreferencesController < ApplicationController
def index def index
@page_title = "TRACKS::Preferences" @page_title = "TRACKS::Preferences"
@prefs = @user.preference @prefs = prefs
end end
def edit def edit
@page_title = "TRACKS::Edit Preferences" @page_title = "TRACKS::Edit Preferences"
@prefs = @user.preference render :object => prefs
render :object => @prefs
end end
def update def update
user_success = @user.update_attributes(params['user']) user_updated = current_user.update_attributes(params['user'])
prefs_success = @user.preference.update_attributes(params['prefs']) prefs_updated = current_user.preference.update_attributes(params['prefs'])
if user_success && prefs_success if user_updated && prefs_updated
redirect_to :action => 'index' redirect_to :action => 'index'
else else
render :action => 'edit' render :action => 'edit'

View file

@ -9,8 +9,8 @@ class ProjectsController < ApplicationController
session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) } session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) }
def index def index
@projects = @user.projects @projects = current_user.projects
@contexts = @user.contexts @contexts = current_user.contexts
init_not_done_counts(['project']) init_not_done_counts(['project'])
if params[:only_active_with_no_next_actions] if params[:only_active_with_no_next_actions]
@projects = @projects.select { |p| p.active? && count_undone_todos(p) == 0 } @projects = @projects.select { |p| p.active? && count_undone_todos(p) == 0 }
@ -26,15 +26,15 @@ class ProjectsController < ApplicationController
def show def show
init_data_for_sidebar init_data_for_sidebar
@projects = @user.projects @projects = current_user.projects
@contexts = @user.contexts @contexts = current_user.contexts
@page_title = "TRACKS::Project: #{@project.name}" @page_title = "TRACKS::Project: #{@project.name}"
@not_done = @project.not_done_todos(:include_project_hidden_todos => true) @not_done = @project.not_done_todos(:include_project_hidden_todos => true)
@deferred = @project.deferred_todos @deferred = @project.deferred_todos
@done = @project.done_todos @done = @project.done_todos
@count = @not_done.size @count = @not_done.size
@next_project = @user.projects.next_from(@project) @next_project = current_user.projects.next_from(@project)
@previous_project = @user.projects.previous_from(@project) @previous_project = current_user.projects.previous_from(@project)
@default_project_context_name_map = build_default_project_context_name_map(@projects).to_json @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
respond_to do |format| respond_to do |format|
format.html format.html
@ -52,7 +52,7 @@ class ProjectsController < ApplicationController
render_failure "Expected post format is valid xml like so: <request><project><name>project name</name></project></request>." render_failure "Expected post format is valid xml like so: <request><project><name>project name</name></project></request>."
return return
end end
@project = @user.projects.build @project = current_user.projects.build
params_are_invalid = true params_are_invalid = true
if (params['project'] || (params['request'] && params['request']['project'])) if (params['project'] || (params['request'] && params['request']['project']))
@project.attributes = params['project'] || params['request']['project'] @project.attributes = params['project'] || params['request']['project']
@ -61,8 +61,8 @@ class ProjectsController < ApplicationController
@go_to_project = params['go_to_project'] @go_to_project = params['go_to_project']
@saved = @project.save @saved = @project.save
@project_not_done_counts = { @project.id => 0 } @project_not_done_counts = { @project.id => 0 }
@active_projects_count = @user.projects.count(:conditions => "state = 'active'") @active_projects_count = current_user.projects.count(:conditions => "state = 'active'")
@contexts = @user.contexts @contexts = current_user.contexts
respond_to do |format| respond_to do |format|
format.js format.js
format.xml do format.xml do
@ -101,10 +101,10 @@ class ProjectsController < ApplicationController
@project_not_done_counts = Hash.new @project_not_done_counts = Hash.new
@project_not_done_counts[@project.id] = @project.reload().not_done_todo_count(:include_project_hidden_todos => true) @project_not_done_counts[@project.id] = @project.reload().not_done_todo_count(:include_project_hidden_todos => true)
end end
@contexts = @user.contexts @contexts = current_user.contexts
@active_projects_count = @user.projects.count(:conditions => "state = 'active'") @active_projects_count = current_user.projects.count(:conditions => "state = 'active'")
@hidden_projects_count = @user.projects.count(:conditions => "state = 'hidden'") @hidden_projects_count = current_user.projects.count(:conditions => "state = 'hidden'")
@completed_projects_count = @user.projects.count(:conditions => "state = 'completed'") @completed_projects_count = current_user.projects.count(:conditions => "state = 'completed'")
render render
elsif boolean_param('update_status') elsif boolean_param('update_status')
render :action => 'update_status' render :action => 'update_status'
@ -121,9 +121,9 @@ class ProjectsController < ApplicationController
def destroy def destroy
@project.destroy @project.destroy
@active_projects_count = @user.projects.count(:conditions => "state = 'active'") @active_projects_count = current_user.projects.count(:conditions => "state = 'active'")
@hidden_projects_count = @user.projects.count(:conditions => "state = 'hidden'") @hidden_projects_count = current_user.projects.count(:conditions => "state = 'hidden'")
@completed_projects_count = @user.projects.count(:conditions => "state = 'completed'") @completed_projects_count = current_user.projects.count(:conditions => "state = 'completed'")
respond_to do |format| respond_to do |format|
format.js format.js
format.xml { render :text => "Deleted project #{@project.name}" } format.xml { render :text => "Deleted project #{@project.name}" }
@ -132,7 +132,7 @@ class ProjectsController < ApplicationController
def order def order
project_ids = params["list-active-projects"] || params["list-hidden-projects"] || params["list-completed-projects"] project_ids = params["list-active-projects"] || params["list-hidden-projects"] || params["list-completed-projects"]
projects = @user.projects.update_positions( project_ids ) projects = current_user.projects.update_positions( project_ids )
render :nothing => true render :nothing => true
rescue rescue
notify :error, $! notify :error, $!
@ -141,8 +141,8 @@ class ProjectsController < ApplicationController
def alphabetize def alphabetize
@state = params['state'] @state = params['state']
@projects = @user.projects.alphabetize(:state => @state) if @state @projects = current_user.projects.alphabetize(:state => @state) if @state
@contexts = @user.contexts @contexts = current_user.contexts
init_not_done_counts(['project']) init_not_done_counts(['project'])
end end
@ -157,26 +157,30 @@ class ProjectsController < ApplicationController
@completed_projects = @projects.select{ |p| p.completed? } @completed_projects = @projects.select{ |p| p.completed? }
@no_projects = @projects.empty? @no_projects = @projects.empty?
@projects.cache_note_counts @projects.cache_note_counts
@new_project = @user.projects.build @new_project = current_user.projects.build
render render
end end
end end
def render_rss_feed def render_rss_feed
lambda do lambda do
render_rss_feed_for @projects, :feed => Project.feed_options(@user), render_rss_feed_for @projects, :feed => feed_options,
:item => { :title => :name, :description => lambda { |p| summary(p) } } :item => { :title => :name, :description => lambda { |p| summary(p) } }
end end
end end
def render_atom_feed def render_atom_feed
lambda do lambda do
render_atom_feed_for @projects, :feed => Project.feed_options(@user), render_atom_feed_for @projects, :feed => feed_options,
:item => { :description => lambda { |p| summary(p) }, :item => { :description => lambda { |p| summary(p) },
:title => :name, :title => :name,
:author => lambda { |p| nil } } :author => lambda { |p| nil } }
end end
end end
def feed_options
Project.feed_options(current_user)
end
def render_text_feed def render_text_feed
lambda do lambda do
@ -186,7 +190,7 @@ class ProjectsController < ApplicationController
end end
def set_project_from_params def set_project_from_params
@project = @user.projects.find_by_params(params) @project = current_user.projects.find_by_params(params)
end end
def set_source_view def set_source_view

View file

@ -10,8 +10,8 @@ class TodosController < ApplicationController
session :off, :only => :index, :if => Proc.new { |req| is_feed_request(req) } session :off, :only => :index, :if => Proc.new { |req| is_feed_request(req) }
def index def index
@projects = @user.projects.find(:all, :include => [ :todos ]) @projects = current_user.projects.find(:all, :include => [ :todos ])
@contexts = @user.contexts.find(:all, :include => [ :todos ]) @contexts = current_user.contexts.find(:all, :include => [ :todos ])
@contexts_to_show = @contexts.reject {|x| x.hide? } @contexts_to_show = @contexts.reject {|x| x.hide? }
@ -27,15 +27,15 @@ class TodosController < ApplicationController
end end
def new def new
@projects = @user.projects.find(:all) @projects = current_user.projects.find(:all)
@contexts = @user.contexts.find(:all) @contexts = current_user.contexts.find(:all)
respond_to do |format| respond_to do |format|
format.m { render :action => "new_mobile" } format.m { render :action => "new_mobile" }
end end
end end
def create def create
@todo = @user.todos.build @todo = current_user.todos.build
p = params['request'] || params p = params['request'] || params
if p['todo']['show_from'] && !mobile? if p['todo']['show_from'] && !mobile?
@ -45,9 +45,9 @@ class TodosController < ApplicationController
@todo.attributes = p['todo'] @todo.attributes = p['todo']
if p['todo']['project_id'].blank? && !p['project_name'].blank? && p['project_name'] != 'None' if p['todo']['project_id'].blank? && !p['project_name'].blank? && p['project_name'] != 'None'
project = @user.projects.find_by_name(p['project_name'].strip) project = current_user.projects.find_by_name(p['project_name'].strip)
unless project unless project
project = @user.projects.build project = current_user.projects.build
project.name = p['project_name'].strip project.name = p['project_name'].strip
project.save project.save
@new_project_created = true @new_project_created = true
@ -56,9 +56,9 @@ class TodosController < ApplicationController
end end
if p['todo']['context_id'].blank? && !p['context_name'].blank? if p['todo']['context_id'].blank? && !p['context_name'].blank?
context = @user.contexts.find_by_name(p['context_name'].strip) context = current_user.contexts.find_by_name(p['context_name'].strip)
unless context unless context
context = @user.contexts.build context = current_user.contexts.build
context.name = p['context_name'].strip context.name = p['context_name'].strip
context.save context.save
@new_context_created = true @new_context_created = true
@ -75,7 +75,7 @@ class TodosController < ApplicationController
@saved = @todo.save @saved = @todo.save
if @saved if @saved
@todo.tag_with(params[:tag_list],@user) if params[:tag_list] @todo.tag_with(params[:tag_list], current_user) if params[:tag_list]
@todo.reload @todo.reload
end end
@ -97,15 +97,15 @@ class TodosController < ApplicationController
end end
def edit def edit
@projects = @user.projects.find(:all) @projects = current_user.projects.find(:all)
@contexts = @user.contexts.find(:all) @contexts = current_user.contexts.find(:all)
end end
def show def show
respond_to do |format| respond_to do |format|
format.m do format.m do
@projects = @user.projects.find(:all) @projects = current_user.projects.find(:all)
@contexts = @user.contexts.find(:all) @contexts = current_user.contexts.find(:all)
render :action => 'show_mobile' render :action => 'show_mobile'
end end
format.xml { render :xml => @todo.to_xml( :root => 'todo', :except => :user_id ) } format.xml { render :xml => @todo.to_xml( :root => 'todo', :except => :user_id ) }
@ -120,7 +120,7 @@ class TodosController < ApplicationController
respond_to do |format| respond_to do |format|
format.js do format.js do
if @saved if @saved
@remaining_undone_in_context = @user.contexts.find(@todo.context_id).not_done_todo_count @remaining_undone_in_context = current_user.contexts.find(@todo.context_id).not_done_todo_count
determine_down_count determine_down_count
determine_completed_count determine_completed_count
end end
@ -145,7 +145,7 @@ class TodosController < ApplicationController
end end
def update def update
@todo.tag_with(params[:tag_list],@user) if params[:tag_list] @todo.tag_with(params[:tag_list], current_user) if params[:tag_list]
@original_item_context_id = @todo.context_id @original_item_context_id = @todo.context_id
@original_item_project_id = @todo.project_id @original_item_project_id = @todo.project_id
@original_item_was_deferred = @todo.deferred? @original_item_was_deferred = @todo.deferred?
@ -153,9 +153,9 @@ class TodosController < ApplicationController
if params['project_name'] == 'None' if params['project_name'] == 'None'
project = Project.null_object project = Project.null_object
else else
project = @user.projects.find_by_name(params['project_name'].strip) project = current_user.projects.find_by_name(params['project_name'].strip)
unless project unless project
project = @user.projects.build project = current_user.projects.build
project.name = params['project_name'].strip project.name = params['project_name'].strip
project.save project.save
@new_project_created = true @new_project_created = true
@ -165,9 +165,9 @@ class TodosController < ApplicationController
end end
if params['todo']['context_id'].blank? && !params['context_name'].blank? if params['todo']['context_id'].blank? && !params['context_name'].blank?
context = @user.contexts.find_by_name(params['context_name'].strip) context = current_user.contexts.find_by_name(params['context_name'].strip)
unless context unless context
context = @user.contexts.build context = current_user.contexts.build
context.name = params['context_name'].strip context.name = params['context_name'].strip
context.save context.save
@new_context_created = true @new_context_created = true
@ -192,9 +192,9 @@ class TodosController < ApplicationController
@saved = @todo.update_attributes params["todo"] @saved = @todo.update_attributes params["todo"]
@context_changed = @original_item_context_id != @todo.context_id @context_changed = @original_item_context_id != @todo.context_id
@todo_was_activated_from_deferred_state = @original_item_was_deferred && @todo.active? @todo_was_activated_from_deferred_state = @original_item_was_deferred && @todo.active?
if @context_changed then @remaining_undone_in_context = @user.contexts.find(@original_item_context_id).not_done_todo_count; end if @context_changed then @remaining_undone_in_context = current_user.contexts.find(@original_item_context_id).not_done_todo_count; end
@project_changed = @original_item_project_id != @todo.project_id @project_changed = @original_item_project_id != @todo.project_id
if (@project_changed && !@original_item_project_id.nil?) then @remaining_undone_in_project = @user.projects.find(@original_item_project_id).not_done_todo_count; end if (@project_changed && !@original_item_project_id.nil?) then @remaining_undone_in_project = current_user.projects.find(@original_item_project_id).not_done_todo_count; end
determine_down_count determine_down_count
respond_to do |format| respond_to do |format|
format.js format.js
@ -231,7 +231,7 @@ class TodosController < ApplicationController
determine_down_count determine_down_count
source_view do |from| source_view do |from|
from.todo do from.todo do
@remaining_undone_in_context = @user.contexts.find(@context_id).not_done_todo_count @remaining_undone_in_context = current_user.contexts.find(@context_id).not_done_todo_count
end end
end end
end end
@ -245,27 +245,27 @@ class TodosController < ApplicationController
def completed def completed
@page_title = "TRACKS::Completed tasks" @page_title = "TRACKS::Completed tasks"
@done = @user.completed_todos @done = current_user.completed_todos
@done_today = @done.completed_within @user.time - 1.day @done_today = @done.completed_within current_user.time - 1.day
@done_this_week = @done.completed_within @user.time - 1.week @done_this_week = @done.completed_within current_user.time - 1.week
@done_this_month = @done.completed_within @user.time - 4.week @done_this_month = @done.completed_within current_user.time - 4.week
end end
def completed_archive def completed_archive
@page_title = "TRACKS::Archived completed tasks" @page_title = "TRACKS::Archived completed tasks"
@done = @user.completed_todos @done = current_user.completed_todos
@done_archive = @done.completed_more_than @user.time - 28.days @done_archive = @done.completed_more_than current_user.time - 28.days
end end
def list_deferred def list_deferred
@source_view = 'deferred' @source_view = 'deferred'
@page_title = "TRACKS::Tickler" @page_title = "TRACKS::Tickler"
@projects = @user.projects.find(:all, :include => [ :todos ]) @projects = current_user.projects.find(:all, :include => [ :todos ])
@contexts_to_show = @contexts = @user.contexts.find(:all, :include => [ :todos ]) @contexts_to_show = @contexts = current_user.contexts.find(:all, :include => [ :todos ])
@user.deferred_todos.find_and_activate_ready current_user.deferred_todos.find_and_activate_ready
@not_done_todos = @user.deferred_todos @not_done_todos = current_user.deferred_todos
@count = @not_done_todos.size @count = @not_done_todos.size
@default_project_context_name_map = build_default_project_context_name_map(@projects).to_json @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
end end
@ -273,7 +273,7 @@ class TodosController < ApplicationController
# Check for any due tickler items, activate them # Check for any due tickler items, activate them
# Called by periodically_call_remote # Called by periodically_call_remote
def check_deferred def check_deferred
@due_tickles = @user.deferred_todos.find_and_activate_ready @due_tickles = current_user.deferred_todos.find_and_activate_ready
respond_to do |format| respond_to do |format|
format.html { redirect_to home_path } format.html { redirect_to home_path }
format.js format.js
@ -281,12 +281,12 @@ class TodosController < ApplicationController
end end
def filter_to_context def filter_to_context
context = @user.contexts.find(params['context']['id']) context = current_user.contexts.find(params['context']['id'])
redirect_to formatted_context_todos_path(context, :m) redirect_to formatted_context_todos_path(context, :m)
end end
def filter_to_project def filter_to_project
project = @user.projects.find(params['project']['id']) project = current_user.projects.find(params['project']['id'])
redirect_to formatted_project_todos_path(project, :m) redirect_to formatted_project_todos_path(project, :m)
end end
@ -303,19 +303,19 @@ class TodosController < ApplicationController
@not_done_todos = [] @not_done_todos = []
else else
tag_collection = Tag.find_by_name(tag_name).todos tag_collection = Tag.find_by_name(tag_name).todos
@not_done_todos = tag_collection.find(:all, :conditions => ['taggings.user_id = ? and state = ?', @user.id, 'active']) @not_done_todos = tag_collection.find(:all, :conditions => ['taggings.user_id = ? and state = ?', current_user.id, 'active'])
end end
@contexts = @user.contexts.find(:all, :include => [ :todos ]) @contexts = current_user.contexts.find(:all, :include => [ :todos ])
@contexts_to_show = @contexts.reject {|x| x.hide? } @contexts_to_show = @contexts.reject {|x| x.hide? }
@deferred = tag_collection.find(:all, :conditions => ['taggings.user_id = ? and state = ?', @user.id, 'deferred']) @deferred = tag_collection.find(:all, :conditions => ['taggings.user_id = ? and state = ?', current_user.id, 'deferred'])
@page_title = "TRACKS::Tagged with \'#{@tag}\'" @page_title = "TRACKS::Tagged with \'#{@tag}\'"
# If you've set no_completed to zero, the completed items box # If you've set no_completed to zero, the completed items box
# isn't shown on the home page # isn't shown on the home page
max_completed = @user.prefs.show_number_completed max_completed = current_user.prefs.show_number_completed
@done = tag_collection.find(:all, :limit => max_completed, :conditions => ['taggings.user_id = ? and state = ?', @user.id, 'completed']) @done = tag_collection.find(:all, :limit => max_completed, :conditions => ['taggings.user_id = ? and state = ?', current_user.id, 'completed'])
# Set count badge to number of items with this tag # Set count badge to number of items with this tag
@not_done_todos.empty? ? @count = 0 : @count = @not_done_todos.size @not_done_todos.empty? ? @count = 0 : @count = @not_done_todos.size
@default_project_context_name_map = build_default_project_context_name_map(@projects).to_json @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
@ -325,7 +325,7 @@ class TodosController < ApplicationController
private private
def get_todo_from_params def get_todo_from_params
@todo = @user.todos.find(params['id']) @todo = current_user.todos.find(params['id'])
end end
def init def init
@ -353,7 +353,7 @@ class TodosController < ApplicationController
if params.key?('due') if params.key?('due')
due_within = params['due'].to_i due_within = params['due'].to_i
due_within_when = @user.time + due_within.days due_within_when = current_user.time + due_within.days
condition_builder.add('todos.due <= ?', due_within_when) condition_builder.add('todos.due <= ?', due_within_when)
due_within_date_s = due_within_when.strftime("%Y-%m-%d") due_within_date_s = due_within_when.strftime("%Y-%m-%d")
@title << " due today" if (due_within == 0) @title << " due today" if (due_within == 0)
@ -363,7 +363,7 @@ class TodosController < ApplicationController
if params.key?('done') if params.key?('done')
done_in_last = params['done'].to_i done_in_last = params['done'].to_i
condition_builder.add('todos.completed_at >= ?', @user.time - done_in_last.days) condition_builder.add('todos.completed_at >= ?', current_user.time - done_in_last.days)
@title << " actions completed" @title << " actions completed"
@description << " in the last #{done_in_last.to_s} days" @description << " in the last #{done_in_last.to_s} days"
end end
@ -376,12 +376,12 @@ class TodosController < ApplicationController
def with_parent_resource_scope(&block) def with_parent_resource_scope(&block)
if (params[:context_id]) if (params[:context_id])
@context = @user.contexts.find_by_params(params) @context = current_user.contexts.find_by_params(params)
Todo.with_scope :find => {:conditions => ['todos.context_id = ?', @context.id]} do Todo.with_scope :find => {:conditions => ['todos.context_id = ?', @context.id]} do
yield yield
end end
elsif (params[:project_id]) elsif (params[:project_id])
@project = @user.projects.find_by_params(params) @project = current_user.projects.find_by_params(params)
Todo.with_scope :find => {:conditions => ['todos.project_id = ?', @project.id]} do Todo.with_scope :find => {:conditions => ['todos.project_id = ?', @project.id]} do
yield yield
end end
@ -414,23 +414,23 @@ class TodosController < ApplicationController
if mobile? if mobile?
@todos, @page = @user.todos.paginate(:all, @todos, @page = current_user.todos.paginate(:all,
:conditions => ['state = ?', 'active' ], :include => [:context], :conditions => ['state = ?', 'active' ], :include => [:context],
:order => 'due IS NULL, due ASC, todos.created_at ASC', :order => 'due IS NULL, due ASC, todos.created_at ASC',
:page => params[:page], :per_page => @prefs.mobile_todos_per_page) :page => params[:page], :per_page => prefs.mobile_todos_per_page)
@pagination_params = { :format => :m } @pagination_params = { :format => :m }
@pagination_params[:context_id] = @context.to_param if @context @pagination_params[:context_id] = @context.to_param if @context
@pagination_params[:project_id] = @project.to_param if @project @pagination_params[:project_id] = @project.to_param if @project
else else
# Note: these next two finds were previously using @users.todos.find but that broke with_scope for :limit # Note: these next two finds were previously using current_users.todos.find but that broke with_scope for :limit
# Exclude hidden projects from count on home page # Exclude hidden projects from count on home page
@todos = Todo.find(:all, :conditions => ['todos.user_id = ? and todos.state = ? or todos.state = ?', @user.id, 'active', 'completed'], :include => [ :project, :context, :tags ]) @todos = Todo.find(:all, :conditions => ['todos.user_id = ? and todos.state = ? or todos.state = ?', current_user.id, 'active', 'completed'], :include => [ :project, :context, :tags ])
# Exclude hidden projects from the home page # Exclude hidden projects from the home page
@not_done_todos = Todo.find(:all, :conditions => ['todos.user_id = ? and todos.state = ?', @user.id, 'active'], :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", :include => [ :project, :context, :tags ]) @not_done_todos = Todo.find(:all, :conditions => ['todos.user_id = ? and todos.state = ?', current_user.id, 'active'], :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", :include => [ :project, :context, :tags ])
end end
@ -442,19 +442,19 @@ class TodosController < ApplicationController
def determine_down_count def determine_down_count
source_view do |from| source_view do |from|
from.todo do from.todo do
@down_count = Todo.count_by_sql(['SELECT COUNT(*) FROM todos, contexts WHERE todos.context_id = contexts.id and todos.user_id = ? and todos.state = ? and contexts.hide = ?', @user.id, 'active', false]) @down_count = Todo.count_by_sql(['SELECT COUNT(*) FROM todos, contexts WHERE todos.context_id = contexts.id and todos.user_id = ? and todos.state = ? and contexts.hide = ?', current_user.id, 'active', false])
end end
from.context do from.context do
@down_count = @user.contexts.find(@todo.context_id).not_done_todo_count @down_count = current_user.contexts.find(@todo.context_id).not_done_todo_count
end end
from.project do from.project do
unless @todo.project_id == nil unless @todo.project_id == nil
@down_count = @user.projects.find(@todo.project_id).not_done_todo_count @down_count = current_user.projects.find(@todo.project_id).not_done_todo_count
@deferred_count = @user.projects.find(@todo.project_id).deferred_todo_count @deferred_count = current_user.projects.find(@todo.project_id).deferred_todo_count
end end
end end
from.deferred do from.deferred do
@down_count = @user.todos.count_in_state(:deferred) @down_count = current_user.todos.count_in_state(:deferred)
end end
end end
end end
@ -462,14 +462,14 @@ class TodosController < ApplicationController
def determine_completed_count def determine_completed_count
source_view do |from| source_view do |from|
from.todo do from.todo do
@completed_count = Todo.count_by_sql(['SELECT COUNT(*) FROM todos, contexts WHERE todos.context_id = contexts.id and todos.user_id = ? and todos.state = ? and contexts.hide = ?', @user.id, 'completed', false]) @completed_count = Todo.count_by_sql(['SELECT COUNT(*) FROM todos, contexts WHERE todos.context_id = contexts.id and todos.user_id = ? and todos.state = ? and contexts.hide = ?', current_user.id, 'completed', false])
end end
from.context do from.context do
@completed_count = @user.contexts.find(@todo.context_id).done_todo_count @completed_count = current_user.contexts.find(@todo.context_id).done_todo_count
end end
from.project do from.project do
unless @todo.project_id == nil unless @todo.project_id == nil
@completed_count = @user.projects.find(@todo.project_id).done_todo_count @completed_count = current_user.projects.find(@todo.project_id).done_todo_count
end end
end end
end end
@ -481,8 +481,8 @@ class TodosController < ApplicationController
# If you've set no_completed to zero, the completed items box # If you've set no_completed to zero, the completed items box
# isn't shown on the home page # isn't shown on the home page
max_completed = @user.prefs.show_number_completed max_completed = current_user.prefs.show_number_completed
@done = @user.completed_todos.find(:all, :limit => max_completed, :include => [ :context, :project, :tags ]) unless max_completed == 0 @done = current_user.completed_todos.find(:all, :limit => max_completed, :include => [ :context, :project, :tags ]) unless max_completed == 0
# Set count badge to number of not-done, not hidden context items # Set count badge to number of not-done, not hidden context items
@count = @todos.reject { |x| !x.active? || x.context.hide? }.size @count = @todos.reject { |x| !x.active? || x.context.hide? }.size
@ -512,7 +512,7 @@ class TodosController < ApplicationController
def render_rss_feed def render_rss_feed
lambda do lambda do
render_rss_feed_for @todos, :feed => Todo.feed_options(@user), render_rss_feed_for @todos, :feed => todo_feed_options,
:item => { :item => {
:title => :description, :title => :description,
:link => lambda { |t| context_url(t.context) }, :link => lambda { |t| context_url(t.context) },
@ -520,6 +520,10 @@ class TodosController < ApplicationController
} }
end end
end end
def todo_feed_options
Todo.feed_options(current_user)
end
def todo_feed_content def todo_feed_content
lambda do |i| lambda do |i|
@ -538,7 +542,7 @@ class TodosController < ApplicationController
def render_atom_feed def render_atom_feed
lambda do lambda do
render_atom_feed_for @todos, :feed => Todo.feed_options(@user), render_atom_feed_for @todos, :feed => todo_feed_options,
:item => { :item => {
:title => :description, :title => :description,
:link => lambda { |t| context_url(t.context) }, :link => lambda { |t| context_url(t.context) },

View file

@ -2,7 +2,7 @@
module ApplicationHelper module ApplicationHelper
def user_time def user_time
@user.time current_user.time
end end
# Replicates the link_to method but also checks request.request_uri to find # Replicates the link_to method but also checks request.request_uri to find
@ -47,7 +47,7 @@ module ApplicationHelper
"<a title='#{format_date(due)}'><span class=\"amber\">Due Tomorrow</span></a> " "<a title='#{format_date(due)}'><span class=\"amber\">Due Tomorrow</span></a> "
# due 2-7 days away # due 2-7 days away
when 2..7 when 2..7
if @user.prefs.due_style == Preference::DUE_ON_DUE_STYLE if prefs.due_style == Preference::DUE_ON_DUE_STYLE
"<a title='#{format_date(due)}'><span class=\"orange\">Due on #{due.strftime("%A")}</span></a> " "<a title='#{format_date(due)}'><span class=\"orange\">Due on #{due.strftime("%A")}</span></a> "
else else
"<a title='#{format_date(due)}'><span class=\"orange\">Due in #{pluralize(days, 'day')}</span></a> " "<a title='#{format_date(due)}'><span class=\"orange\">Due in #{pluralize(days, 'day')}</span></a> "
@ -114,13 +114,13 @@ module ApplicationHelper
def item_link_to_context(item) def item_link_to_context(item)
descriptor = "[C]" descriptor = "[C]"
descriptor = "[#{item.context.name}]" if (@user.prefs.verbose_action_descriptors) descriptor = "[#{item.context.name}]" if prefs.verbose_action_descriptors
link_to_context( item.context, descriptor ) link_to_context( item.context, descriptor )
end end
def item_link_to_project(item) def item_link_to_project(item)
descriptor = "[P]" descriptor = "[P]"
descriptor = "[#{item.project.name}]" if (@user.prefs.verbose_action_descriptors) descriptor = "[#{item.project.name}]" if prefs.verbose_action_descriptors
link_to_project( item.project, descriptor ) link_to_project( item.project, descriptor )
end end

View file

@ -2,21 +2,28 @@ module FeedlistHelper
def rss_formatted_link(options = {}) def rss_formatted_link(options = {})
image_tag = image_tag("feed-icon.png", :size => "16X16", :border => 0, :class => "rss-icon") image_tag = image_tag("feed-icon.png", :size => "16X16", :border => 0, :class => "rss-icon")
linkoptions = { :token => @user.token, :format => 'rss' } linkoptions = merge_hashes( { :format => 'rss'}, user_token_hash, options)
linkoptions.merge!(options)
link_to(image_tag, linkoptions, :title => "RSS feed") link_to(image_tag, linkoptions, :title => "RSS feed")
end end
def text_formatted_link(options = {}) def text_formatted_link(options = {})
linkoptions = { :token => @user.token, :format => 'txt' } linkoptions = merge_hashes( { :format => 'txt'}, user_token_hash, options)
linkoptions.merge!(options)
link_to('<span class="feed">TXT</span>', linkoptions, :title => "Plain text feed" ) link_to('<span class="feed">TXT</span>', linkoptions, :title => "Plain text feed" )
end end
def ical_formatted_link(options = {}) def ical_formatted_link(options = {})
linkoptions = { :token => @user.token, :format => 'ics' } linkoptions = merge_hashes ( { :format => 'ics'}, user_token_hash, options)
linkoptions.merge!(options)
link_to('<span class="feed">iCal</span>', linkoptions, :title => "iCal feed" ) link_to('<span class="feed">iCal</span>', linkoptions, :title => "iCal feed" )
end end
protected
def merge_hashes(*hashes)
hashes.inject(Hash.new){ |result, h| result.merge(h) }
end
def user_token_hash
{ :token => current_user.token }
end
end end

View file

@ -107,11 +107,11 @@ module TodosHelper
def staleness_class(item) def staleness_class(item)
if item.due || item.completed? if item.due || item.completed?
return "" return ""
elsif item.created_at < user_time - (@user.prefs.staleness_starts * 3).days elsif item.created_at < user_time - (prefs.staleness_starts * 3).days
return " stale_l3" return " stale_l3"
elsif item.created_at < user_time - (@user.prefs.staleness_starts * 2).days elsif item.created_at < user_time - (prefs.staleness_starts * 2).days
return " stale_l2" return " stale_l2"
elsif item.created_at < user_time - (@user.prefs.staleness_starts).days elsif item.created_at < user_time - (prefs.staleness_starts).days
return " stale_l1" return " stale_l1"
else else
return "" return ""
@ -138,7 +138,7 @@ module TodosHelper
"<a title='" + format_date(due) + "'><span class=\"amber\">Show Tomorrow</span></a> " "<a title='" + format_date(due) + "'><span class=\"amber\">Show Tomorrow</span></a> "
# due 2-7 days away # due 2-7 days away
when 2..7 when 2..7
if @user.prefs.due_style == Preference::DUE_ON_DUE_STYLE if prefs.due_style == Preference::DUE_ON_DUE_STYLE
"<a title='" + format_date(due) + "'><span class=\"orange\">Show on " + due.strftime("%A") + "</span></a> " "<a title='" + format_date(due) + "'><span class=\"orange\">Show on " + due.strftime("%A") + "</span></a> "
else else
"<a title='" + format_date(due) + "'><span class=\"orange\">Show in " + days.to_s + " days</span></a> " "<a title='" + format_date(due) + "'><span class=\"orange\">Show in " + days.to_s + " days</span></a> "
@ -150,10 +150,8 @@ module TodosHelper
end end
def calendar_setup( input_field ) def calendar_setup( input_field )
date_format = @user.prefs.date_format str = "Calendar.setup({ ifFormat:\"#{prefs.date_format}\""
week_starts = @user.prefs.week_starts str << ",firstDay:#{prefs.week_starts},showOthers:true,range:[2004, 2010]"
str = "Calendar.setup({ ifFormat:\"#{date_format}\""
str << ",firstDay:#{week_starts},showOthers:true,range:[2004, 2010]"
str << ",step:1,inputField:\"" + input_field + "\",cache:true,align:\"TR\" })\n" str << ",step:1,inputField:\"" + input_field + "\",cache:true,align:\"TR\" })\n"
javascript_tag str javascript_tag str
end end

View file

@ -1,6 +1,6 @@
<div id="display_box"> <div id="display_box">
<%= render :partial => "contexts/context", :locals => { :context => @context, :collapsible => false } %> <%= render :partial => "contexts/context", :locals => { :context => @context, :collapsible => false } %>
<%= render :partial => "todos/completed", :locals => { :done => @done, :collapsible => false, :append_descriptor => "in this context (last #{@user.prefs.show_number_completed})" } %> <%= render :partial => "todos/completed", :locals => { :done => @done, :collapsible => false, :append_descriptor => "in this context (last #{prefs.show_number_completed})" } %>
</div><!-- [end:display_box] --> </div><!-- [end:display_box] -->

View file

@ -17,7 +17,7 @@
<%= javascript_include_tag "protoload" %> <%= javascript_include_tag "protoload" %>
<link rel="shortcut icon" href="<%= url_for(:controller => 'favicon.ico') %>" /> <link rel="shortcut icon" href="<%= url_for(:controller => 'favicon.ico') %>" />
<%= auto_discovery_link_tag(:rss,{:controller => "todos", :action => "index", :format => 'rss', :token => "#{@user.token}"}, {:title => "RSS feed of next actions"}) %> <%= auto_discovery_link_tag(:rss, {:controller => "todos", :action => "index", :format => 'rss', :token => "#{current_user.token}"}, {:title => "RSS feed of next actions"}) %>
<script type="text/javascript"> <script type="text/javascript">
window.onload=function(){ window.onload=function(){
@ -44,7 +44,7 @@ window.onload=function(){
page.select('.notes').each { |e| e.toggle } page.select('.notes').each { |e| e.toggle }
end end
-%>&nbsp;|&nbsp; -%>&nbsp;|&nbsp;
<%= link_to "Logout (#{@user.display_name}) »", logout_path %> <%= link_to "Logout (#{current_user.display_name}) »", logout_path %>
</div> </div>
<div id="navcontainer"> <div id="navcontainer">
@ -57,7 +57,7 @@ window.onload=function(){
<li><%= navigation_link( "Notes", notes_path, {:accesskey => "o", :title => "Show all notes"} ) %></li> <li><%= navigation_link( "Notes", notes_path, {:accesskey => "o", :title => "Show all notes"} ) %></li>
<li><%= navigation_link( "Preferences", preferences_path, {:accesskey => "u", :title => "Show my preferences"} ) %></li> <li><%= navigation_link( "Preferences", preferences_path, {:accesskey => "u", :title => "Show my preferences"} ) %></li>
<li><%= navigation_link( "Import/Export", {:controller => "data", :action => "index"}, {:accesskey => "i", :title => "Import and export data"} ) %></li> <li><%= navigation_link( "Import/Export", {:controller => "data", :action => "index"}, {:accesskey => "i", :title => "Import and export data"} ) %></li>
<% if @user.is_admin? -%> <% if current_user.is_admin? -%>
<li><%= navigation_link("Admin", users_path, {:accesskey => "a", :title => "Add or delete users"} ) %></li> <li><%= navigation_link("Admin", users_path, {:accesskey => "a", :title => "Add or delete users"} ) %></li>
<% end -%> <% end -%>
<li><%= navigation_link(image_tag("feed-icon.png", :size => "16X16", :border => 0), {:controller => "feedlist", :action => "index"}, :title => "See a list of available feeds" ) %></li> <li><%= navigation_link(image_tag("feed-icon.png", :size => "16X16", :border => 0), {:controller => "feedlist", :action => "index"}, :title => "See a list of available feeds" ) %></li>

View file

@ -11,7 +11,7 @@
<li><strong>show completed projects in sidebar:</strong> whether or not projects marked as complete are shown in the sidebar on the home page and elsewhere</li> <li><strong>show completed projects in sidebar:</strong> whether or not projects marked as complete are shown in the sidebar on the home page and elsewhere</li>
<li><strong>show hidden contexts in sidebar:</strong> whether or not contexts marked as hidden are shown in the sidebar on the home page and elsewhere</li> <li><strong>show hidden contexts in sidebar:</strong> whether or not contexts marked as hidden are shown in the sidebar on the home page and elsewhere</li>
<li><strong>show project on todo done:</strong> whether or not to redirect to the project page when an action associated with a project is marked complete</li> <li><strong>show project on todo done:</strong> whether or not to redirect to the project page when an action associated with a project is marked complete</li>
<% if @user.is_admin? %> <% if current_user.is_admin? %>
<li><strong>admin email:</strong> email address for the admin user of Tracks (displayed on the signup page for users to contact to obtain an account)</li> <li><strong>admin email:</strong> email address for the admin user of Tracks (displayed on the signup page for users to contact to obtain an account)</li>
<% end %> <% end %>
<li><strong>staleness starts:</strong> the number of days before items with no due date get marked as stale (with a yellow highlight)</li> <li><strong>staleness starts:</strong> the number of days before items with no due date get marked as stale (with a yellow highlight)</li>
@ -61,7 +61,7 @@
<%= row_with_select_field("show_hidden_contexts_in_sidebar") %> <%= row_with_select_field("show_hidden_contexts_in_sidebar") %>
<%= row_with_select_field("show_project_on_todo_done") %> <%= row_with_select_field("show_project_on_todo_done") %>
<% if @user.is_admin? %> <%= row_with_text_field('admin_email') %> <% end %> <% if current_user.is_admin? %> <%= row_with_text_field('admin_email') %> <% end %>
<%= row_with_text_field('staleness_starts', true) %> <%= row_with_text_field('staleness_starts', true) %>
<%= row_with_text_field('show_number_completed') %> <%= row_with_text_field('show_number_completed') %>
<%= row_with_text_field('refresh') %> <%= row_with_text_field('refresh') %>

View file

@ -3,31 +3,31 @@
<h2>Your preferences</h2> <h2>Your preferences</h2>
<ul id="prefs"> <ul id="prefs">
<li>First name: <span class="highlight"><%= @user.first_name %></span></li> <li>First name: <span class="highlight"><%= current_user.first_name %></span></li>
<li>Last name: <span class="highlight"><%= @user.last_name %></span></li> <li>Last name: <span class="highlight"><%= current_user.last_name %></span></li>
<li>Date format: <span class="highlight"><%= @prefs.date_format %></span> Your current date: <%= format_date(user_time) %></li> <li>Date format: <span class="highlight"><%= prefs.date_format %></span> Your current date: <%= format_date(user_time) %></li>
<li>Title date format: <span class="highlight"><%= @prefs.title_date_format %></span> Your current title date: <%= user_time.strftime(@prefs.title_date_format) %></li> <li>Title date format: <span class="highlight"><%= prefs.title_date_format %></span> Your current title date: <%= user_time.strftime(prefs.title_date_format) %></li>
<li>Time zone: <span class="highlight"><%= @prefs.tz %></span> Your current time: <%= user_time.strftime('%I:%M %p') %></li> <li>Time zone: <span class="highlight"><%= prefs.tz %></span> Your current time: <%= user_time.strftime('%I:%M %p') %></li>
<li>Week starts on: <span class="highlight"><%= Preference.day_number_to_name_map[@prefs.week_starts] %></span></li> <li>Week starts on: <span class="highlight"><%= Preference.day_number_to_name_map[prefs.week_starts] %></span></li>
<li>Show the last <span class="highlight"><%= @prefs.show_number_completed %></span> completed items on the home page</li> <li>Show the last <span class="highlight"><%= prefs.show_number_completed %></span> completed items on the home page</li>
<li>Show completed projects in sidebar: <span class="highlight"><%= @prefs.show_completed_projects_in_sidebar %></span></li> <li>Show completed projects in sidebar: <span class="highlight"><%= prefs.show_completed_projects_in_sidebar %></span></li>
<li>Show hidden projects in sidebar: <span class="highlight"><%= @prefs.show_hidden_projects_in_sidebar %></span></li> <li>Show hidden projects in sidebar: <span class="highlight"><%= prefs.show_hidden_projects_in_sidebar %></span></li>
<li>Show hidden contexts in sidebar: <span class="highlight"><%= @prefs.show_hidden_contexts_in_sidebar %></span></li> <li>Show hidden contexts in sidebar: <span class="highlight"><%= prefs.show_hidden_contexts_in_sidebar %></span></li>
<li>Go to project page on todo complete: <span class="highlight"><%= @prefs.show_project_on_todo_done %></span></li> <li>Go to project page on todo complete: <span class="highlight"><%= prefs.show_project_on_todo_done %></span></li>
<li>Staleness starts after <span class="highlight"><%= @prefs.staleness_starts %></span> days</li> <li>Staleness starts after <span class="highlight"><%= prefs.staleness_starts %></span> days</li>
<li>Due style: <span class="highlight"> <li>Due style: <span class="highlight">
<% if @prefs.due_style == Preference::DUE_IN_N_DAYS_DUE_STYLE %> <% if prefs.due_style == Preference::DUE_IN_N_DAYS_DUE_STYLE %>
Due in ___ days Due in ___ days
<% else %> <% else %>
Due on ________ Due on ________
<% end %> <% end %>
</span></li> </span></li>
<% if @user.is_admin? %> <% if current_user.is_admin? %>
<li>Admin email: <span class="highlight"><%= @prefs.admin_email %></span></li> <li>Admin email: <span class="highlight"><%= prefs.admin_email %></span></li>
<% end %> <% end %>
<li>Refresh interval (in minutes): <span class="highlight"><%= @prefs.refresh %></span></li> <li>Refresh interval (in minutes): <span class="highlight"><%= prefs.refresh %></span></li>
<li>Verbose action descriptors: <span class="highlight"><%= @prefs.verbose_action_descriptors %></span></li> <li>Verbose action descriptors: <span class="highlight"><%= prefs.verbose_action_descriptors %></span></li>
<li>Actions per page (Mobile View): <span class="highlight"><%= @prefs.mobile_todos_per_page %></span></li> <li>Actions per page (Mobile View): <span class="highlight"><%= prefs.mobile_todos_per_page %></span></li>
</ul> </ul>
<div class="actions"> <div class="actions">
<%= link_to "Edit preferences &raquo;", { :controller => 'preferences', :action => 'edit'}, :class => 'edit_link' %> <%= link_to "Edit preferences &raquo;", { :controller => 'preferences', :action => 'edit'}, :class => 'edit_link' %>
@ -36,29 +36,29 @@
<h2>Your token</h2> <h2>Your token</h2>
<div id="token_area"> <div id="token_area">
<div class="description">Token (for feeds and API use):</div> <div class="description">Token (for feeds and API use):</div>
<div id="token><span class="highlight"><%= @user.token %></span></div> <div id="token><span class="highlight"><%= current_user.token %></span></div>
<div class="token_regenerate"> <div class="token_regenerate">
<%= button_to "Generate a new token", refresh_token_user_path(@user), <%= button_to "Generate a new token", refresh_token_user_path(current_user),
:confirm => "Are you sure? Generating a new token will replace the existing one and break any external usages of this token." %> :confirm => "Are you sure? Generating a new token will replace the existing one and break any external usages of this token." %>
</div> </div>
</div> </div>
<h2>Your authentication</h2> <h2>Your authentication</h2>
<div id="authentication_area"> <div id="authentication_area">
<% if Tracks::Config.auth_schemes.length > 1 %> <% if Tracks::Config.auth_schemes.length > 1 %>
<p>Your authentication type is <span class="highlight"><%= @user.auth_type %></span>. <p>Your authentication type is <span class="highlight"><%= current_user.auth_type %></span>.
<div class="actions"> <div class="actions">
<%= link_to "Change your authentication type &raquo;", change_auth_type_user_path(@user), :class => 'edit_link' %> <%= link_to "Change your authentication type &raquo;", change_auth_type_user_path(current_user), :class => 'edit_link' %>
</div> </div>
<% end %> <% end %>
<% if @user.auth_type == 'database' %> <% if current_user.auth_type == 'database' %>
<div class="actions"> <div class="actions">
<%= link_to 'Change your password &raquo;', change_password_user_path(@user) %> <%= link_to 'Change your password &raquo;', change_password_user_path(current_user) %>
</div> </div>
<% end %> <% end %>
<% if @user.auth_type == 'open_id' %> <% if current_user.auth_type == 'open_id' %>
<p>Your Open ID URL is <span class="highlight"><%= @user.open_id_url %></span>. <p>Your Open ID URL is <span class="highlight"><%= current_user.open_id_url %></span>.
<div class="actions"> <div class="actions">
<%= link_to 'Change Your Identity URL &raquo;', change_auth_type_user_path(@user) %></p> <%= link_to 'Change Your Identity URL &raquo;', change_auth_type_user_path(current_user) %></p>
</div> </div>
<% end %> <% end %>
</div> </div>

View file

@ -4,13 +4,13 @@
:locals => { :list_name => 'Active Projects', :locals => { :list_name => 'Active Projects',
:projects => @projects.select{|p| p.active? } } -%> :projects => @projects.select{|p| p.active? } } -%>
<% if @user.prefs.show_hidden_projects_in_sidebar -%> <% if prefs.show_hidden_projects_in_sidebar -%>
<%= render :partial => "sidebar/project_list", <%= render :partial => "sidebar/project_list",
:locals => { :list_name => 'Hidden Projects', :locals => { :list_name => 'Hidden Projects',
:projects => @projects.select{|p| p.hidden? } } -%> :projects => @projects.select{|p| p.hidden? } } -%>
<% end -%> <% end -%>
<% if @user.prefs.show_completed_projects_in_sidebar -%> <% if prefs.show_completed_projects_in_sidebar -%>
<%= render :partial => "sidebar/project_list", <%= render :partial => "sidebar/project_list",
:locals => { :list_name => 'Completed Projects', :locals => { :list_name => 'Completed Projects',
:projects => @projects.select{|p| p.completed? } } -%> :projects => @projects.select{|p| p.completed? } } -%>
@ -20,7 +20,7 @@
:locals => { :list_name => 'Active Contexts', :locals => { :list_name => 'Active Contexts',
:contexts => @contexts.reject{|c| c.hide? } } -%> :contexts => @contexts.reject{|c| c.hide? } } -%>
<% if @user.prefs.show_hidden_contexts_in_sidebar -%> <% if prefs.show_hidden_contexts_in_sidebar -%>
<%= render :partial => "sidebar/context_list", <%= render :partial => "sidebar/context_list",
:locals => { :list_name => 'Hidden Contexts', :locals => { :list_name => 'Hidden Contexts',
:contexts => @contexts.select{|c| c.hide? } } -%> :contexts => @contexts.select{|c| c.hide? } } -%>

View file

@ -8,7 +8,7 @@
<% form_tag :action => 'update_auth_type' do %> <% form_tag :action => 'update_auth_type' do %>
<div><label for="user_auth_type">Authentication type:</label> <%= select('user', 'auth_type', Tracks::Config.auth_schemes.collect {|p| [ p, p ] }) %></div> <div><label for="user_auth_type">Authentication type:</label> <%= select('user', 'auth_type', Tracks::Config.auth_schemes.collect {|p| [ p, p ] }) %></div>
<div id="open_id" style="display:<%= @user.auth_type == 'open_id' ? 'block' : 'none' %>"><label for="user_open_id_url">Identity URL:</label> <input type="text" name="openid_url" value="<%= @user.open_id_url %>" class="open_id" /></div> <div id="open_id" style="display:<%= current_user.auth_type == 'open_id' ? 'block' : 'none' %>"><label for="user_open_id_url">Identity URL:</label> <input type="text" name="openid_url" value="<%= current_user.open_id_url %>" class="open_id" /></div>
<div class="actions"><%= submit_tag 'Change Authentication Type' %> <%= link_to 'Cancel', preferences_path %></div> <div class="actions"><%= submit_tag 'Change Authentication Type' %> <%= link_to 'Cancel', preferences_path %></div>
<%= observe_field( :user_auth_type, :function => "$('open_id').style.display = value == 'open_id' ? 'block' : 'none'") %> <%= observe_field( :user_auth_type, :function => "$('open_id').style.display = value == 'open_id' ? 'block' : 'none'") %>

View file

@ -1,7 +1,15 @@
require_dependency "user" require_dependency "user"
module LoginSystem module LoginSystem
def current_user
get_current_user
end
def prefs
current_user.prefs unless current_user.nil?
end
protected protected
# overwrite this if you want to restrict access to only a few actions # overwrite this if you want to restrict access to only a few actions
@ -38,9 +46,9 @@ module LoginSystem
user = User.find_by_remember_token(cookies[:auth_token]) user = User.find_by_remember_token(cookies[:auth_token])
if user && user.remember_token? if user && user.remember_token?
session['user_id'] = user.id session['user_id'] = user.id
@user = user set_current_user(user)
@user.remember_me current_user.remember_me
cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at } cookies[:auth_token] = { :value => current_user.remember_token , :expires => current_user.remember_token_expires_at }
flash[:notice] = "Logged in successfully. Welcome back!" flash[:notice] = "Logged in successfully. Welcome back!"
end end
end end
@ -111,8 +119,7 @@ module LoginSystem
end end
def logged_in? def logged_in?
get_current_user current_user != nil
@user != nil
end end
def get_current_user def get_current_user
@ -125,8 +132,6 @@ module LoginSystem
def set_current_user(user) def set_current_user(user)
@user = user @user = user
@prefs = @user.prefs unless @user.nil?
@user
end end
# overwrite if you want to have special behavior in case the user is not authorized # overwrite if you want to have special behavior in case the user is not authorized

View file

@ -114,6 +114,26 @@ class LoginControllerTest < Test::Rails::TestCase
assert !@controller.send(:logged_in?) assert !@controller.send(:logged_in?)
end end
def test_current_user_nil
get :login
assert_nil @controller.current_user
end
def test_current_user_correct
user = login('jane','sesame', 'off')
assert_equal user, @controller.current_user
end
def test_prefs_nil
get :login
assert_nil @controller.prefs
end
def test_prefs_correct
user = login('jane','sesame', 'off')
assert_equal user.prefs, @controller.prefs
end
private private
# Logs in a user and returns the user object found in the session object # Logs in a user and returns the user object found in the session object