Module: Contactable::ClassMethods

Defined in:
lib/contactable.rb

Overview

This extension provides contact information to an active record model.

Examples:

class Person < ActiveRecord::Base
  contactable
end

Instance Method Summary collapse

Instance Method Details

#contactable(*args, &block) ⇒ Object

Configuration options are:



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/contactable.rb', line 16

def contactable(*args, &block)
  # default options
  options = {}
  options.merge!(args.pop) if args.last.kind_of? Hash

  class_eval <<-EOV
    def name_part_columns
      [:first_name, :middle_name, :last_name]
    end

    def name_parts
      name_parts = []
        name_part_columns.each do |name_part|
          name_parts << self.send(name_part) if self.respond_to?(name_part)
        end
      name_parts
    end

    # contactable#name() supports 2 options:
    # If a name column exists that one is used
    # If not use first, middle and last_name columns (only if available)
    def name
      if self[:name]
        self[:name]
      else
        name_parts.join(' ')
      end
    end

    def initials
      if self[:name]
        self[:name].tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + '.'}.join
      else
        name_parts.map {|name_part| name_part.chars.first.upcase.to_s + '.'}.join
      end
    end

    def age
      if self.respond_to?(:birthday) && !self.birthday.nil?
        now = Time.now.utc.to_date
        now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)
      end
    end

    has_many                      :addresses,           :as => :owner
    has_many                      :emails,              :as => :owner
    has_many                      :instant_messengers,  :as => :owner
    has_many                      :phones,              :as => :owner
    has_many                      :websites,            :as => :owner
    has_many                      :identifications,     :as => :owner

    accepts_nested_attributes_for :addresses,
      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
    accepts_nested_attributes_for :emails,
      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
    accepts_nested_attributes_for :instant_messengers,
      :reject_if => proc { |attributes| attributes['nick'].blank? }, :allow_destroy => true
    accepts_nested_attributes_for :phones,
      :reject_if => proc { |attributes| attributes['number'].blank? }, :allow_destroy => true
    accepts_nested_attributes_for :websites,
      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
    accepts_nested_attributes_for :identifications,
      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
  EOV
end