tracks/tracks/app/controllers/application.rb
bsag 4c2742f6f3 Started adding support for the Chronic library, to provide natural language date selections. You can now type phrases such as "tomorrow", "nov 10", "1 week hence", "10 days hence" and so on into the date box and these will be parsed into a valid date. I haven't managed to get proper validation working yet, but you'll get a live preview of the parsed date just below the input box.
What doesn't work yet:

* If you delete all characters in the date box, you'll get an error message. This will go away if you type more characters
* You'll get an error as above when the form is cleared and redisplayed after submission. Again, it will go away if you type anything in the box.
* Validation doesn't work, but the preview will display "Invalid date" if Chronic can't parse your phrase
* This isn't added to the edit form for actions yet.

Also partially fixed #394: the mobile interface works again, but you might get an error visiting the subsequent pages of a filtered view (i.e. viewing a single context or project). I'm not sure what's causing this, but it's on my list to fix.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@332 a4c988fc-2ded-0310-b66e-134b36920a42
2006-10-25 15:35:08 +00:00

103 lines
2.9 KiB
Ruby

# The filters added to this controller will be run for all controllers in the application.
# Likewise will all the methods added be available for all controllers.
require_dependency "login_system"
require_dependency "source_view"
require "redcloth"
require 'date'
require 'time'
class ApplicationController < ActionController::Base
helper :application
include LoginSystem
before_filter :set_session_expiration
before_filter :get_current_user
after_filter :set_charset
# By default, sets the charset to UTF-8 if it isn't already set
def set_charset
@headers["Content-Type"] ||= "text/html; charset=UTF-8"
end
# Reverses the urlize() method by substituting underscores for spaces
#
def deurlize(name)
name.to_s.gsub(/_/, " ")
end
def set_session_expiration
# http://wiki.rubyonrails.com/rails/show/HowtoChangeSessionOptions
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
# don't set the session expiry time.
if session
# Get expiry time (allow ten seconds window for the case where we have none)
expiry_time = session['expiry_time'] || Time.now + 10
if expiry_time < Time.now
# Too late, matey... bang goes your session!
reset_session
else
# Okay, you get another hour
session['expiry_time'] = Time.now + (60*60)
end
end
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
private
def get_current_user
@user = User.find(session['user_id']) if session['user_id']
@prefs = @user.preference unless @user.nil?
end
def get_admin_user
@admin = User.find(:first, :conditions => [ "is_admin = ?", true ])
end
def parse_date_per_user_prefs( s )
return nil if s == ''
Chronic.parse(s)
end
def init_data_for_sidebar
@projects = @user.projects
@contexts = @user.contexts
init_not_done_counts
end
def init_not_done_counts(parents = ['project','context'])
parents.each {|parent|
eval("@#{parent}_not_done_counts = Todo.count(:all,
:conditions => ['user_id = ? and type = ? and done = ?', @user.id, \"Immediate\", false],
:group => :#{parent}_id)")
}
end
end