OSDN Git Service

add field description
[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
7   def edit
8     @dialog_params = nil
9     @issue.available_custom_fields.each do |f|
10       custom_field_name = 'issue[custom_field_values][%d]' % f.id
11       if custom_field_name == @target_specifier
12         @dialog_params = get_input_dialog_params_for_custom_fields(@issue, @target_specifier, f)
13         @dialog_params[:description] = f.description.presence
14       end
15     end
16     if @dialog_params.nil?
17       @dialog_params = get_input_dialog_params_for_core_fields(@issue, @target_specifier)
18       @dialog_params[:description] = nil
19     end
20     @dialog_params[:description] = nil if (@dialog_params[:description] == "")
21     @dialog_params[:issue_ids] = params[:ids]
22     @dialog_params[:back_url] = params[:back_url]
23     @dialog_params[:default_value] = params[:default_value] unless (params[:default_value].nil?)
24   end
25
26 private
27   # rails filter
28   def check_first_issue
29     if @issues.empty?
30       logger.warn "### quick edit ### issues not found."
31       render_404
32     end
33
34     @issue = @issues[0]
35   end
36
37   # rails filter
38   def check_target_specifier
39     @target_specifier = params[:target_specifier]
40     parsed = parse_target_specifier(@target_specifier)
41     if parsed.nil? || parsed.empty?
42       logger.warn "### quick edit ### invalid target specifier. target_specifier=" + @target_specifier
43       render_404
44     end
45
46     attribute_name = parsed[0]
47     unless @issue.safe_attribute_names.include?(attribute_name)
48       logger.warn "### quick edit ### no safe attribute. target_specifier=" + @target_specifier
49       render_404
50     end
51   end
52
53   def get_input_dialog_params_for_core_fields(issue, target_specifier)
54     attribute_name = parse_target_specifier(target_specifier)[0]
55
56     caption = get_attribute_caption(attribute_name)
57     field_type = get_attribute_type(attribute_name)
58     default_value = issue[attribute_name]
59     default_value = "" if default_value.nil?
60     validation_pattern = get_field_validation_pattern(field_type)
61     help_message = get_field_help_message(field_type)
62     ret =
63       { :caption => caption,
64         :target_specifier => target_specifier,
65         :field_type => field_type,
66         :default_value => default_value,
67         :validation_pattern => validation_pattern,
68         :help_message => help_message
69       }
70   end
71
72   def get_input_dialog_params_for_custom_fields(issue, target_specifier, custom_field)
73     caption = custom_field.name
74     field_type = custom_field.field_format.to_sym
75     default_value = issue.editable_custom_field_values.detect {|v| v.custom_field_id == custom_field.id}
76     default_value = "" if default_value.nil?
77     validation_pattern = get_field_validation_pattern(field_type)
78     help_message = get_field_help_message(field_type)
79
80     ret =
81       { :caption => caption,
82         :target_specifier => target_specifier,
83         :field_type => field_type,
84         :default_value => default_value,
85         :validation_pattern => validation_pattern,
86         :help_message => help_message
87       }
88   end
89
90   def get_field_validation_pattern(field_type)
91      case field_type.to_sym
92      when :string
93         pattern = ''
94      when :text
95         pattern = ''
96      when :int
97         pattern = '\d+'
98      when :date
99         pattern = '\d{4}-\d{2}-\d{2}'
100      end
101   end
102
103   def get_field_help_message(field_type)
104     help_message= l(:text_edit_confirm)
105     help_message += " (yyyy-mm-dd)" if field_type == :date
106     help_message
107   end
108
109 end