Module: TaskwarriorWeb::App::Helpers

Defined in:
lib/taskwarrior-web/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/taskwarrior-web/helpers.rb', line 84

def authorized?
  @auth ||=  Rack::Auth::Basic::Request.new(request.env)
  values = [TaskwarriorWeb::Config.property('task-web.user'), TaskwarriorWeb::Config.property('task-web.passwd')]
  @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == values
end


33
34
35
# File 'lib/taskwarrior-web/helpers.rb', line 33

def auto_link(text)
  Rinku.auto_link(text, :all, 'target="_blank"')
end

#badge_countObject



50
51
52
53
54
55
56
57
# File 'lib/taskwarrior-web/helpers.rb', line 50

def badge_count
  if filter = TaskwarriorWeb::Config.property('task-web.filter.badge')
    total = TaskwarriorWeb::Task.query(filter).count
  else
    total = task_count
  end
  total.to_i == 0 ? '' : total.to_s
end

#colorize_date(timestamp) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/taskwarrior-web/helpers.rb', line 13

def colorize_date(timestamp)
  return if timestamp.nil?
  due_def = (TaskwarriorWeb::Config.due || 7).to_i
  date = Date.parse(timestamp)
  case
    when Date.today == date then 'warning'                            # today
    when Date.today > date then 'error'                               # overdue
    when Date.today.advance(:days => due_def) >= date then 'success'  # within the "due" range
    else 'regular'                                                    # just a regular task
  end
end


68
69
70
71
72
73
74
75
76
77
# File 'lib/taskwarrior-web/helpers.rb', line 68

def crud_links(task)
  string = %(<span class="crud-links">)
  string << %(<a class="annotation-add" href="/tasks/#{task.uuid}/annotations/new"><i class="icon-comment"></i></a>)
  string << %(&nbsp;|&nbsp;)
  string << %(<a href="/tasks/#{task.uuid}?destination=#{ERB::Util.u(request.path_info)}"><i class="icon-pencil"></i></a>)
  string << %(&nbsp;|&nbsp;)
  string << %(<a href="/tasks/#{task.uuid}?destination=#{ERB::Util.u(request.path_info)}" data-method="DELETE" data-confirm="Are you sure you want to delete this task?"><i class="icon-trash"></i></a>)
  string << %(</span>)
  string
end

#flash_typesObject



37
38
39
# File 'lib/taskwarrior-web/helpers.rb', line 37

def flash_types
  [:success, :info, :warning, :error]
end

#format_date(timestamp) ⇒ Object



4
5
6
7
# File 'lib/taskwarrior-web/helpers.rb', line 4

def format_date(timestamp)
  format = TaskwarriorWeb::Config.dateformat || '%-m/%-d/%Y'
  Time.parse(timestamp).localtime.strftime(format)
end

#format_tags(tags) ⇒ Object



9
10
11
# File 'lib/taskwarrior-web/helpers.rb', line 9

def format_tags(tags)
  tags.join(', ')
end

#linkify(item) ⇒ Object



25
26
27
# File 'lib/taskwarrior-web/helpers.rb', line 25

def linkify(item)
  item.gsub('.', '--') unless item.nil? unless item.nil?
end

#progress_bar(tasks) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/taskwarrior-web/helpers.rb', line 59

def progress_bar(tasks)
  return 0 if tasks.empty?
  doneness = (tasks.select { |t| t.status == 'completed' }.count.to_f / tasks.count.to_f) * 100
  string = %(<div class="progress progress-striped">)
  string << %(<div class="bar" style="width: #{doneness.to_i}%;"></div>&nbsp;#{doneness.to_i}%)
  string << %(</div>)
  string
end

#protected!Object

Authentication



80
81
82
# File 'lib/taskwarrior-web/helpers.rb', line 80

def protected!
  response['WWW-Authenticate'] = %(Basic realm="Taskwarrior Web") and throw(:halt, [401, "Not authorized\n"]) and return unless authorized?
end

#syncObject

Syncronise the local task database with the server



92
93
94
# File 'lib/taskwarrior-web/helpers.rb', line 92

def sync
  TaskwarriorWeb::Command.new(:sync, nil, nil).run
end

#task_countObject



41
42
43
44
45
46
47
48
# File 'lib/taskwarrior-web/helpers.rb', line 41

def task_count
  if filter = TaskwarriorWeb::Config.property('task-web.filter')
    total = TaskwarriorWeb::Task.query(filter).count
  else
    total = TaskwarriorWeb::Task.count(:status => :pending)
  end
  total.to_s
end

#unlinkify(item) ⇒ Object



29
30
31
# File 'lib/taskwarrior-web/helpers.rb', line 29

def unlinkify(item)
  item.gsub('--', '.') unless item.nil?
end