From c216ab325bc56123819fb5b700a759753959ead8 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Thu, 4 Oct 2007 17:04:50 +0000 Subject: Added position on Enumeration model. git-svn-id: http://redmine.rubyforge.org/svn/trunk@800 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/enumerations_controller.rb | 15 ++++++++++++ app/models/enumeration.rb | 28 ++++++++++++--------- app/views/enumerations/list.rhtml | 38 +++++++++++++++++------------ db/migrate/072_add_enumerations_position.rb | 12 +++++++++ 4 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 db/migrate/072_add_enumerations_position.rb diff --git a/app/controllers/enumerations_controller.rb b/app/controllers/enumerations_controller.rb index e77af49ab..7a7f1685a 100644 --- a/app/controllers/enumerations_controller.rb +++ b/app/controllers/enumerations_controller.rb @@ -59,6 +59,21 @@ class EnumerationsController < ApplicationController end end + def move + @enumeration = Enumeration.find(params[:id]) + case params[:position] + when 'highest' + @enumeration.move_to_top + when 'higher' + @enumeration.move_higher + when 'lower' + @enumeration.move_lower + when 'lowest' + @enumeration.move_to_bottom + end if params[:position] + redirect_to :action => 'index' + end + def destroy Enumeration.find(params[:id]).destroy flash[:notice] = l(:notice_successful_delete) diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index 46d350a21..b0ad48bc7 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -16,27 +16,33 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class Enumeration < ActiveRecord::Base + acts_as_list :scope => 'opt = \'#{opt}\'' + before_destroy :check_integrity validates_presence_of :opt, :name validates_uniqueness_of :name, :scope => [:opt] validates_length_of :name, :maximum => 30 validates_format_of :name, :with => /^[\w\s\'\-]*$/i - - OPTIONS = { - "IPRI" => :enumeration_issue_priorities, - "DCAT" => :enumeration_doc_categories, - "ACTI" => :enumeration_activities - }.freeze - - def self.get_values(option) - find(:all, :conditions => ['opt=?', option]) - end - + + OPTIONS = { + "IPRI" => :enumeration_issue_priorities, + "DCAT" => :enumeration_doc_categories, + "ACTI" => :enumeration_activities + }.freeze + + def self.get_values(option) + find(:all, :conditions => {:opt => option}, :order => 'position') + end + def option_name OPTIONS[self.opt] end + #def <=>(enumeration) + # position <=> enumeration.position + #end + def to_s; name end private diff --git a/app/views/enumerations/list.rhtml b/app/views/enumerations/list.rhtml index f7756ce30..f92ca0a31 100644 --- a/app/views/enumerations/list.rhtml +++ b/app/views/enumerations/list.rhtml @@ -1,19 +1,25 @@

<%=l(:label_enumerations)%>

-  + <% Enumeration::OPTIONS.each do |option, name| %> +

<%= l(name) %>

+ +<% enumerations = Enumeration.get_values(option) %> +<% if enumerations.any? %> + +<% enumerations.each do |enumeration| %> + + + + +<% end %> +
<%= link_to enumeration.name, :action => 'edit', :id => enumeration %> + <%= link_to image_tag('2uparrow.png', :alt => l(:label_sort_highest)), {:action => 'move', :id => enumeration, :position => 'highest'}, :method => :post, :title => l(:label_sort_highest) %> + <%= link_to image_tag('1uparrow.png', :alt => l(:label_sort_higher)), {:action => 'move', :id => enumeration, :position => 'higher'}, :method => :post, :title => l(:label_sort_higher) %> - + <%= 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) %> +
+<% reset_cycle %> +<% end %> - <% if params[:opt]==option %> - -

<%= l(name) %>

- -

<%= link_to l(:label_enumeration_new), { :action => 'new', :opt => option }, :class => "icon icon-add" %>

  - - <% else %> -

<%= link_to l(name), :opt => option %>

- <% end %> - -<% end %> \ No newline at end of file +

<%= link_to l(:label_enumeration_new), { :action => 'new', :opt => option } %>

+<% end %> diff --git a/db/migrate/072_add_enumerations_position.rb b/db/migrate/072_add_enumerations_position.rb new file mode 100644 index 000000000..7def86772 --- /dev/null +++ b/db/migrate/072_add_enumerations_position.rb @@ -0,0 +1,12 @@ +class AddEnumerationsPosition < ActiveRecord::Migration + def self.up + add_column :enumerations, :position, :integer, :default => 1, :null => false + Enumeration.find(:all).group_by(&:opt).each_value do |enums| + enums.each_with_index {|enum, i| enum.update_attribute(:position, i+1)} + end + end + + def self.down + remove_column :enumerations, :position + end +end -- cgit v1.2.3