diff --git a/tracks/app/controllers/application.rb b/tracks/app/controllers/application.rb index a9e39020..f7c8c1b2 100644 --- a/tracks/app/controllers/application.rb +++ b/tracks/app/controllers/application.rb @@ -50,22 +50,6 @@ class ApplicationController < ActionController::Base end end - def redirect_with_notice( message, options = {}) - options['flash_target'] = 'notice' - redirect_with_flash message, options - end - - def redirect_with_flash( message, options ) - flash[options['flash_target']] = message - options.delete 'flash_target' - redirect_to options - end - - def redirect_with_warning( message, options = {}) - options['flash_target'] = 'warning' - redirect_with_flash message, options - end - def render_failure message, status = 404 render :text => message, :status => status end @@ -107,4 +91,12 @@ class ApplicationController < ActionController::Base end end + # Set the contents of the flash message from a controller + # Usage: notify :warning, "This is the message" + # Sets the flash of type 'warning' to "This is the message" + def notify(type, message) + flash[type] = message + logger.error("ERROR: #{message}") if type == :error + end + end diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/context_controller.rb index 621e4ace..a646c7b8 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/context_controller.rb @@ -3,7 +3,7 @@ class ContextController < ApplicationController helper :todo prepend_before_filter :login_required - layout "standard" + layout "standard", :except => :date_preview def index list @@ -78,19 +78,17 @@ class ContextController < ApplicationController @saved = @item.save if @saved - # This reports real count +1 for some reason that I don't understand - # Almost identical code for add_item in projects reports correct num - @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 + @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.state = ? and todos.context_id = ?", @user.id, 'active', @item.context_id]).size.to_s end return if request.xhr? # fallback for standard requests if @saved - flash[:notice] = 'Added new next action.' + notify :notice, 'Added new next action.' redirect_to :controller => 'todo', :action => 'list' else - flash[:warning] = 'The next action was not added. Please try again.' + notify :warning, 'The next action was not added. Please try again.' redirect_to :controller => 'todo', :action => 'list' end @@ -98,7 +96,7 @@ class ContextController < ApplicationController if request.xhr? # be sure to include an error.rjs render :action => 'error' else - flash[:warning] = 'An error occurred on the server.' + notify :warning, 'An error occurred on the server.' redirect_to :controller => 'todo', :action => 'list' end end @@ -113,7 +111,7 @@ class ContextController < ApplicationController if @context.save render :partial => 'context_listing', :object => @context else - flash[:warning] = "Couldn't update new context" + notify :warning, "Couldn't update new context" render :text => "" end end @@ -126,7 +124,7 @@ class ContextController < ApplicationController if @context.destroy render_text "" else - flash[:warning] = "Couldn't delete context \"#{@context.name}\"" + notify :warning, "Couldn't delete context \"#{@context.name}\"" redirect_to( :controller => "context", :action => "list" ) end end @@ -156,7 +154,7 @@ class ContextController < ApplicationController return @context else @context = nil # Should be nil anyway. - flash[:warning] = "Item and session user mis-match: #{@context.user_id} and #{@user.id}!" + notify :warning, "Item and session user mis-match: #{@context.user_id} and #{@user.id}!" render_text "" end end @@ -167,7 +165,7 @@ class ContextController < ApplicationController return @context else @context = nil - flash[:warning] = "Project and session user mis-match: #{@context.user_id} and #{@user.id}!" + notify :warning, "Project and session user mis-match: #{@context.user_id} and #{@user.id}!" render_text "" end end @@ -177,7 +175,7 @@ class ContextController < ApplicationController if @user == item.user return item else - flash[:warning] = "Item and session user mis-match: #{item.user.name} and #{@user.name}!" + notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!" render_text "" end end diff --git a/tracks/app/controllers/deferred_controller.rb b/tracks/app/controllers/deferred_controller.rb index 35c8bfd2..f8a4c2ee 100644 --- a/tracks/app/controllers/deferred_controller.rb +++ b/tracks/app/controllers/deferred_controller.rb @@ -79,7 +79,7 @@ class DeferredController < ApplicationController respond_to do |wants| wants.html do - flash[:notice] = 'Successfully deleted next action' if @saved + notify :notice, 'Successfully deleted next action' if @saved redirect_to :action => "index" end wants.js do @@ -92,7 +92,7 @@ class DeferredController < ApplicationController rescue respond_to do |wants| wants.html do - flash[:warning] = 'An error occurred on the server.' + notify :warning, 'An error occurred on the server.' redirect_to :action => "index" end wants.js { render :action => 'error' } @@ -123,7 +123,7 @@ class DeferredController < ApplicationController if @user == item.user return item else - flash[:warning] = "Item and session user mis-match: #{item.user.name} and #{@user.name}!" + notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!" render_text "" end end diff --git a/tracks/app/controllers/login_controller.rb b/tracks/app/controllers/login_controller.rb index 6d89c35f..96128963 100644 --- a/tracks/app/controllers/login_controller.rb +++ b/tracks/app/controllers/login_controller.rb @@ -14,12 +14,12 @@ class LoginController < ApplicationController # of inactivity session['noexpiry'] = params['user_noexpiry'] msg = (should_expire_sessions?) ? "will expire after 1 hour of inactivity." : "will not expire." - flash[:notice] = "Login successful: session #{msg}" + notify :notice, "Login successful: session #{msg}" cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year } redirect_back_or_default :controller => "todo", :action => "index" else @login = params['user_login'] - flash[:warning] = "Login unsuccessful" + notify :warning, "Login unsuccessful" end end end @@ -37,7 +37,7 @@ class LoginController < ApplicationController # redirect to the server redirect_to open_id_response.redirect_url((request.protocol + request.host_with_port + "/"), url_for(:action => 'complete')) else - flash[:warning] = "Unable to find openid server for #{params[:openid_url]}" + notify :warning, "Unable to find openid server for #{params[:openid_url]}" redirect_to :action => 'login' end end @@ -49,11 +49,11 @@ class LoginController < ApplicationController # URL that we were verifying. We include it in the error # message to help the user figure out what happened. if open_id_response.identity_url - flash[:message] = "Verification of #{open_id_response.identity_url} failed. " + msg = "Verification of #{open_id_response.identity_url} failed. " else - flash[:message] = "Verification failed. " + msg = "Verification failed. " end - flash[:message] += open_id_response.msg.to_s + notify :error, open_id_response.msg.to_s + msg when OpenID::SUCCESS # Success means that the transaction completed without @@ -61,18 +61,18 @@ class LoginController < ApplicationController # the verification. @user = User.find_by_open_id_url(open_id_response.identity_url) unless (@user.nil?) - flash[:message] = "You have successfully verified #{open_id_response.identity_url} as your identity." + notify :notice, "You have successfully verified #{open_id_response.identity_url} as your identity." session['user_id'] = @user.id redirect_back_or_default :controller => 'todo', :action => 'index' else - flash[:warning] = "You have successfully verified #{open_id_response.identity_url} as your identity, but you do not have a Tracks account. Please ask your administrator to sign you up." + notify :warning, "You have successfully verified #{open_id_response.identity_url} as your identity, but you do not have a Tracks account. Please ask your administrator to sign you up." end when OpenID::CANCEL - flash[:message] = "Verification cancelled." + notify :warning, "Verification cancelled." else - flash[:warning] = "Unknown response status: #{open_id_response.status}" + notify :warning, "Unknown response status: #{open_id_response.status}" end redirect_to :action => 'login' unless performed? end @@ -107,7 +107,7 @@ class LoginController < ApplicationController @user = User.authenticate(user.login, params['user']['password']) @user.create_preference @user.save - flash[:notice] = "Signup successful for user #{@user.login}." + notify :notice, "Signup successful for user #{@user.login}." redirect_back_or_default :controller => "todo", :action => "index" end end @@ -124,7 +124,7 @@ class LoginController < ApplicationController def logout session['user_id'] = nil reset_session - flash[:notice] = "You have been logged out of Tracks." + notify :notice, "You have been logged out of Tracks." redirect_to :action => "login" end diff --git a/tracks/app/controllers/mobile_controller.rb b/tracks/app/controllers/mobile_controller.rb index 2d257e40..221e76cd 100644 --- a/tracks/app/controllers/mobile_controller.rb +++ b/tracks/app/controllers/mobile_controller.rb @@ -87,7 +87,7 @@ class MobileController < ApplicationController if @user == item.user return item else - flash[:warning] = "Item and session user mis-match: #{item.user.name} and #{@user.name}!" + notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!" render_text "" end end diff --git a/tracks/app/controllers/note_controller.rb b/tracks/app/controllers/note_controller.rb index 74a06a62..64467493 100644 --- a/tracks/app/controllers/note_controller.rb +++ b/tracks/app/controllers/note_controller.rb @@ -37,7 +37,7 @@ class NoteController < ApplicationController if note.destroy render :text => '' else - flash[:warning] = "Couldn't delete note \"#{note.id.to_s}\"" + notify :warning, "Couldn't delete note \"#{note.id.to_s}\"" render :text => '' end end @@ -48,7 +48,7 @@ class NoteController < ApplicationController if note.save render :partial => 'notes', :object => note else - flash[:warning] = "Couldn't update note \"#{note.id.to_s}\"" + notify :warning, "Couldn't update note \"#{note.id.to_s}\"" render :text => '' end end diff --git a/tracks/app/controllers/project_controller.rb b/tracks/app/controllers/project_controller.rb index 0e6db1c6..deceb4c0 100644 --- a/tracks/app/controllers/project_controller.rb +++ b/tracks/app/controllers/project_controller.rb @@ -5,7 +5,7 @@ class ProjectController < ApplicationController helper :todo prepend_before_filter :login_required - layout "standard" + layout "standard", :except => :date_preview def index list @@ -35,7 +35,7 @@ class ProjectController < ApplicationController @page_title = "TRACKS::Project: #{@project.name}" if @contexts.empty? - flash[:warning] = 'You must add at least one context before adding next actions.' + notify :warning, 'You must add at least one context before adding next actions.' end if @not_done.empty? @@ -103,17 +103,17 @@ class ProjectController < ApplicationController @saved = @item.save if @saved - @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 + @up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.state = ? and todos.project_id = ?", @user.id, 'active', @item.project_id]).size.to_s end return if request.xhr? # fallback for standard requests if @saved - flash[:notice] = 'Added new next action.' + notify :notice, 'Added new next action.' redirect_to :controller => 'todo', :action => 'index' else - flash[:warning] = 'The next action was not added. Please try again.' + notify :warning, 'The next action was not added. Please try again.' redirect_to :controller => 'todo', :action => 'index' end @@ -121,7 +121,7 @@ class ProjectController < ApplicationController if request.xhr? # be sure to include an error.rjs render :action => 'error' else - flash[:warning] = 'An error occurred on the server.' + notify :warning, 'An error occurred on the server.' redirect_to :controller => 'todo', :action => 'index' end end @@ -148,7 +148,7 @@ class ProjectController < ApplicationController render :text => 'Success' end else - flash[:warning] = "Couldn't update project" + notify :warning, "Couldn't update project" render :text => '' end end @@ -171,7 +171,7 @@ class ProjectController < ApplicationController if @project.destroy render :text => '' else - flash[:warning] = "Couldn't delete project \"#{@project.name}\"" + notify :warning, "Couldn't delete project \"#{@project.name}\"" redirect_to( :controller => "project", :action => "list" ) end end @@ -201,7 +201,7 @@ class ProjectController < ApplicationController return @project else @project = nil # Should be nil anyway - flash[:warning] = "Project and session user mis-match: #{@project.user_id} and #{@user.id}!" + notify :warning, "Project and session user mis-match: #{@project.user_id} and #{@user.id}!" render :text => '' end end @@ -212,7 +212,7 @@ class ProjectController < ApplicationController return @project else @project = nil - flash[:warning] = "Project and session user mis-match: #{@project.user_id} and #{@user.id}!" + notify :warning, "Project and session user mis-match: #{@project.user_id} and #{@user.id}!" render :text => '' end end @@ -222,7 +222,7 @@ class ProjectController < ApplicationController if @user == item.user return item else - flash[:warning] = "Item and session user mis-match: #{item.user.name} and #{@user.name}!" + notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!" render :text => '' end end diff --git a/tracks/app/controllers/todo_controller.rb b/tracks/app/controllers/todo_controller.rb index 0f46261e..5de57808 100644 --- a/tracks/app/controllers/todo_controller.rb +++ b/tracks/app/controllers/todo_controller.rb @@ -35,7 +35,7 @@ class TodoController < ApplicationController @contexts_to_show = @contexts.reject {|x| x.hide? } if @contexts.empty? - flash[:warning] = 'You must add at least one context before adding next actions.' + notify :warning, "You must add at least one context before adding next actions." end # Set count badge to number of not-done, not hidden context items @@ -90,7 +90,7 @@ class TodoController < ApplicationController rescue respond_to do |wants| wants.html do - flash[:warning] = 'An error occurred on the server.' + notify :warning, "An error occurred on the server." render :action => "index" end wants.js { render :action => 'error' } @@ -130,9 +130,12 @@ class TodoController < ApplicationController return if request.xhr? if @saved - redirect_with_notice "The action '#{@item.description}' was marked as #{@item.completed? ? 'complete' : 'incomplete' }", :action => "index" + # TODO: I think this will work, but can't figure out how to test it + notify :notice, "The action '#{@item.description}' was marked as #{@item.completed? ? 'complete' : 'incomplete' }" + redirect_to :action => "index" else - redirect_with_notice "The action '#{@item.description}' was NOT marked as #{@item.completed? ? 'complete' : 'incomplete' } due to an error on the server.", :action => "index" + notify :notice, "The action '#{@item.description}' was NOT marked as #{@item.completed? ? 'complete' : 'incomplete' } due to an error on the server.", "index" + redirect_to :action => "index" end end @@ -166,7 +169,7 @@ class TodoController < ApplicationController render :action => 'update' else render :update do |page| - page.replace_html "info", content_tag("div", "Error updating the context of the dragged item. Item and context user mis-match: #{@item.user.name} and #{@context.user.name}! - refresh the page to see them.", "class" => "warning") + page.notify :warning, content_tag("div", "Error updating the context of the dragged item. Item and context user mis-match: #{@item.user.name} and #{@context.user.name}! - refresh the page to see them."), 8.0 end end end @@ -183,7 +186,7 @@ class TodoController < ApplicationController render :action => 'update' else render :update do |page| - page.replace_html "info", content_tag("div", "Error updating the project of the dragged item. Item and project user mis-match: #{@item.user.name} and #{@project.user.name}! - refresh the page to see them.", "class" => "warning") + page.notify :warning, content_tag("div", "Error updating the project of the dragged item. Item and project user mis-match: #{@item.user.name} and #{@project.user.name}! - refresh the page to see them."), 8.0 end end end @@ -198,9 +201,11 @@ class TodoController < ApplicationController wants.html do if @saved - redirect_with_notice 'Successfully deleted next action', :action => 'index' + notify :notice, "Successfully deleted next action", 2.0 + redirect_to :action => 'index' else - redirect_with_warning 'Failed to delete the action.', :action => 'index' + notify :error, "Failed to delete the action", 2.0 + redirect_to :action => 'index' end end @@ -223,7 +228,7 @@ class TodoController < ApplicationController rescue respond_to do |wants| wants.html do - flash[:warning] = 'An error occurred on the server.' + notify :error, 'An error occurred on the server.', 8.0 redirect_to :action => 'index' end wants.js { render :action => 'error' } @@ -255,7 +260,7 @@ class TodoController < ApplicationController @error_message = 'Item and session user mis-match: #{item.user.name} and #{@user.name}!' respond_to do |wants| wants.html do - flash[:warning] = @error_message + notify :error, @error_message, 8.0 render :action => "index" end wants.js { render :action => 'error' } diff --git a/tracks/app/controllers/user_controller.rb b/tracks/app/controllers/user_controller.rb index 1c0d9df2..e185f6f2 100644 --- a/tracks/app/controllers/user_controller.rb +++ b/tracks/app/controllers/user_controller.rb @@ -75,6 +75,7 @@ class UserController < ApplicationController redirect_to :controller => 'user', :action => 'preferences' else redirect_to :controller => 'user', :action => 'change_password' + notify :warning, "There was a problem saving the password. Please retry." end end @@ -92,17 +93,17 @@ class UserController < ApplicationController # redirect to the server redirect_to open_id_response.redirect_url((request.protocol + request.host_with_port + "/"), url_for(:action => 'complete')) else - flash[:warning] = "Unable to find openid server for #{params[:openid_url]}" + notify :warning, "Unable to find openid server for #{params[:openid_url]}" redirect_to :action => 'change_auth_type' end return end @user.auth_type = params[:user][:auth_type] if @user.save - flash[:notice] = "Authentication type updated." + notify :notice, "Authentication type updated." redirect_to :controller => 'user', :action => 'preferences' else - flash[:warning] = "There was a problem updating your authentication type: #{ @user.errors.full_messages.join(', ')}" + notify :warning, "There was a problem updating your authentication type: #{ @user.errors.full_messages.join(', ')}" redirect_to :controller => 'user', :action => 'change_auth_type' end end @@ -114,11 +115,11 @@ class UserController < ApplicationController # URL that we were verifying. We include it in the error # message to help the user figure out what happened. if open_id_response.identity_url - flash[:message] = "Verification of #{open_id_response.identity_url} failed. " + msg = "Verification of #{open_id_response.identity_url} failed. " else - flash[:message] = "Verification failed. " + msg = "Verification failed. " end - flash[:message] += open_id_response.msg.to_s + notify :error, open_id_response.msg.to_s + msg when OpenID::SUCCESS # Success means that the transaction completed without @@ -127,17 +128,17 @@ class UserController < ApplicationController @user.auth_type = 'open_id' @user.open_id_url = open_id_response.identity_url if @user.save - flash[:message] = "You have successfully verified #{open_id_response.identity_url} as your identity and set your authentication type to Open ID." + notify :notice, "You have successfully verified #{open_id_response.identity_url} as your identity and set your authentication type to Open ID." else - flash[:warning] = "You have successfully verified #{open_id_response.identity_url} as your identity but there was a problem saving your authentication preferences." + notify :warning, "You have successfully verified #{open_id_response.identity_url} as your identity but there was a problem saving your authentication preferences." end redirect_to :action => 'preferences' when OpenID::CANCEL - flash[:message] = "Verification cancelled." + notify :warning, "Verification cancelled." else - flash[:warning] = "Unknown response status: #{open_id_response.status}" + notify :warning, "Unknown response status: #{open_id_response.status}" end redirect_to :action => 'change_auth_type' unless performed? end @@ -146,6 +147,7 @@ class UserController < ApplicationController def refresh_token @user.crypt_word @user.save + notify :notice, "New token successfully generated" redirect_to :controller => 'user', :action => 'preferences' end @@ -154,10 +156,10 @@ class UserController < ApplicationController def do_change_password_for(user) user.change_password(params[:updateuser][:password], params[:updateuser][:password_confirmation]) if user.save - flash[:notice] = "Password updated." + notify :notice, "Password updated." return true else - flash[:warning] = 'There was a problem saving the password. Please retry.' + notify :error, 'There was a problem saving the password. Please retry.' return false end end diff --git a/tracks/app/helpers/application_helper.rb b/tracks/app/helpers/application_helper.rb index 2635e54c..b532d557 100644 --- a/tracks/app/helpers/application_helper.rb +++ b/tracks/app/helpers/application_helper.rb @@ -148,4 +148,13 @@ module ApplicationHelper render :partial => 'shared/flash' end + # Display a flash message in RJS templates + # Usage: page.notify :warning, "This is the message", 5.0 + # Puts the message into a flash of type 'warning', fades over 5 secs + def notify(type, message, fade_duration) + type = type.to_s # symbol to string + page.replace 'flash', "

