Sha256: ce3262b3acbd5fef4bdc88bd0455da48bfb93a36eb470ad68aebc018586b02c4

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

module Gaku
  class PersonDecorator < Draper::Decorator
    def sex_and_birth
      [h.gender(object), object.birth_date.to_s].reject(&:empty?).join(', ')
    end

    # returns full name complete with formatting [if any]
    def formatted_name
      student_names
    end

    def formatted_name_reading
      student_names reading: true
    end

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

    def full_name_reading
      student_names without_formating: true, reading: true
    end

    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

    private

    def proper_name(attribute, reading)
      reading ? object.send(attribute.to_s + '_reading') : object.send(attribute)
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
gaku_core-0.3.0 app/decorators/gaku/person_decorator.rb
gaku_core-0.3.0.pre.4 app/decorators/gaku/person_decorator.rb
gaku_core-0.3.0.pre.3 app/decorators/gaku/person_decorator.rb
gaku_core-0.3.0.pre.2 app/decorators/gaku/person_decorator.rb
gaku_core-0.3.0.pre.1 app/decorators/gaku/person_decorator.rb
gaku_core-0.3.0.pre.0 app/decorators/gaku/person_decorator.rb
gaku-0.2.4 core/app/decorators/gaku/person_decorator.rb
gaku_core-0.2.4 app/decorators/gaku/person_decorator.rb
gaku-0.2.3 core/app/decorators/gaku/person_decorator.rb
gaku_core-0.2.3 app/decorators/gaku/person_decorator.rb
gaku-0.2.2 core/app/decorators/gaku/person_decorator.rb
gaku_core-0.2.2 app/decorators/gaku/person_decorator.rb
gaku-0.2.1 core/app/decorators/gaku/person_decorator.rb
gaku_core-0.2.1 app/decorators/gaku/person_decorator.rb
gaku-0.2.0 core/app/decorators/gaku/person_decorator.rb
gaku_core-0.2.0 app/decorators/gaku/person_decorator.rb