summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/enumerations_controller.rb21
-rw-r--r--app/models/enumeration.rb37
-rw-r--r--app/views/enumerations/destroy.rhtml12
-rw-r--r--app/views/enumerations/list.rhtml7
4 files changed, 57 insertions, 20 deletions
diff --git a/app/controllers/enumerations_controller.rb b/app/controllers/enumerations_controller.rb
index 7a7f1685a..788fa11b2 100644
--- a/app/controllers/enumerations_controller.rb
+++ b/app/controllers/enumerations_controller.rb
@@ -75,11 +75,20 @@ class EnumerationsController < ApplicationController
end
def destroy
- Enumeration.find(params[:id]).destroy
- flash[:notice] = l(:notice_successful_delete)
- redirect_to :action => 'list'
- rescue
- flash[:error] = "Unable to delete enumeration"
- redirect_to :action => 'list'
+ @enumeration = Enumeration.find(params[:id])
+ if !@enumeration.in_use?
+ # No associated objects
+ @enumeration.destroy
+ redirect_to :action => 'index'
+ elsif params[:reassign_to_id]
+ if reassign_to = Enumeration.find_by_opt_and_id(@enumeration.opt, params[:reassign_to_id])
+ @enumeration.destroy(reassign_to)
+ redirect_to :action => 'index'
+ end
+ end
+ @enumerations = Enumeration.get_values(@enumeration.opt) - [@enumeration]
+ #rescue
+ # flash[:error] = 'Unable to delete enumeration'
+ # redirect_to :action => 'index'
end
end
diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb
index e86768724..d32a0c049 100644
--- a/app/models/enumeration.rb
+++ b/app/models/enumeration.rb
@@ -24,10 +24,11 @@ class Enumeration < ActiveRecord::Base
validates_uniqueness_of :name, :scope => [:opt]
validates_length_of :name, :maximum => 30
+ # Single table inheritance would be an option
OPTIONS = {
- "IPRI" => :enumeration_issue_priorities,
- "DCAT" => :enumeration_doc_categories,
- "ACTI" => :enumeration_activities
+ "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id},
+ "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_id},
+ "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id}
}.freeze
def self.get_values(option)
@@ -39,13 +40,32 @@ class Enumeration < ActiveRecord::Base
end
def option_name
- OPTIONS[self.opt]
+ OPTIONS[self.opt][:label]
end
def before_save
Enumeration.update_all("is_default = #{connection.quoted_false}", {:opt => opt}) if is_default?
end
+ def objects_count
+ OPTIONS[self.opt][:model].count(:conditions => "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
+ end
+
+ def in_use?
+ self.objects_count != 0
+ end
+
+ alias :destroy_without_reassign :destroy
+
+ # Destroy the enumeration
+ # If a enumeration is specified, objects are reassigned
+ def destroy(reassign_to = nil)
+ if reassign_to && reassign_to.is_a?(Enumeration)
+ OPTIONS[self.opt][:model].update_all("#{OPTIONS[self.opt][:foreign_key]} = #{reassign_to.id}", "#{OPTIONS[self.opt][:foreign_key]} = #{id}")
+ end
+ destroy_without_reassign
+ end
+
def <=>(enumeration)
position <=> enumeration.position
end
@@ -54,13 +74,6 @@ class Enumeration < ActiveRecord::Base
private
def check_integrity
- case self.opt
- when "IPRI"
- raise "Can't delete enumeration" if Issue.find(:first, :conditions => ["priority_id=?", self.id])
- when "DCAT"
- raise "Can't delete enumeration" if Document.find(:first, :conditions => ["category_id=?", self.id])
- when "ACTI"
- raise "Can't delete enumeration" if TimeEntry.find(:first, :conditions => ["activity_id=?", self.id])
- end
+ raise "Can't delete enumeration" if self.in_use?
end
end
diff --git a/app/views/enumerations/destroy.rhtml b/app/views/enumerations/destroy.rhtml
new file mode 100644
index 000000000..657df8322
--- /dev/null
+++ b/app/views/enumerations/destroy.rhtml
@@ -0,0 +1,12 @@
+<h2><%= l(@enumeration.option_name) %>: <%=h @enumeration %></h2>
+
+<% form_tag({}) do %>
+<div class="box">
+<p><strong><%= l(:text_enumeration_destroy_question, @enumeration.objects_count) %></strong></p>
+<p><%= l(:text_enumeration_category_reassign_to) %>
+<%= select_tag 'reassign_to_id', ("<option>--- #{l(:actionview_instancetag_blank_option)} ---</option>" + options_from_collection_for_select(@enumerations, 'id', 'name')) %></p>
+</div>
+
+<%= submit_tag l(:button_apply) %>
+<%= link_to l(:button_cancel), :controller => 'enumerations', :action => 'index' %>
+<% end %>
diff --git a/app/views/enumerations/list.rhtml b/app/views/enumerations/list.rhtml
index 1967e5cf9..7f3886b44 100644
--- a/app/views/enumerations/list.rhtml
+++ b/app/views/enumerations/list.rhtml
@@ -1,7 +1,7 @@
<h2><%=l(:label_enumerations)%></h2>
-<% Enumeration::OPTIONS.each do |option, name| %>
-<h3><%= l(name) %></h3>
+<% Enumeration::OPTIONS.each do |option, params| %>
+<h3><%= l(params[:label]) %></h3>
<% enumerations = Enumeration.get_values(option) %>
<% if enumerations.any? %>
@@ -16,6 +16,9 @@
<%= link_to image_tag('1downarrow.png', :alt => l(:label_sort_lower)), {:action => 'move', :id => enumeration, :position => 'lower'}, :method => :post, :title => l(:label_sort_lower) %>
<%= link_to image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), {:action => 'move', :id => enumeration, :position => 'lowest'}, :method => :post, :title => l(:label_sort_lowest) %>
</td>
+ <td align="center" style="width:10%;">
+ <%= link_to l(:button_delete), { :action => 'destroy', :id => enumeration }, :method => :post, :confirm => l(:text_are_you_sure), :class => "icon icon-del" %>
+ </td>
</tr>
<% end %>
</table>