OSDN Git Service

add replace function.
[quickedit/quick_edit.git] / app / controllers / quick_edit_issues_controller.rb
1 class QuickEditIssuesController < ApplicationController
2   include ApplicationHelper
3   before_filter :find_issues
4   before_filter :check_first_issue
5   before_filter :check_target_specifier
6   before_filter :check_replace_args, :only => [:replace, :replace_preview]
7
8   def edit
9     @dialog_params = nil
10     @issue.available_custom_fields.each do |f|
11       custom_field_name = 'issue[custom_field_values][%d]' % f.id
12       if custom_field_name == @target_specifier
13         @dialog_params = get_input_dialog_params_for_custom_fields(@issue, @target_specifier, f)
14         @dialog_params[:description] = f.description.presence if f.attributes().has_key?('description')
15       end
16     end
17     if @dialog_params.nil?
18       @dialog_params = get_input_dialog_params_for_core_fields(@issue, @target_specifier)
19       @dialog_params[:description] = nil
20     end
21     @dialog_params[:description] = nil if (@dialog_params[:description] == "")
22     @dialog_params[:issue_ids] = params[:ids]
23     @dialog_params[:back_url] = params[:back_url]
24     @dialog_params[:default_value] = params[:default_value] unless (params[:default_value].nil?)
25   end
26
27   def replace_preview
28     @replaced_issues = @issues.map do |issue|
29       { :id  => issue.id,
30         :old => issue[@attribute_name],
31         :new => issue[@attribute_name].gsub(@find, @replace) }
32     end
33   end
34
35   def replace
36     Issue.transaction do
37       @issues.each do |issue|
38         logger.info "#{issue.id}"
39         issue.safe_attributes = {@attribute_name => issue[@attribute_name].gsub(@find, @replace)}
40         issue.save!
41       end
42     end
43
44     redirect_to params[:back_url]
45   end
46
47 private
48   # rails filter
49   def check_first_issue
50     if @issues.empty?
51       logger.warn "### quick edit ### issues not found."
52       render_404
53     end
54
55     @issue = @issues[0]
56   end
57
58   # rails filter
59   def check_target_specifier
60     @target_specifier = params[:target_specifier]
61     if @target_specifier.nil?
62       logger.warn "### quick edit ### missing target specifier."
63       render_404
64       return
65     end
66
67     parsed = parse_target_specifier(@target_specifier)
68     if parsed.nil? || parsed.empty?
69       logger.warn "### quick edit ### invalid target specifier. target_specifier=" + @target_specifier
70       render_404
71       return
72     end
73
74     @attribute_name = parsed[0]
75     if parsed.size == 2
76       @custom_field_id = parsed[1]
77     else
78       @custom_field_id = ""
79     end
80     unless @issue.safe_attribute_names.include?(@attribute_name)
81       logger.warn "### quick edit ### no safe attribute. target_specifier=" + @target_specifier
82       render_404
83     end
84   end
85
86   def check_replace_args
87     unless @attribute_name.include?('subject')
88       logger.warn "### quick edit ### no support. target_specifier=" + @target_specifier
89       render_400
90       return
91     end
92
93     @find = params[:find]
94     if @find.nil? || @find == ""
95       logger.warn "### quick edit ### missing params[find]."
96       render_400
97       return
98     end
99
100     if @find.length > 127
101       logger.warn "### quick edit ### length over params[find]."
102       render_400
103       return
104     end
105
106     @replace = params[:replace]
107     if @replace.length > 127
108       logger.warn "### quick edit ### length over params[replace]."
109       render_400
110       return
111     end
112   end
113
114   def get_input_dialog_params_for_core_fields(issue, target_specifier)
115     attribute_name = parse_target_specifier(target_specifier)[0]
116
117     caption = get_attribute_caption(attribute_name)
118     field_type = get_attribute_type(attribute_name)
119     default_value = issue[attribute_name]
120     default_value = "" if default_value.nil?
121     validation_pattern = get_field_validation_pattern(field_type)
122     help_message = get_field_help_message(field_type)
123     clear_pseudo_value = nil
124     clear_pseudo_value = 'none' if %w(parent_issue_id start_date due_date estimated_hours).include?(attribute_name)
125
126     ret =
127       { :attribute_name => attribute_name.to_sym,
128         :caption => caption,
129         :target_specifier => target_specifier,
130         :field_type => field_type,
131         :default_value => default_value,
132         :validation_pattern => validation_pattern,
133         :help_message => help_message,
134         :clear_pseudo_value => clear_pseudo_value
135       }
136   end
137
138   def get_input_dialog_params_for_custom_fields(issue, target_specifier, custom_field)
139     attribute_name = parse_target_specifier(target_specifier)[0]
140
141     caption = custom_field.name
142     field_type = custom_field.field_format.to_sym
143     default_value = issue.editable_custom_field_values.detect {|v| v.custom_field_id == custom_field.id}
144     default_value = "" if default_value.nil?
145     validation_pattern = get_field_validation_pattern(field_type)
146     help_message = get_field_help_message(field_type)
147
148     ret =
149       { :attribute_name => attribute_name.to_sym,
150         :caption => caption,
151         :target_specifier => target_specifier,
152         :field_type => field_type,
153         :default_value => default_value,
154         :validation_pattern => validation_pattern,
155         :help_message => help_message,
156         :clear_pseudo_value => '__none__'
157       }
158   end
159
160   def get_field_validation_pattern(field_type)
161      case field_type.to_sym
162      when :string
163         pattern = ''
164      when :text
165         pattern = ''
166      when :int
167         pattern = '\d+'
168      when :float
169         pattern = '^[+-]?(\d+|\d*\.\d+|\d+\.\d+)($|[eE][+-]?\d+$)'
170      when :date
171         pattern = '\d{4}-\d{2}-\d{2}'
172      end
173   end
174
175   def get_field_help_message(field_type)
176     help_message= l(:text_edit_confirm)
177     help_message += " (yyyy-mm-dd)" if field_type == :date
178     help_message
179   end
180
181 end