Class: Redmine::FieldFormat::Base

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, Helpers::URL, I18n, Singleton
Defined in:
lib/redmine/field_format.rb

Direct Known Subclasses

List, Unbounded

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::URL

#uri_with_safe_scheme?

Methods included from I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #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

Since:

  • 2.5.0



89
90
91
# File 'lib/redmine/field_format.rb', line 89

def self.field_attributes(*args)
  CustomField.store_accessor :format_store, *args
end

Instance Method Details

#before_custom_field_save(custom_field) ⇒ Object

Since:

  • 2.5.0



253
254
# File 'lib/redmine/field_format.rb', line 253

def before_custom_field_save(custom_field)
end

#bulk_edit_tag(view, tag_id, tag_name, custom_field, objects, value, options = {}) ⇒ Object

Since:

  • 2.5.0



232
233
234
235
# File 'lib/redmine/field_format.rb', line 232

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

Since:

  • 2.5.0



103
104
105
# File 'lib/redmine/field_format.rb', line 103

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

Since:

  • 2.5.0



120
121
122
# File 'lib/redmine/field_format.rb', line 120

def cast_single_value(custom_field, value, customized=nil)
  value.to_s
end

#cast_value(custom_field, value, customized = nil) ⇒ Object

Since:

  • 2.5.0



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/redmine/field_format.rb', line 107

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

Since:

  • 2.5.0



228
229
230
# File 'lib/redmine/field_format.rb', line 228

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

#formatted_custom_value(view, custom_value, html = false) ⇒ Object

Since:

  • 2.5.0



177
178
179
# File 'lib/redmine/field_format.rb', line 177

def formatted_custom_value(view, custom_value, html=false)
  formatted_value(view, custom_value.custom_field, custom_value.value, custom_value.customized, html)
end

#formatted_value(view, custom_field, value, customized = nil, html = false) ⇒ Object

Since:

  • 2.5.0



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/redmine/field_format.rb', line 181

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 {|text, url| view.link_to_if uri_with_safe_scheme?(url), text, url}
    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.

Since:

  • 2.5.0



266
267
268
# File 'lib/redmine/field_format.rb', line 266

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

Since:

  • 2.5.0



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/redmine/field_format.rb', line 271

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

#labelObject

Since:

  • 2.5.0



99
100
101
# File 'lib/redmine/field_format.rb', line 99

def label
  "label_#{name}"
end

#nameObject

Since:

  • 2.5.0



95
96
97
# File 'lib/redmine/field_format.rb', line 95

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.

Since:

  • 2.5.0



259
260
261
262
# File 'lib/redmine/field_format.rb', line 259

def order_statement(custom_field)
  # COALESCE is here to make sure that blank and NULL values are sorted equally
  "COALESCE(#{join_alias custom_field}.value, '')"
end

#possible_custom_value_options(custom_value) ⇒ Object

Since:

  • 2.5.0



128
129
130
# File 'lib/redmine/field_format.rb', line 128

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

Since:

  • 2.5.0



132
133
134
# File 'lib/redmine/field_format.rb', line 132

def possible_values_options(custom_field, object=nil)
  []
end

#query_filter_options(custom_field, query) ⇒ Object

Since:

  • 2.5.0



249
250
251
# File 'lib/redmine/field_format.rb', line 249

def query_filter_options(custom_field, query)
  {:type => :string}
end

#target_classObject

Since:

  • 2.5.0



124
125
126
# File 'lib/redmine/field_format.rb', line 124

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

Since:

  • 2.5.0



154
155
156
157
158
159
160
161
# File 'lib/redmine/field_format.rb', line 154

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

Since:

  • 2.5.0



165
166
167
168
169
170
171
# File 'lib/redmine/field_format.rb', line 165

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

Since:

  • 2.5.0



173
174
175
# File 'lib/redmine/field_format.rb', line 173

def validate_single_value(custom_field, value, customized=nil)
  []
end

#value_from_keyword(custom_field, keyword, object) ⇒ Object

Since:

  • 3.2.0



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/redmine/field_format.rb', line 136

def value_from_keyword(custom_field, keyword, object)
  possible_values_options = possible_values_options(custom_field, object)
  if possible_values_options.present?
    keyword = keyword.to_s
    if v = possible_values_options.detect {|text, id| keyword.casecmp(text)  == 0}
      if v.is_a?(Array)
        v.last
      else
        v
      end
    end
  else
    keyword
  end
end