Module: NameableRecord::ActiveRecordExtensions::ActsMethods

Defined in:
lib/nameable_record/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#has_name(*args) ⇒ Object Also known as: has_names



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nameable_record/active_record_extensions.rb', line 9

def has_name( *args )
  args.each do |attr|

    composed_of attr, :class_name => "NameableRecord::Name", :mapping => [["#{attr}_last",   "last"],
                                                                          ["#{attr}_first",  "first"],
                                                                          ["#{attr}_prefix", "prefix"],
                                                                          ["#{attr}_middle", "middle"],
                                                                          ["#{attr}_suffix", "suffix"]]

    define_method "conversational_#{attr}" do
      [
        send( "#{attr}_prefix" ),
        send( "#{attr}_first" ),
        send( "#{attr}_middle" ),
        send( "#{attr}_last" ),
        send( "#{attr}_suffix" )
      ].reject( &:blank? ).
        join( ' ' )
    end

    define_method "sortable_#{attr}" do
      [
        send( "#{attr}_last" ),
        [
          send( "#{attr}_prefix" ),
          send( "#{attr}_first" ),
          send( "#{attr}_middle" ),
          send( "#{attr}_suffix" )
        ].reject( &:blank? ).
          join( ' ' )
      ].reject( &:blank? ).
        join( ', ' )
    end

  end
end