Class: Gaku::PersonDecorator

Inherits:
Draper::Decorator
  • Object
show all
Defined in:
app/decorators/gaku/person_decorator.rb

Instance Method Summary collapse

Instance Method Details

#formatted_nameObject

returns full name complete with formatting [if any]



8
9
10
# File 'app/decorators/gaku/person_decorator.rb', line 8

def formatted_name
  student_names
end

#formatted_name_readingObject



12
13
14
# File 'app/decorators/gaku/person_decorator.rb', line 12

def formatted_name_reading
  student_names reading: true
end

#full_nameObject



16
17
18
# File 'app/decorators/gaku/person_decorator.rb', line 16

def full_name
  "#{object.surname} #{object.name}"
end

#full_name_readingObject



20
21
22
# File 'app/decorators/gaku/person_decorator.rb', line 20

def full_name_reading
  student_names without_formating: true, reading: true
end

#sex_and_birthObject



3
4
5
# File 'app/decorators/gaku/person_decorator.rb', line 3

def sex_and_birth
  [h.gender(object), object.birth_date.to_s].reject(&:empty?).join(', ')
end

#student_names(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/decorators/gaku/person_decorator.rb', line 24

def student_names(options = {})
  @names_preset ||= Gaku::Preset.names
  preset = @names_preset

  if options[:without_formating]
    preset_without_format = ''
    preset.gsub(/%(\w+)/) { |n|  preset_without_format << n + ' ' }
    preset = preset_without_format.strip
  end

  if options[:reading] && object.name_reading.blank? &&
    object.surname_reading.blank? &&
    object.middle_name_reading.blank?
    return ''
  end

  reading = options[:reading]
  return reading ? object.phonetic_reading : object.to_s if preset.blank?
  result = preset.gsub(/%(\w+)/) do |name|
    case name
    when '%first' then proper_name(:name, reading)
    when '%middle' then proper_name(:middle_name, reading)
    when '%last' then proper_name(:surname, reading)
    end
  end
  return result.gsub(/[^[[:word:]]\s]/, '').gsub(/\s+/, ' ').strip if object.middle_name.blank?
  result.gsub(/\s+/, ' ').strip
end