#{message}

" + page.visual_effect :fade, 'flash', :duration => fade_duration + end + end diff --git a/tracks/app/views/context/add_item.rjs b/tracks/app/views/context/add_item.rjs index 4238c94d..432d1784 100644 --- a/tracks/app/views/context/add_item.rjs +++ b/tracks/app/views/context/add_item.rjs @@ -1,7 +1,5 @@ if @saved - page.show "status" - page.replace_html "status", content_tag("div", "Added new next action", "class" => "confirmation") - page.visual_effect :fade, 'status', :duration => 2 + page.notify :notice, "Added new next action", 5.0 page['badge_count'].replace_html @up_count page.send :record, "Form.reset('todo-form-new-action');Form.focusFirstElement('todo-form-new-action')" page.send :record, "Form.reset('todo-form-new-action-lightbox');Form.focusFirstElement('todo-form-new-action-lightbox')" @@ -9,7 +7,6 @@ if @saved page.visual_effect :highlight, "item-#{@item.id}-container", :duration => 3 page["c#{@item.context_id}empty-nd"].hide # If we are adding an new action, the uncompleted actions must be > 0 else - page.hide "status" - page.replace_html "status", content_tag("div", content_tag("h2", "#{pluralize(@item.errors.count, "error")} prohibited this record from being saved") + content_tag("p", "There were problems with the following fields:") + content_tag("ul", @item.errors.each_full { |msg| content_tag("li", msg) }), "id" => "ErrorExplanation", "class" => "ErrorExplanation") - page.visual_effect :appear, 'status', :duration => 0.5 + page.show 'status' + page.replace_html 'status', "#{error_messages_for('item')}" end \ No newline at end of file diff --git a/tracks/app/views/context/create.rjs b/tracks/app/views/context/create.rjs index 6e8b3e07..170fd716 100644 --- a/tracks/app/views/context/create.rjs +++ b/tracks/app/views/context/create.rjs @@ -1,11 +1,9 @@ if @saved - page.hide "warning" page.insert_html :bottom, "list-contexts", :partial => 'context_listing', :locals => { :context_listing => @context } page.sortable "list-contexts", get_listing_sortable_options page.call "Form.reset", "context-form" page.call "Form.focusFirstElement", "context-form" else - page.hide "warning" - page.replace_html "warning", content_tag("div", content_tag("h2", "#{pluralize(@context.errors.count, "error")} prohibited this record from being saved") + content_tag("p", "There were problems with the following fields:") + content_tag("ul", @context.errors.each_full { |msg| content_tag("li", msg) }), "id" => "ErrorExplanation", "class" => "ErrorExplanation") - page.visual_effect :appear, 'warning', :duration => 0.5 + page.show 'status' + page.replace_html 'status', "#{error_messages_for('context')}" end \ No newline at end of file diff --git a/tracks/app/views/context/error.rjs b/tracks/app/views/context/error.rjs index 8bbd1558..4f82b3e4 100644 --- a/tracks/app/views/context/error.rjs +++ b/tracks/app/views/context/error.rjs @@ -1 +1 @@ -page.replace_html "status", content_tag("div", "A server error has occurred", "class" => "warning") \ No newline at end of file +page.notify :error, @error_message || "An error occurred on the server.", 8.0 \ No newline at end of file diff --git a/tracks/app/views/context/list.rhtml b/tracks/app/views/context/list.rhtml index ff489173..dff089dc 100644 --- a/tracks/app/views/context/list.rhtml +++ b/tracks/app/views/context/list.rhtml @@ -1,6 +1,4 @@
- <%= render_flash %> -
<%= render :partial => 'context_listing', :collection => @contexts %>
@@ -13,6 +11,9 @@