Class: WorksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/works_controller.rb

Constant Summary

Constants included from Localization

Localization::LOCALIZED_STRINGS

Instance Method Summary collapse

Methods inherited from ApplicationController

in_place_edit_for, #initialize

Methods included from ApplicationHelper

#back_or_link_to, #detour?, #detour_to, #display_notice, #h, #image_button_to, #image_detour_to, #image_link_to, #image_link_to_remote, #insert, #record, #resolution_image, #t, #update_task, #with_detour

Methods included from Localization

#l, load_localized_strings, #valid_language?

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#auto_complete_for_work_backlog_nameObject



179
180
181
182
183
184
185
186
# File 'app/controllers/works_controller.rb', line 179

def auto_complete_for_work_backlog_name
  @backlogs = Backlog.find(:all, 
                           :conditions => [ 'LOWER(name) LIKE ?',
  '%' + params[:work][:backlog_name].downcase + '%' ], 
  :order => 'name ASC',
  :limit => 16)
  render :partial => '/backlogs/name_list'
end

#auto_complete_for_work_task_descriptionObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/works_controller.rb', line 188

def auto_complete_for_work_task_description
  if backlog_name = params[:backlog_name]
    if @backlog = Backlog.find_by_name(backlog_name)
      @tasks = @backlog.tasks_with_children.select {|t| t.description.downcase =~ /#{params[:work][:task_description].downcase}/}
    end
  end
  @tasks ||= Task.find(:all,
                       :conditions => [ "finished_at IS NULL AND LOWER(description) LIKE ?",
                      '%' + params[:work][:task_description].downcase + '%' ], 
  :order => 'description ASC',
  :limit => 16)
  
  render :partial => 'description_list'
end

#auto_complete_for_work_work_account_nameObject



170
171
172
173
174
175
176
177
# File 'app/controllers/works_controller.rb', line 170

def 
  @accounts = WorkAccount.find(:all, 
                               :conditions => [ 'LOWER(name) LIKE ?',
  '%' + params[:work][:work_account_name].downcase + '%' ], 
  :order => 'name ASC',
  :limit => 16)
  render :partial => '/work_accounts/name_list'
end

#calculate_hoursObject



203
204
205
206
207
# File 'app/controllers/works_controller.rb', line 203

def calculate_hours
  calculated_duration = (Time.parse(params[:completed_at]) - Time.parse(params[:started_at]))
  new_value = '%2d:%02d' % [calculated_duration / 3600, (calculated_duration % 3600) / 60]
  render :update do |page| page['work_hours_time'].value = new_value end
end

#createObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/works_controller.rb', line 38

def create
  if params[:work]
    backlog_name = params[:work].delete(:backlog_name)
    task_description = params[:work].delete(:task_description)
    if backlog_name && backlog_name.size > 0 && task_description && task_description.size > 0
      backlog = Backlog.find_by_name(backlog_name)
      task = backlog.tasks_with_children.find{|t| t.description == task_description}
      params[:work][:task_id] = task.id if task
    end
     = params[:work].delete(:work_account_name)
    if  && .size > 0
       = WorkAccount.find_by_name()
      params[:work][:work_account_id] = .id
    end
  end
  convert_hours_param
  convert_start_time_param
  @work = Work.new(params[:work])
  @work.completed_at = Time.now unless @work.completed_at
  @work.user_id = current_user.id
  if @work.save
    flash[:notice] = 'Work was successfully created.'
  else
    new
    render :action => 'new'
    return
  end
  
  @work.task.estimates.create!(params[:estimate]) if @work.task && params[:estimate]
  
  back_or_redirect_to :controller => 'periods', :action => 'show', :id => @work.task && @work.task.period, :task_id => @work.task && @work.task.id
end

#daily_work_sheetObject



131
132
133
134
135
136
137
138
139
# File 'app/controllers/works_controller.rb', line 131

def daily_work_sheet
  @date = (params[:id] && Date.parse(params[:id])) || Date.today
  @periods = []
  @works = Work.find_work_for_day @date
  @started_works = Task.find_started
  @work = Work.new(:started_at => Time.now, :completed_at => Time.now)
  @work_accounts = WorkAccount.find(:all, :order => :name)
  render :layout => 'wide'
end

#destroyObject



124
125
126
127
128
129
# File 'app/controllers/works_controller.rb', line 124

def destroy
  work = Work.find(params[:id])
  period = work.task && work.task.period
  work.destroy
  back_or_redirect_to :controller => 'periods', :action => :list_work, :id => period 
end

#editObject



71
72
73
74
75
76
77
78
# File 'app/controllers/works_controller.rb', line 71

def edit
  @work = Work.find(params[:id])
  @work.attributes = params[:work]
  @estimate = Estimate.new(params[:estimate])
  @work_accounts = WorkAccount.find(:all)
  @tasks = [@work.task] + Task.find_open
  @users = User.find(:all)
end

#indexObject



9
10
11
12
# File 'app/controllers/works_controller.rb', line 9

def index
  list
  render :action => 'list'
end

#listObject



18
19
20
21
# File 'app/controllers/works_controller.rb', line 18

def list
  @period = params[:id] && Period.find(params[:id])
  @works = Work.paginate :order => 'completed_at', :page => params[:page]
end

#newObject



27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/works_controller.rb', line 27

def new
  unless @work
    @work = Work.new(params[:work])
    @work.user = user
  end
  @estimate ||= Estimate.new(params[:estimate])
  @work_accounts = WorkAccount.find(:all, :order => :name)
  @tasks = Task.find_open
  @users = User.find(:all)
end

#showObject



23
24
25
# File 'app/controllers/works_controller.rb', line 23

def show
  @work = Work.find(params[:id])
end

#timelisteObject



161
162
163
164
165
166
167
168
# File 'app/controllers/works_controller.rb', line 161

def timeliste
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @work_totals_per_work_account = Work.work_totals_for_week(@year, @week)
  headers["Content-Type"] = "application/vnd.ms-excel"
  headers["Content-Disposition"] = 'attachment; filename="export.xml"'
  render :layout => false
end

#updateObject



80
81
82
83
84
85
86
87
88
# File 'app/controllers/works_controller.rb', line 80

def update
  if update_work
    back_or_redirect_to :controller => 'periods', :action => 'show', :id => @work.task.period, :task_id => @work.task.id
  else
    @task = @work.task
    edit
    render :action => 'edit'
  end
end

#update_rowObject



90
91
92
93
94
95
# File 'app/controllers/works_controller.rb', line 90

def update_row
  update_work
  flash.discard
  @field = params[:field] || 'started_at_time'
  @work_accounts = WorkAccount.find(:all, :order => :name)
end

#update_timeObject



97
98
99
100
101
# File 'app/controllers/works_controller.rb', line 97

def update_time
  update_work
  flash.discard
  @field = params[:field] || 'started_at_time'
end

#update_workObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/works_controller.rb', line 103

def update_work
  @work = Work.find(params[:id])
  convert_start_time_param
  convert_hours_param
  if @work.update_attributes(params[:work])
    flash[:notice] = 'Work was successfully updated.'
    if @work.task
      if params[:estimate]
        if (@estimate = @work.task.estimates.create(params[:estimate])).errors.size == 0
          return true
        end
      else
        return true
      end
    else
      return true
    end
  end
  return false
end

#weekly_work_sheetObject



141
142
143
144
145
146
147
148
149
# File 'app/controllers/works_controller.rb', line 141

def weekly_work_sheet
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @rows = Work.works_for_week(@year, @week)
  @first_date = Date.commercial(@year, @week, 1)
  @last_date = @first_date + 6
  @lock = WorkLock.find(:first, :conditions => ['user_id = ? AND start_on <= ? AND end_on >= ?', current_user.id, @first_date, @last_date])
  render :layout => 'wide'
end

#weekly_work_sheet_by_work_accountObject



151
152
153
154
155
156
157
158
159
# File 'app/controllers/works_controller.rb', line 151

def 
  @year = (params[:year] && params[:year].to_i) || Date.today.year
  @week = (params[:week] && params[:week].to_i) || Date.today.cweek
  @work_accounts = Work.(@year, @week)
  @first_date = Date.commercial(@year, @week, 1)
  @last_date = @first_date + 6
  @lock = WorkLock.find(:first, :conditions => ['user_id = ? AND start_on <= ? AND end_on >= ?', current_user.id, @first_date, @last_date])
  render :layout => 'wide'
end