Class: CardsController

Inherits:
ApplicationController show all
Includes:
TransitionManager
Defined in:
app/controllers/cards_controller.rb

Instance Method Summary collapse

Methods included from TransitionManager

#apply_transition, #successor_statuses, #transition_allowed?

Methods included from ApplicationHelper

#footer_menu, #home_page

Instance Method Details

#change_statusObject



45
46
47
48
49
50
51
52
# File 'app/controllers/cards_controller.rb', line 45

def change_status
  @card = Card.find(params[:id])
  status = Status.get(params[:status_name])
  if (apply_transition(@card,status))
    flash[:notice] = 'Card was successfully updated.'
  end
  snap_back()
end

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/cards_controller.rb', line 18

def create
  @card = Card.new(params[:card])
  projectInfo = ProjectInfo.find(:first)
  @card.project_id=(projectInfo.id)
  @card.iteration_raised= projectInfo.current_iteration
  if @card.save
    flash[:notice] = 'Card was successfully created.'
    snap_back()
  else
    render :action => 'raise_card'
  end
end

#editObject



31
32
33
# File 'app/controllers/cards_controller.rb', line 31

def edit
  @card = Card.find(params[:id])
end

#exportObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/cards_controller.rb', line 183

def export
  filename = "#{params[:file_name]}.csv"
  cards = Card.find(params[:card_ids].split("/"))
  
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    response.headers['Pragma'] = 'public'
    response.headers["Content-type"] = "text/plain" 
    response.headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
    response.headers.headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" 
    response.headers['Expires'] = "0" 
  else
    response.headers['Content-Type'] = "text/csv; charset=iso-8859-1; header=present"
    response.headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
  end
  render :text => Proc.new { |response, output|
    exporter = CardsCSVExporter.new()
    exporter.export(cards,output)
   }
end

#find_other_cards(card) ⇒ Object



97
98
99
100
101
102
103
# File 'app/controllers/cards_controller.rb', line 97

def find_other_cards(card)
  conditions = "status_name != '#{Status::DROPPED.name()}' AND status_name != '#{Status::DELIVERED.name()}'"
  if (! card.id().nil?()) 
    conditions += " AND id != #{@card.id()}"
  end
  Card.find(:all, :conditions => conditions)
end

#header_menuObject



226
227
228
229
230
231
232
# File 'app/controllers/cards_controller.rb', line 226

def header_menu
  menu = [
         ['Export table',{:action => 'export', :controller => 'cards', :file_name => page_title(), :card_ids => @cards.collect {|card| card.id()}}]
         ]
  menu<<(['Import Cards',{:action => 'import', :controller => 'cards'}]) if (action_name() == "list_per_status") && ((@status == Status::NEW) || (@status == Status::ESTIMATED))
  return menu
end

#importObject



203
204
# File 'app/controllers/cards_controller.rb', line 203

def import
end

#import_updateObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/controllers/cards_controller.rb', line 206

def import_update
  if (! params[:import][:file].nil?())
    parser = CardsCSVImporter.new()
    begin
      new_objects = parser.import(params[:import][:file])
      for object in new_objects
          if (object.kind_of?(Card))
            object.owner_name=(user_name())
          end
          object.save()
      end
    rescue ArgumentError
      flash[:notice] = "Failed to import file."
    end
  else
    flash[:notice] = "No file to import."
  end
  snap_back()
end

#list_per_functional_setObject



93
94
95
# File 'app/controllers/cards_controller.rb', line 93

def list_per_functional_set
  @functional_set = FunctionalSet.find(params[:id])
end

#list_per_milestoneObject



89
90
91
# File 'app/controllers/cards_controller.rb', line 89

def list_per_milestone
  @milestone = Milestone.find(params[:id])
end

#list_per_statusObject



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

def list_per_status
  @status = Status.get(params[:status_name])
  @card_pages, @cards = paginate :cards, :conditions => " status_name = '#{@status.name()}'", :order => "priority_rank, title", :per_page => 100
end

#list_per_userObject



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

def list_per_user
  if (params[:owner_name]).nil?()
    @owner = user_name()
  else
    @owner = params[:owner_name]
  end
  @card_pages, @cards = paginate :cards, :conditions => " owner_name = '#{@owner}'", :order => "status_name, priority_rank, title", :per_page => 100
end

#mergeObject



105
106
107
# File 'app/controllers/cards_controller.rb', line 105

