Project

General

Profile

Feature #3718 » 0009-categoria_padre.diff

categoria padre - Luis Roa, 2013-08-15 18:10

View differences:

app/views/issues/_attributes.html.erb Wed Aug 07 15:28:59 2013 +0200
19 19
<% end %>
20 20

  
21 21
<% if @issue.safe_attribute?('category_id') && @issue.project.issue_categories.any? %>
22
<p><%= f.select :category_id, (@issue.project.issue_categories.collect {|c| [c.name, c.id]}), :include_blank => true, :required => @issue.required_attribute?('category_id') %>
22
<p><%= f.select :category_id, (@issue.project.issue_categories[0].get_categories(@project,nil).collect{ |c| [c.full_name(c,@project,c.name),c.id]}), :include_blank => true, :required => @issue.required_attribute?('category_id') %>
23 23
<%= link_to(image_tag('add.png', :style => 'vertical-align: middle;'),
24 24
            new_project_issue_category_path(@issue.project),
25 25
            :remote => true,
app/models/issue_category.rb Wed Aug 07 15:28:59 2013 +0200
25 25
  validates_uniqueness_of :name, :scope => [:project_id]
26 26
  validates_length_of :name, :maximum => 30
27 27

  
28
  safe_attributes 'name', 'assigned_to_id'
28
  safe_attributes 'name', 'assigned_to_id', 'categoria_padre'
29 29

  
30 30
  scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
31 31

  
......
43 43
  def <=>(category)
44 44
    name <=> category.name
45 45
  end
46

  
46
  
47
  def all_sons(cat,proj)  	
48
	mysons = proj.issue_categories.select{ |c| c.categoria_padre == cat.id }
49
	sons = []
50
	if mysons.length < 1
51
		return mysons
52
	else
53
		for son in mysons		
54
			sons.push(son) 
55
			sons.push *all_sons(son,proj)
56
		end
57
		return sons
58
	end
59
  end
60
  
61
  def full_name(cat,proj,fullname)
62
	if cat.categoria_padre.nil?
63
		myfathers = proj.issue_categories.select{ |c| c.id == 0 }
64
	else
65
		myfathers = proj.issue_categories.select{ |c| c.id == cat.categoria_padre }
66
	end
67
	if myfathers.length < 1
68
		return fullname
69
	else
70
		father = myfathers[0]
71
		fullname = father.name+" / "+fullname		
72
		return full_name(father,proj,fullname)
73
	end	
74
  end
75
  
76
  def get_categories(proj,idcat)
77
	categories = proj.issue_categories.select{ |c| c.categoria_padre == nil && !c.id.nil? && c.id != idcat }
78
	cats = []
79
	for c in categories
80
		cats.push(c)
81
		cats.push *all_sons(c,proj)
82
	end
83
	return cats
84
  end
85
	
47 86
  def to_s; name end
48 87
end 
49 88

  
app/views/issue_categories/_form.html.erb Wed Aug 07 15:28:59 2013 +0200
3 3
<div class="box tabular">
4 4
<p><%= f.text_field :name, :size => 30, :required => true %></p>
5 5
<p><%= f.select :assigned_to_id, principals_options_for_select(@project.assignable_users, @category.assigned_to), :include_blank => true %></p>
6
<p><%= f.select :categoria_padre, options_for_select(@category.name.nil? || @category.name=="" ? @category.get_categories(@project,nil).collect{ |c| [c.full_name(c,@project,c.name),c.id]}  : ((@category.get_categories(@project,@category.id).collect{ |c| [c.full_name(c,@project,c.name),c.id] })),@category.categoria_padre) , :include_blank => true %></p>									
6 7
</div>
app/views/issues/show.html.erb Wed Aug 07 15:28:59 2013 +0200
40 40
    rows.left l(:field_assigned_to), avatar(@issue.assigned_to, :size => "14").to_s.html_safe + (@issue.assigned_to ? link_to_user(@issue.assigned_to) : "-"), :class => 'assigned-to'
41 41
  end
42 42
  unless @issue.disabled_core_fields.include?('category_id')
43
    rows.left l(:field_category), h(@issue.category ? @issue.category.name : "-"), :class => 'category'
43
    rows.left l(:field_category), h(@issue.category ? @issue.category.full_name(@issue.category,@project,@issue.category.name) : "-"), :class => 'category'
44 44
  end
45 45
  unless @issue.disabled_core_fields.include?('fixed_version_id')
46 46
    rows.left l(:field_fixed_version), (@issue.fixed_version ? link_to_version(@issue.fixed_version) : "-"), :class => 'fixed-version'
app/models/project.rb Wed Aug 07 15:28:59 2013 +0200
45 45
  has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
46 46
  has_many :documents, :dependent => :destroy
47 47
  has_many :news, :dependent => :destroy, :include => :author
48
  has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
48
  has_many :issue_categories, :dependent => :delete_all, :order => "COALESCE(#{IssueCategory.table_name}.categoria_padre, #{IssueCategory.table_name}.id), #{IssueCategory.table_name}.categoria_padre IS NOT NULL, #{IssueCategory.table_name}.id"
49 49
  has_many :boards, :dependent => :destroy, :order => "position ASC"
50 50
  has_one :repository, :conditions => ["is_default = ?", true]
51 51
  has_many :repositories, :dependent => :destroy
db/migrate/20130807152159_add_categoria_padre_to_issue_categories.rb Thu Aug 08 08:42:00 2013 +0200
1
class AddCategoriaPadreToIssueCategories < ActiveRecord::Migration
2

  
3
  def self.up
4
    add_column :issue_categories, :categoria_padre, :int, :default => 0
5
  end
6

  
7
  def self.down
8
    remove_column :issue_categories, :categoria_padre
9
  end
10

  
11
end
app/views/projects/settings/_issue_categories.html.erb Thu Aug 08 08:42:00 2013 +0200
2 2
<table class="list">
3 3
  <thead><tr>
4 4
    <th><%= l(:label_issue_category) %></th>	
5
	 <th><%= l(:categoria_padre) %></th>
5 6
    <th><%= l(:field_assigned_to) %></th>	
6 7
    <th></th>
7 8
  </tr></thead>
8 9
  <tbody>
9
<% for category in @project.issue_categories %>
10
<% for category in @project.issue_categories[0].get_categories(@project,nil) %>
10 11
  <% unless category.new_record? %>
11 12
  <tr class="<%= cycle 'odd', 'even' %>">
12
    <td><%=h(category.name) %></td>
13
    <td><%=h( ("»"*((category.full_name(category,@project,category.name).split("/").length)-1))+" "+category.name ) %></td>
14
	<td><%=h((@project.issue_categories.detect{ |c| c.id == category.categoria_padre}).name) if category.categoria_padre %></td>	
13 15
	<td><%=h(category.assigned_to.name) if category.assigned_to %></td>
14 16
    <td class="buttons">
15 17
    	<% if User.current.allowed_to?(:manage_categories, @project) %>
config/locales/es.yml Thu Aug 08 08:42:00 2013 +0200
1124 1124
  field_closed_on: Closed
1125 1125
  setting_default_projects_tracker_ids: Default trackers for new projects
1126 1126
  label_total_time: Total
1127
  categoria_padre: categoría padre
1128
  field_categoria_padre: categoría padre
1129
  notice_tiene_subcategorias: "Primero debe eliminar las subcategorias"
app/controllers/issue_categories_controller.rb Thu Aug 08 08:42:00 2013 +0200
51 51
  def create
52 52
    @category = @project.issue_categories.build
53 53
    @category.safe_attributes = params[:issue_category]
54
	#cat2 = @project.issue_categories.detect{ |c| c.id == @category.categoria_padre }
55
	@category.name = level(@category,"") + @category.name
54 56
    if @category.save
55 57
      respond_to do |format|
56 58
        format.html do
......
69 70
      end
70 71
    end
71 72
  end
73
  
74
  def level(cat=nil,lev="")
75
	if cat.categoria_padre.nil? || cat.categoria_padre == ""
76
		return lev
77
	else
78
		#lev = lev+">"
79
		cat2 = @project.issue_categories.detect{ |c| c.id == cat.categoria_padre }
80
		if !cat2.name.nil? && cat2.name != ""
81
			#lev = lev + ">"
82
			#super unless ['try', 'test', 'my_method'].include?
83
			level(cat2,lev)
84
		else
85
			return lev
86
		end
87
	end  
88
  end
72 89

  
73 90
  def edit
74 91
  end
......
91 107
  end
92 108

  
93 109
  def destroy
94
    @issue_count = @category.issues.size
95
    if @issue_count == 0 || params[:todo] || api_request?
96
      reassign_to = nil
97
      if params[:reassign_to_id] && (params[:todo] == 'reassign' || params[:todo].blank?)
98
        reassign_to = @project.issue_categories.find_by_id(params[:reassign_to_id])
99
      end
100
      @category.destroy(reassign_to)
101
      respond_to do |format|
102
        format.html { redirect_to_settings_in_projects }
103
        format.api { render_api_ok }
104
      end
105
      return
106
    end
107
    @categories = @project.issue_categories - [@category]
108
  end
110
	if @category.all_sons(@category,@project).length > 0
111
		flash[:notice] = l(:notice_tiene_subcategorias)
112
		index
113
	else
114
		@issue_count = @category.issues.size
115
		if @issue_count == 0 || params[:todo] || api_request?
116
		reassign_to = nil
117
		if params[:reassign_to_id] && (params[:todo] == 'reassign' || params[:todo].blank?)
118
			reassign_to = @project.issue_categories.find_by_id(params[:reassign_to_id])
119
		end
120
		@category.destroy(reassign_to)
121
		respond_to do |format|
122
			format.html { redirect_to_settings_in_projects }
123
			format.api { render_api_ok }
124
		end
125
		return
126
		end
127
		@categories = @project.issue_categories - [@category]
128
	end
129
  end	
109 130

  
110 131
  private
111 132

  
(5-5/5)