Class: Redmine::CustomFieldFormat

Inherits:
Object
  • Object
show all
Includes:
I18n
Defined in:
lib/redmine/custom_field_format.rb

Constant Summary collapse

@@available =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18n

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

Constructor Details

#initialize(name, options = {}) ⇒ CustomFieldFormat

Returns a new instance of CustomFieldFormat.



27
28
29
30
31
# File 'lib/redmine/custom_field_format.rb', line 27

def initialize(name, options={})
  self.name = name
  self.label = options[:label]
  self.order = options[:order]
end

Instance Attribute Details

#labelObject

Returns the value of attribute label.



25
26
27
# File 'lib/redmine/custom_field_format.rb', line 25

def label
  @label
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/redmine/custom_field_format.rb', line 25

def name
  @name
end

#orderObject

Returns the value of attribute order.



25
26
27
# File 'lib/redmine/custom_field_format.rb', line 25

def order
  @order
end

Class Method Details

.as_selectObject

Return an array of custom field formats which can be used in select_tag



82
83
84
85
86
87
88
# File 'lib/redmine/custom_field_format.rb', line 82

def as_select
  @@available.values.sort {|a,b|
    a.order <=> b.order
  }.collect {|custom_field_format|
    [ l(custom_field_format.label), custom_field_format.name ]
  }
end

.available_formatsObject



68
69
70
# File 'lib/redmine/custom_field_format.rb', line 68

def available_formats
  @@available.keys
end

.find_by_name(name) ⇒ Object



72
73
74
# File 'lib/redmine/custom_field_format.rb', line 72

def find_by_name(name)
  @@available[name.to_s]
end

.format_value(value, field_format) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/redmine/custom_field_format.rb', line 90

def format_value(value, field_format)
  return "" unless value && !value.empty?

  if format_type = find_by_name(field_format)
    format_type.format(value)
  else
    value
  end
end

.label_for(name) ⇒ Object



76
77
78
79
# File 'lib/redmine/custom_field_format.rb', line 76

def label_for(name)
  format = @@available[name.to_s]
  format.label if format
end

.map {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



59
60
61
# File 'lib/redmine/custom_field_format.rb', line 59

def map(&block)
  yield self
end

.register(custom_field_format, options = {}) ⇒ Object

Registers a custom field format



64
65
66
# File 'lib/redmine/custom_field_format.rb', line 64

def register(custom_field_format, options={})
  @@available[custom_field_format.name] = custom_field_format unless @@available.keys.include?(custom_field_format.name)
end

Instance Method Details

#edit_asObject

Allow displaying the edit type of another field_format

Example: display a custom field as a list



54
55
56
# File 'lib/redmine/custom_field_format.rb', line 54

def edit_as
  name
end

#format(value) ⇒ Object



33
34
35
# File 'lib/redmine/custom_field_format.rb', line 33

def format(value)
  send "format_as_#{name}", value
end

#format_as_bool(value) ⇒ Object



41
42
43
# File 'lib/redmine/custom_field_format.rb', line 41

def format_as_bool(value)
  l(value == "1" ? :general_text_Yes : :general_text_No)
end

#format_as_date(value) ⇒ Object



37
38
39
# File 'lib/redmine/custom_field_format.rb', line 37

def format_as_date(value)
  begin; format_date(value.to_date); rescue; value end
end