def merge
  @cards = Card.find(:all, :conditions => "status_name in ('#{Status::NEW.name()}','#{Status::ESTIMATED.name()}','#{Status::IN_PLAY.name()}')")
end

#merge_cards(cards, user = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/cards_controller.rb', line 141

def merge_cards(cards,user = nil)
  new_card = Card.create()
  new_owner = cards[0].owner_name()
  common_owner = true
  new_priority = nil
  new_milestone = nil
  new_card_type = nil
  new_card_size = nil
  new_child_cards = Array.new()
  new_parent_cards = Array.new()
  new_functional_sets = Array.new()
  new_description = ""
  new_title = "merged from "
  for card in cards
    new_title += " #{card.id()},"
    new_description += "#{card.description()}\n"
    new_card_type = card.card_type() unless (! new_card_type.nil?()) && (new_card_type <= card.card_type())
    new_priority = card.priority() unless (! new_priority.nil?()) && (new_priority <= card.priority())
    new_card_size = card.estimated_size() unless (! new_card_size.nil?()) && (! card.estimated_size().nil?()) && (new_card_size >= card.estimated_size())
    new_milestone = card.target_milestone() unless (! new_milestone.nil?()) && (new_milestone <= card.target_milestone())
    new_child_cards.concat(card.child_cards())
    new_parent_cards.concat(card.parent_cards())
    new_functional_sets.concat(card.functional_sets())
    common_owner = common_owner && (new_owner == card.owner_name())
    apply_transition(card,Status::DROPPED)
  end
  new_owner = user unless (common_owner)
  
  new_card.title=(new_title.chop())
  new_card.description=(new_description.chop())
  new_card.owner_name=(new_owner)
  new_card.priority=(new_priority)
  new_card.target_milestone=(new_milestone)
  new_card.card_type=(new_card_type)
  new_card.estimated_size=(new_card_size)
  new_card.child_cards=(new_child_cards.compact().select {|card| ! cards.include?(card)} )
  new_card.parent_cards=(new_parent_cards.compact().select {|card| ! cards.include?(card)})
  new_card.functional_sets=(new_functional_sets.compact())
  new_card.save()
  return new_card
end

#merge_updateObject



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

def merge_update
  cards_to_merge = Array.new()
  params[:merge_cards].each {|id, value| cards_to_merge<<Card.find(id) if (value == "true")}
  if (cards_to_merge.empty?())
    redirect_to :action => "index", :controller => "board"
  else
    merged_card = merge_cards(cards_to_merge, current_user())
    redirect_to :action => "edit", :id => merged_card.id()
  end
end

#page_titleObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/cards_controller.rb', line 120

def page_title
  if ('list_per_functional_set' == action_name())
  "Cards in #{@functional_set.name()} Set"
  elsif ('list_per_status' == action_name())
    "#{@status.name()} Cards"
  elsif ('list_per_user' == action_name())
    "Cards for #{@owner}"
  elsif ('list_per_milestone' == action_name())
    "Cards for #{@milestone.name()}"
    
  elsif ('split' == action_name())
    "Splitting #{@card.title()} Card"
  else
    super
  end
end

#raise_cardObject



13
14
15
16
# File 'app/controllers/cards_controller.rb', line 13

def raise_card
  @card = Card.new
  @card.owner_name= user_name()
end

#splitObject



54
55
56
57
# File 'app/controllers/cards_controller.rb', line 54

def split
  @card = Card.find(params[:id])
  redirect_to :back unless splitable?(@card)
end

#splitable?(card) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/controllers/cards_controller.rb', line 137

def splitable?(card)
  [Status::NEW, Status::ESTIMATED, Status::IN_PLAY].include?(card.status())
end

#updateObject



35
36
37
38
39
40
41
42
43
# File 'app/controllers/cards_controller.rb', line 35

def update
  @card = Card.find(params[:id])
  if @card.update_attributes(params[:card])
    flash[:notice] = 'Card was successfully updated.'
    snap_back()
  else
    render :action => 'edit'
  end
end

#update_splitObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/cards_controller.rb', line 59

def update_split
  @card = Card.find(params[:id])
  slices = params[:slice][:slices].to_i
  count = 0
  while (count < slices) do
    new_card = Card.new(@card.attributes())
    new_card.id=(nil)
    new_card.title=("#{new_card.title()} (#{count.to_s})")
    new_card.save!()
    count = count + 1
  end
  apply_transition(@card, Status::DROPPED)
  
  snap_back()
end