dustin / money

My money tracking app. There are many like it, but this one is mine.

This URL has Read+Write access

money / app / controllers / allowance_tasks_controller.rb
100644 60 lines (53 sloc) 1.837 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class AllowanceTasksController < ApplicationController
 
  def index
    title "Allowance Tasks You've Created"
    @tasks=AllowanceTask.find_all_by_creator_id(current_user.id,
      :order => 'allowance_tasks.name', :include => :owner).group_by(&:owner)
    @weekly_sums = {}
    @tasks.each do |owner,tasks|
      @weekly_sums[owner] = tasks.reject(&:deleted).inject(0.0) {|c,i| c + i.weekly_value}
    end
  end
 
  def new
    title "Create an allowance task"
    @users=User.find :all, :conditions => ["id != ?", current_user.id], :order => 'name'
    @accounts = Hash.new {|h,k| h[k] = []}
    MoneyAccount.find(:all, :conditions => ["active = ?", true], :order => 'name').each do |a|
      if current_user.groups.include? a.group
        @accounts[a.group_id] << a
      end
    end
    @categories = Hash.new {|h,k| h[k] = []}
    Category.find(:all, :order => 'name').each do |c|
      if current_user.groups.include? c.group
        @categories[c.group_id] << c
      end
    end
    @groups = current_user.groups
  end
 
  def create
    @task=AllowanceTask.new params[:allowance_task]
    @task.creator = current_user
    @task.save!
    flash[:info] = "Created new task: #{@task.name}"
    redirect_to allowance_tasks_path
  end
 
  # Toggle the active state of a task
  def update
    task=AllowanceTask.find params[:id].to_i
    active = (params[:active] == 'true')
    task.deleted = !active
    task.save!
    render :action => (active ? :activate : :deactivate)
  end
 
  def complete
    tids = params['task'].keys.map(&:to_i)
    # To prevent fraud, only include task IDs from those available.
    available = AllowanceTask.find_available(current_user)
    tasks = available.find_all {|n| tids.include? n.id}
    AllowanceTask.transaction do
      tasks.each {|t| t.perform!}
    end
    redirect_to home_path
  end
 
end