diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2016-04-17 06:57:20 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2016-04-17 06:57:20 +0000 |
commit | 64afa24a7f72526a2cbf6761e51b6cd326aa0c36 (patch) | |
tree | e0766ba52e537838fb6c06c09e81b10010690b09 /app/models/enumeration.rb | |
parent | f2eb979f66da758fbed7d98ae970f7ef74d1263f (diff) | |
download | redmine-64afa24a7f72526a2cbf6761e51b6cd326aa0c36.tar.gz redmine-64afa24a7f72526a2cbf6761e51b6cd326aa0c36.zip |
Replaces acts_as_list with an implementation that handles #position= (#12909).
Objects are reordered using the regular attribute writer #position= and AR callbacks.
git-svn-id: http://svn.redmine.org/redmine/trunk@15335 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/enumeration.rb')
-rw-r--r-- | app/models/enumeration.rb | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index eac403452..fc8486174 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -22,7 +22,7 @@ class Enumeration < ActiveRecord::Base belongs_to :project - acts_as_list :scope => 'type = \'#{type}\' AND #{parent_id ? "parent_id = #{parent_id}" : "parent_id IS NULL"}' + acts_as_positioned :scope => :parent_id acts_as_customizable acts_as_tree @@ -129,33 +129,38 @@ class Enumeration < ActiveRecord::Base return new == previous end - # Overrides acts_as_list reset_positions_in_list so that enumeration overrides - # get the same position as the overriden enumeration - def reset_positions_in_list - acts_as_list_class.where(scope_condition).reorder("#{position_column} ASC, id ASC").each_with_index do |item, i| - acts_as_list_class.where("id = :id OR parent_id = :id", :id => item.id). - update_all({position_column => (i + 1)}) - end - end + private -private def check_integrity raise "Cannot delete enumeration" if self.in_use? end - # Overrides acts_as_list add_to_list_bottom so that enumeration overrides + # Overrides Redmine::Acts::Positioned#set_default_position so that enumeration overrides # get the same position as the overriden enumeration - def add_to_list_bottom - if parent - self[position_column] = parent.position - else - super + def set_default_position + if position.nil? && parent + self.position = parent.position + end + super + end + + # Overrides Redmine::Acts::Positioned#update_position so that overrides get the same + # position as the overriden enumeration + def update_position + super + if position_changed? + self.class.where.not(:parent_id => nil).update_all( + "position = coalesce(( + select position + from (select id, position from enumerations) as parent + where parent_id = parent.id), 1)" + ) end end - # Overrides acts_as_list remove_from_list so that enumeration overrides + # Overrides Redmine::Acts::Positioned#remove_position so that enumeration overrides # get the same position as the overriden enumeration - def remove_from_list + def remove_position if parent_id.blank? super end |