Class: Redmine::FieldFormat::Base
- Inherits:
-
Object
- Object
- Redmine::FieldFormat::Base
show all
- Includes:
- ERB::Util, Helpers::URL, I18n, Singleton
- Defined in:
- lib/redmine/field_format.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#after_save_custom_value(custom_field, custom_value) ⇒ Object
CustomValue after_save callback.
-
#before_custom_field_save(custom_field) ⇒ Object
-
#bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options = {}) ⇒ Object
-
#cast_custom_value(custom_value) ⇒ Object
-
#cast_single_value(custom_field, value, customized = nil) ⇒ Object
-
#cast_value(custom_field, value, customized = nil) ⇒ Object
-
#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object
-
#formatted_custom_value(view, custom_value, html = false) ⇒ Object
-
#formatted_value(view, custom_field, value, customized = nil, html = false) ⇒ Object
-
#group_statement(custom_field) ⇒ Object
Returns a GROUP BY clause that can used to group by custom value Returns
nil if the custom field can not be used for grouping.
-
#join_for_order_statement(custom_field) ⇒ Object
Returns a JOIN clause that is added to the query when sorting by custom
values.
-
#label ⇒ Object
-
#name ⇒ Object
-
#order_statement(custom_field) ⇒ Object
Returns a ORDER BY clause that can used to sort customized objects by their
value of the custom field.
-
#possible_custom_value_options(custom_value) ⇒ Object
-
#possible_values_options(custom_field, object = nil) ⇒ Object
-
#query_filter_options(custom_field, query) ⇒ Object
-
#set_custom_field_value(custom_field, custom_field_value, value) ⇒ Object
-
#target_class ⇒ Object
-
#validate_custom_field(custom_field) ⇒ Object
Returns the validation errors for custom_field Should return an empty array
if custom_field is valid.
-
#validate_custom_value(custom_value) ⇒ Object
Returns the validation error messages for custom_value Should return an
empty array if custom_value is valid custom_value is a CustomFieldValue.
-
#validate_single_value(custom_field, value, customized = nil) ⇒ Object
-
#value_from_keyword(custom_field, keyword, object) ⇒ Object
#uri_with_safe_scheme?
Methods included from I18n
#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages
Class Method Details
.field_attributes(*args) ⇒ Object
107
108
109
|
# File 'lib/redmine/field_format.rb', line 107
def self.field_attributes(*args)
CustomField.store_accessor :format_store, *args
end
|
Instance Method Details
#after_save_custom_value(custom_field, custom_value) ⇒ Object
CustomValue after_save callback
236
237
|
# File 'lib/redmine/field_format.rb', line 236
def after_save_custom_value(custom_field, custom_value)
end
|
#before_custom_field_save(custom_field) ⇒ Object
318
319
|
# File 'lib/redmine/field_format.rb', line 318
def before_custom_field_save(custom_field)
end
|
#bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options = {}) ⇒ Object
297
298
299
300
|
# File 'lib/redmine/field_format.rb', line 297
def bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options={})
view.text_field_tag(tag_name, value, options.merge(:id => tag_id)) +
bulk_clear_tag(view, tag_id, tag_name, custom_field, value)
end
|
#cast_custom_value(custom_value) ⇒ Object
134
135
136
|
# File 'lib/redmine/field_format.rb', line 134
def cast_custom_value(custom_value)
cast_value(custom_value.custom_field, custom_value.value, custom_value.customized)
end
|
#cast_single_value(custom_field, value, customized = nil) ⇒ Object
151
152
153
|
# File 'lib/redmine/field_format.rb', line 151
def cast_single_value(custom_field, value, customized=nil)
value.to_s
end
|
#cast_value(custom_field, value, customized = nil) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/redmine/field_format.rb', line 138
def cast_value(custom_field, value, customized=nil)
if value.blank?
nil
elsif value.is_a?(Array)
casted = value.map do |v|
cast_single_value(custom_field, v, customized)
end
casted.compact.sort
else
cast_single_value(custom_field, value, customized)
end
end
|
#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object
293
294
295
|
# File 'lib/redmine/field_format.rb', line 293
def edit_tag(view, tag_id, tag_name, custom_value, options={})
view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id))
end
|
239
240
241
|
# File 'lib/redmine/field_format.rb', line 239
def formatted_custom_value(view, custom_value, html=false)
formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
end
|
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/redmine/field_format.rb', line 243
def formatted_value(view, custom_field, value, customized=nil, html=false)
casted = cast_value(custom_field, value, customized)
if html && custom_field.url_pattern.present?
texts_and_urls = Array.wrap(casted).map do |single_value|
text = view.format_object(single_value, false).to_s
url = url_from_pattern(custom_field, single_value, customized)
[text, url]
end
links = texts_and_urls.sort_by(&:first).map do |text, url|
css_class = (url =~ /^https?:\/\//) ? 'external' : nil
view.link_to_if uri_with_safe_scheme?(url), text, url, :class => css_class
end
links.join(', ').html_safe
else
casted
end
end
|
#group_statement(custom_field) ⇒ Object
Returns a GROUP BY clause that can used to group by custom value Returns
nil if the custom field can not be used for grouping.
331
332
333
|
# File 'lib/redmine/field_format.rb', line 331
def group_statement(custom_field)
nil
end
|
#join_for_order_statement(custom_field) ⇒ Object
Returns a JOIN clause that is added to the query when sorting by custom
values
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/redmine/field_format.rb', line 336
def join_for_order_statement(custom_field)
alias_name = join_alias(custom_field)
"LEFT OUTER JOIN #{CustomValue.table_name} #{alias_name}" +
" ON #{alias_name}.customized_type = '#{custom_field.class.customized_class.base_class.name}'" +
" AND #{alias_name}.customized_id = #{custom_field.class.customized_class.table_name}.id" +
" AND #{alias_name}.custom_field_id = #{custom_field.id}" +
" AND (#{custom_field.visibility_by_project_condition})" +
" AND #{alias_name}.value <> ''" +
" AND #{alias_name}.id = (SELECT max(#{alias_name}_2.id) FROM #{CustomValue.table_name} #{alias_name}_2" +
" WHERE #{alias_name}_2.customized_type = #{alias_name}.customized_type" +
" AND #{alias_name}_2.customized_id = #{alias_name}.customized_id" +
" AND #{alias_name}_2.custom_field_id = #{alias_name}.custom_field_id)"
end
|
#label ⇒ Object
117
118
119
|
# File 'lib/redmine/field_format.rb', line 117
def label
"label_#{name}"
end
|
#name ⇒ Object
113
114
115
|
# File 'lib/redmine/field_format.rb', line 113
def name
self.class.format_name
end
|
#order_statement(custom_field) ⇒ Object
Returns a ORDER BY clause that can used to sort customized objects by their
value of the custom field. Returns nil if the custom field can not be used
for sorting.
324
325
326
327
|
# File 'lib/redmine/field_format.rb', line 324
def order_statement(custom_field)
Arel.sql "COALESCE(#{join_alias custom_field}.value, '')"
end
|
#possible_custom_value_options(custom_value) ⇒ Object
159
160
161
|
# File 'lib/redmine/field_format.rb', line 159
def possible_custom_value_options(custom_value)
possible_values_options(custom_value.custom_field, custom_value.customized)
end
|
#possible_values_options(custom_field, object = nil) ⇒ Object
163
164
165
|
# File 'lib/redmine/field_format.rb', line 163
def possible_values_options(custom_field, object=nil)
[]
end
|
#query_filter_options(custom_field, query) ⇒ Object
314
315
316
|
# File 'lib/redmine/field_format.rb', line 314
def query_filter_options(custom_field, query)
{:type => :string}
end
|
#set_custom_field_value(custom_field, custom_field_value, value) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/redmine/field_format.rb', line 121
def set_custom_field_value(custom_field, custom_field_value, value)
if value.is_a?(Array)
value = value.map(&:to_s).reject{|v| v==''}.uniq
if value.empty?
value << ''
end
else
value = value.to_s
end
value
end
|
#target_class ⇒ Object
155
156
157
|
# File 'lib/redmine/field_format.rb', line 155
def target_class
nil
end
|
#validate_custom_field(custom_field) ⇒ Object
Returns the validation errors for custom_field Should return an empty array
if custom_field is valid
211
212
213
214
215
216
217
218
|
# File 'lib/redmine/field_format.rb', line 211
def validate_custom_field(custom_field)
errors = []
pattern = custom_field.url_pattern
if pattern.present? && !uri_with_safe_scheme?(url_pattern_without_tokens(pattern))
errors << [:url_pattern, :invalid]
end
errors
end
|
#validate_custom_value(custom_value) ⇒ Object
Returns the validation error messages for custom_value Should return an
empty array if custom_value is valid custom_value is a CustomFieldValue.
223
224
225
226
227
228
229
|
# File 'lib/redmine/field_format.rb', line 223
def validate_custom_value(custom_value)
values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
errors = values.map do |value|
validate_single_value(custom_value.custom_field, value, custom_value.customized)
end
errors.flatten.uniq
end
|
#validate_single_value(custom_field, value, customized = nil) ⇒ Object
231
232
233
|
# File 'lib/redmine/field_format.rb', line 231
def validate_single_value(custom_field, value, customized=nil)
[]
end
|
#value_from_keyword(custom_field, keyword, object) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/redmine/field_format.rb', line 167
def value_from_keyword(custom_field, keyword, object)
possible_values_options = possible_values_options(custom_field, object)
if possible_values_options.present?
parse_keyword(custom_field, keyword) do |k|
if v = possible_values_options.detect {|text, id| k.casecmp(text) == 0}
if v.is_a?(Array)
v.last
else
v
end
end
end
else
keyword
end
end
|