Module: TaskwarriorWeb::App::Helpers

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

Instance Method Summary collapse

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


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

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


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

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

#badge_countObject



46
47
48
49
50
51
52
53
# File 'lib/taskwarrior-web/helpers.rb', line 46

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

#colorize_date(timestamp) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/taskwarrior-web/helpers.rb', line 9

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


64
65
66
67
68
69
70
71
# File 'lib/taskwarrior-web/helpers.rb', line 64

def crud_links(task)
  string = %(<span class="crud-links">)
  string << %(<a href="/tasks/#{task.uuid}"><i class="icon-pencil"></i></a>)
  string << %(&nbsp;|&nbsp;)
  string << %(<a href="/tasks/#{task.uuid}/delete"><i class="icon-trash"></i></a>)
  string << %(</span>)
  string
end

#flash_typesObject



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

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

#linkify(item) ⇒ Object



21
22
23
# File 'lib/taskwarrior-web/helpers.rb', line 21

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

#progress_bar(tasks) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/taskwarrior-web/helpers.rb', line 55

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



74
75
76
# File 'lib/taskwarrior-web/helpers.rb', line 74

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

#task_countObject



37
38
39
40
41
42
43
44
# File 'lib/taskwarrior-web/helpers.rb', line 37

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



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

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