Class: Redmine::FieldFormat::Base

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, 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 I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_time, included, #l, #l_hours, #l_or_humanize, #languages_options, #ll, #month_name, #set_language_if_valid, #valid_languages

Class Method Details

.field_attributes(*args) ⇒ Object


79
80
81
# File 'lib/redmine/field_format.rb', line 79

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

Instance Method Details

#before_custom_field_save(custom_field) ⇒ Object


215
216
# File 'lib/redmine/field_format.rb', line 215

def before_custom_field_save(custom_field)
end

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


194
195
196
197
# File 'lib/redmine/field_format.rb', line 194

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


93
94
95
# File 'lib/redmine/field_format.rb', line 93

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


110
111
112
# File 'lib/redmine/field_format.rb', line 110

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

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


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/redmine/field_format.rb', line 97

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


190
191
192
# File 'lib/redmine/field_format.rb', line 190

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


146
147
148
# File 'lib/redmine/field_format.rb', line 146

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


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/redmine/field_format.rb', line 150

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 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.


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

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


233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/redmine/field_format.rb', line 233

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


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

def label
  "label_#{name}"
end

#nameObject


85
86
87
# File 'lib/redmine/field_format.rb', line 85

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.


221
222
223
224
# File 'lib/redmine/field_format.rb', line 221

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


118
119
120
# File 'lib/redmine/field_format.rb', line 118

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


122
123
124
# File 'lib/redmine/field_format.rb', line 122

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

#query_filter_options(custom_field, query) ⇒ Object


211
212
213
# File 'lib/redmine/field_format.rb', line 211

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

#target_classObject


114
115
116
# File 'lib/redmine/field_format.rb', line 114

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


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

def validate_custom_field(custom_field)
  []
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


134
135
136
137
138
139
140
# File 'lib/redmine/field_format.rb', line 134

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


142
143
144
# File 'lib/redmine/field_format.rb', line 142

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