Class: PeriodsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/periods_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

#abort_taskObject



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

def abort_task
  @task = Task.find params[:id]
  @task.abort
  load_tasks(@task.period)
  render :action => :finish_task, :layout => false
end

#burn_down_chartObject



100
101
102
# File 'app/controllers/periods_controller.rb', line 100

def burn_down_chart
  send_burn_down_chart Chart::NORMAL
end

#burn_down_chart_largeObject



104
105
106
# File 'app/controllers/periods_controller.rb', line 104

def burn_down_chart_large
  send_burn_down_chart Chart::LARGE
end

#burn_down_chart_thumbnailObject



96
97
98
# File 'app/controllers/periods_controller.rb', line 96

def burn_down_chart_thumbnail
  send_burn_down_chart Chart::THUMBNAIL
end

#createObject



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/periods_controller.rb', line 65

def create
  @period = Period.new(params[:period])
  if @period.save
    @period.reload
    flash[:notice] = 'Period was successfully created.'
    back_or_redirect_to :action => 'show', :id => @period.higher_item ? @period.higher_item : @period
  else
    render :action => 'new'
  end
end

#destroyObject



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

def destroy
  Period.find(params[:id]).destroy
  flash[:notice] = 'Period was successfully deleted.'
  redirect_to :action => 'index'
end

#editObject



76
77
78
# File 'app/controllers/periods_controller.rb', line 76

def edit
  @period = Period.find(params[:id])
end

#finish_taskObject



157
158
159
160
161
# File 'app/controllers/periods_controller.rb', line 157

def finish_task
  @task = Task.find(params[:id])
  @task.finish(Task::COMPLETED, true)
  load_tasks(@task.period)
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/periods_controller.rb', line 5

def index
  active_periods = Period.find(:all).select {|period| period.active?(true)}
  my_active_periods = active_periods.select {|period| period.party.includes?(current_user)}
  unless my_active_periods.empty?
    most_urgent_period = my_active_periods.sort_by {|p| p.required_speed}.last
  else
    most_urgent_period = active_periods.sort_by {|p| p.required_speed}.last
  end
  flash.keep
  redirect_to :action => 'show', :id => most_urgent_period
end

#list_workObject



108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/periods_controller.rb', line 108

def list_work
  if @period = Period.find_by_id(params[:id])
    @works = @period.tasks.map {|t| t.works}.flatten.sort_by {|w| [w.completed_at || Time.now.midnight, w.started_on, w.start_time]}
    def @works.page_count; size / 25; end
    render :template => '/works/list'
  else
    flash[:notice] = "You need to provide a period ID"
    redirect_to ''
  end
end


119
120
121
122
123
124
125
# File 'app/controllers/periods_controller.rb', line 119

def make_link
  if params[:id].to_i > 0
    render :partial => 'link', :layout => false, :locals => {:period => Period.find(params[:id])}
  else
    render :text => ''
  end
end

#move_task_to_next_periodObject



149
150
151
152
153
154
155
# File 'app/controllers/periods_controller.rb', line 149

def move_task_to_next_period
  @task = Task.find(params[:id])
  next_period = @task.period.lower_item
  next_period = next_period.lower_item while next_period && next_period.passed?
  params[:period_id] = next_period && next_period.id
  move_task_to_period
end

#move_task_to_periodObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/periods_controller.rb', line 127

def move_task_to_period
  params[:id] = $1 if params[:id] =~ /^task_(\d*)/
  @task = Task.find(params[:id])
  if request.post?
    period = params[:period_id] && Period.find(params[:period_id])
    if period && period.active_or_future?
      next_task = @task.higher_item ? @task.higher_item : @task.lower_item ? @task.lower_item : @task
      @task.move_to_period period
      load_tasks(@task.period)
      if request.xhr?
        render :action => :move_task_to_period, :layout => false
      else
        redirect_to :action => :show, :id => @task.period, :task_id => @task.id, :layout => false
      end
    else
      rjs_detour_to :action => :new, :period => {:party_id => @task.period.party_id}, :layout => false
    end
  else
    redirect_to :action => :show, :id => @task.period, :task_id => @task.id, :layout => false
  end
end

#newObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/periods_controller.rb', line 50

def new
  @period = Period.new
  if params[:period] && params[:period][:party_id]
    @period.party_id = params[:period][:party_id]
    previous = @period.party.periods.last
    if previous
      @period.start_on = previous.end_on + 1
      @period.end_on = previous.end_on + (1 + (previous.end_on - previous.start_on))
    else
      @period.start_on = Date.today
    end
  end
  @parties = Party.find(:all)
end

#orderObject



205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/periods_controller.rb', line 205

def order
  if params.keys.find {|k| k =~ /active_tasks_(.*)/}
  period_id = $1
  ids = params["active_tasks_#{period_id}"]
  tasks = ids.select {|id| not id.empty?}
  tasks.each_with_index do |id,idx|
    task = Task.find(id)
    task.insert_at(idx + 1) 
    task.save!
  end
  end
end

#reopen_taskObject



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

def reopen_task
  @task = Task.find(params[:id])
  @task.reopen
  @task.reload
  finished_count = Task.count(:conditions => ['period_id = ? AND finished_at IS NOT NULL', @task.period_id])
  @last_finished = finished_count == 0
end

#showObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/periods_controller.rb', line 20

def show
  if params[:task_id]
    @selected_task = Task.find(params[:task_id])
  end
  if @selected_task
    @period = @selected_task.period
    if @period.nil?
      redirect_to :controller => 'backlogs', :action => :show, :id => @selected_task.backlog.id
      return
    end
  elsif params[:id] && @period = Period.find_by_id(params[:id])
  elsif @period = Period.find(:first)
  else
    redirect_to :action => :new
    return
  end
  if @selected_task.nil?
    @selected_task = @period.open_tasks.first
  end
  # TODO (uwe): Switch to use cookie since the same behavior should work on next session.
  # TODO (uwe): Problem: Cannot read cookie in the same request it was written...
  @show_only_grabbed_tasks = session['show_only_grabbed_tasks'] == 'true'
  load_tasks(@period)
end

#show_no_layoutObject



45
46
47
48
# File 'app/controllers/periods_controller.rb', line 45

def show_no_layout
  show
  render :partial => 'show_active', :layout => false, :locals => {:i => 0}
end

#updateObject



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

def update
  @period = Period.find(params[:id])
  if @period.update_attributes(params[:period])
    flash[:notice] = 'Period was successfully updated.'
    redirect_to :action => 'show', :id => @period
  else
    render :action => 'edit'
  end
end

#update_task_estimateObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/controllers/periods_controller.rb', line 178

def update_task_estimate
  if params[:id]
    @task = Task.find_by_id(params[:id])
    if params[:estimate] && params[:estimate][:todo]
      begin
        Float(params[:estimate][:todo])
        @task.estimate(params[:estimate][:todo])
        @period = @task.period
        @success, flash[:notice] = true, 'Estimate updated'
        rescue ArgumentError => e
        @success, flash[:notice] = false, "Estimate was not numeric"
      end
      if @task.finished?
        load_tasks(@task.period)
        render :action => 'finish_task', :layout => false
      else
        render :template => '/tasks/update_estimate', :layout => false
      end
    else
      return false, "Estimate is missing."
    end
  else
    @estimate = Estimate.new
    return false, 'Task id is missing.'
  end
end