##// END OF EJS Templates
Use safe_attributes for custom field enumerations....
Jean-Philippe Lang -
r15308:cf22053dd583
parent child
Show More
@@ -1,71 +1,75
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class CustomFieldEnumerationsController < ApplicationController
18 class CustomFieldEnumerationsController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_action :require_admin
21 before_action :require_admin
22 before_action :find_custom_field
22 before_action :find_custom_field
23 before_action :find_enumeration, :only => :destroy
23 before_action :find_enumeration, :only => :destroy
24
24
25 helper :custom_fields
25 helper :custom_fields
26
26
27 def index
27 def index
28 @values = @custom_field.enumerations.order(:position)
28 @values = @custom_field.enumerations.order(:position)
29 end
29 end
30
30
31 def create
31 def create
32 @value = @custom_field.enumerations.build(params[:custom_field_enumeration])
32 @value = @custom_field.enumerations.build
33 @value.safe_attributes = params[:custom_field_enumeration]
33 @value.save
34 @value.save
34 respond_to do |format|
35 respond_to do |format|
35 format.html { redirect_to custom_field_enumerations_path(@custom_field) }
36 format.html { redirect_to custom_field_enumerations_path(@custom_field) }
36 format.js
37 format.js
37 end
38 end
38 end
39 end
39
40
40 def update_each
41 def update_each
41 if CustomFieldEnumeration.update_each(@custom_field, params[:custom_field_enumerations])
42 saved = CustomFieldEnumeration.update_each(@custom_field, params[:custom_field_enumerations]) do |enumeration, enumeration_attributes|
43 enumeration.safe_attributes = enumeration_attributes
44 end
45 if saved
42 flash[:notice] = l(:notice_successful_update)
46 flash[:notice] = l(:notice_successful_update)
43 end
47 end
44 redirect_to :action => 'index'
48 redirect_to :action => 'index'
45 end
49 end
46
50
47 def destroy
51 def destroy
48 reassign_to = @custom_field.enumerations.find_by_id(params[:reassign_to_id])
52 reassign_to = @custom_field.enumerations.find_by_id(params[:reassign_to_id])
49 if reassign_to.nil? && @value.in_use?
53 if reassign_to.nil? && @value.in_use?
50 @enumerations = @custom_field.enumerations - [@value]
54 @enumerations = @custom_field.enumerations - [@value]
51 render :action => 'destroy'
55 render :action => 'destroy'
52 return
56 return
53 end
57 end
54 @value.destroy(reassign_to)
58 @value.destroy(reassign_to)
55 redirect_to custom_field_enumerations_path(@custom_field)
59 redirect_to custom_field_enumerations_path(@custom_field)
56 end
60 end
57
61
58 private
62 private
59
63
60 def find_custom_field
64 def find_custom_field
61 @custom_field = CustomField.find(params[:custom_field_id])
65 @custom_field = CustomField.find(params[:custom_field_id])
62 rescue ActiveRecord::RecordNotFound
66 rescue ActiveRecord::RecordNotFound
63 render_404
67 render_404
64 end
68 end
65
69
66 def find_enumeration
70 def find_enumeration
67 @value = @custom_field.enumerations.find(params[:id])
71 @value = @custom_field.enumerations.find(params[:id])
68 rescue ActiveRecord::RecordNotFound
72 rescue ActiveRecord::RecordNotFound
69 render_404
73 render_404
70 end
74 end
71 end
75 end
@@ -1,80 +1,90
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class CustomFieldEnumeration < ActiveRecord::Base
18 class CustomFieldEnumeration < ActiveRecord::Base
19 include Redmine::SafeAttributes
20
19 belongs_to :custom_field
21 belongs_to :custom_field
20 attr_accessible :name, :active, :position
22 attr_accessible :name, :active, :position
21
23
22 validates_presence_of :name, :position, :custom_field_id
24 validates_presence_of :name, :position, :custom_field_id
23 validates_length_of :name, :maximum => 60
25 validates_length_of :name, :maximum => 60
24 validates_numericality_of :position, :only_integer => true
26 validates_numericality_of :position, :only_integer => true
25 before_create :set_position
27 before_create :set_position
26
28
27 scope :active, lambda { where(:active => true) }
29 scope :active, lambda { where(:active => true) }
28
30
31 safe_attributes 'name',
32 'active',
33 'position'
34
29 def to_s
35 def to_s
30 name.to_s
36 name.to_s
31 end
37 end
32
38
33 def objects_count
39 def objects_count
34 custom_values.count
40 custom_values.count
35 end
41 end
36
42
37 def in_use?
43 def in_use?
38 objects_count > 0
44 objects_count > 0
39 end
45 end
40
46
41 alias :destroy_without_reassign :destroy
47 alias :destroy_without_reassign :destroy
42 def destroy(reassign_to=nil)
48 def destroy(reassign_to=nil)
43 if reassign_to
49 if reassign_to
44 custom_values.update_all(:value => reassign_to.id.to_s)
50 custom_values.update_all(:value => reassign_to.id.to_s)
45 end
51 end
46 destroy_without_reassign
52 destroy_without_reassign
47 end
53 end
48
54
49 def custom_values
55 def custom_values
50 custom_field.custom_values.where(:value => id.to_s)
56 custom_field.custom_values.where(:value => id.to_s)
51 end
57 end
52
58
53 def self.update_each(custom_field, attributes)
59 def self.update_each(custom_field, attributes)
54 return unless attributes.is_a?(Hash)
60 return unless attributes.is_a?(Hash)
55 transaction do
61 transaction do
56 attributes.each do |enumeration_id, enumeration_attributes|
62 attributes.each do |enumeration_id, enumeration_attributes|
57 enumeration = custom_field.enumerations.find_by_id(enumeration_id)
63 enumeration = custom_field.enumerations.find_by_id(enumeration_id)
58 if enumeration
64 if enumeration
59 enumeration.attributes = enumeration_attributes
65 if block_given?
66 yield enumeration, enumeration_attributes
67 else
68 enumeration.attributes = enumeration_attributes
69 end
60 unless enumeration.save
70 unless enumeration.save
61 raise ActiveRecord::Rollback
71 raise ActiveRecord::Rollback
62 end
72 end
63 end
73 end
64 end
74 end
65 end
75 end
66 end
76 end
67
77
68 def self.fields_for_order_statement(table=nil)
78 def self.fields_for_order_statement(table=nil)
69 table ||= table_name
79 table ||= table_name
70 columns = ['position']
80 columns = ['position']
71 columns.uniq.map {|field| "#{table}.#{field}"}
81 columns.uniq.map {|field| "#{table}.#{field}"}
72 end
82 end
73
83
74 private
84 private
75
85
76 def set_position
86 def set_position
77 max = self.class.where(:custom_field_id => custom_field_id).maximum(:position) || 0
87 max = self.class.where(:custom_field_id => custom_field_id).maximum(:position) || 0
78 self.position = max + 1
88 self.position = max + 1
79 end
89 end
80 end
90 end
General Comments 0
You need to be logged in to leave comments. Login now