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


24
25
26
27
28
29
30
# File 'lib/redmine/custom_field_format.rb', line 24

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

Instance Attribute Details

#class_namesObject

Returns the value of attribute class_names


22
23
24
# File 'lib/redmine/custom_field_format.rb', line 22

def class_names
  @class_names
end

#edit_asObject

Returns the value of attribute edit_as


22
23
24
# File 'lib/redmine/custom_field_format.rb', line 22

def edit_as
  @edit_as
end

#labelObject

Returns the value of attribute label


22
23
24
# File 'lib/redmine/custom_field_format.rb', line 22

def label
  @label
end

#nameObject

Returns the value of attribute name


22
23
24
# File 'lib/redmine/custom_field_format.rb', line 22

def name
  @name
end

#orderObject

Returns the value of attribute order


22
23
24
# File 'lib/redmine/custom_field_format.rb', line 22

def order
  @order
end

Class Method Details

.as_select(class_name = nil) ⇒ Object

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


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

def as_select(class_name=nil)
  fields = @@available.values
  fields = fields.select {|field| field.class_names.nil? || field.class_names.include?(class_name)}
  fields.sort {|a,b|
    a.order <=> b.order
  }.collect {|custom_field_format|
    [ l(custom_field_format.label), custom_field_format.name ]
  }
end

.available_formatsObject


66
67
68
# File 'lib/redmine/custom_field_format.rb', line 66

def available_formats
  @@available.keys
end

.find_by_name(name) ⇒ Object


70
71
72
# File 'lib/redmine/custom_field_format.rb', line 70

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


74
75
76
77
# File 'lib/redmine/custom_field_format.rb', line 74

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

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

Yields:

  • (_self)

Yield Parameters:


57
58
59
# File 'lib/redmine/custom_field_format.rb', line 57

def map(&block)
  yield self
end

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

Registers a custom field format


62
63
64
# File 'lib/redmine/custom_field_format.rb', line 62

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

#format(value) ⇒ Object


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

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

#format_as_bool(value) ⇒ Object


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

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

#format_as_date(value) ⇒ Object


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

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