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 "    def name_part_columns\n      [:first_name, :middle_name, :last_name]\n    end\n\n    def name_parts\n      name_parts = []\n        name_part_columns.each do |name_part|\n          name_parts << self.send(name_part) if self.respond_to?(name_part)\n        end\n      name_parts\n    end\n\n    # contactable#name() supports 2 options:\n    # If a name column exists that one is used\n    # If not use first, middle and last_name columns (only if available)\n    def name\n      if self[:name]\n        self[:name]\n      else\n        name_parts.join(' ')\n      end\n    end\n\n    def initials\n      if self[:name]\n        self[:name].tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + '.'}.join\n      else\n        name_parts.map {|name_part| name_part.chars.first.upcase.to_s + '.'}.join\n      end\n    end\n\n    def age\n      if self.respond_to?(:birthday) && !self.birthday.nil?\n        now = Time.now.utc.to_date\n        now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)\n      end\n    end\n\n    has_many                      :addresses,           :as => :owner\n    has_many                      :emails,              :as => :owner\n    has_many                      :instant_messengers,  :as => :owner\n    has_many                      :phones,              :as => :owner\n    has_many                      :websites,            :as => :owner\n    has_many                      :identifications,     :as => :owner\n\n    accepts_nested_attributes_for :addresses,\n      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true\n    accepts_nested_attributes_for :emails,\n      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true\n    accepts_nested_attributes_for :instant_messengers,\n      :reject_if => proc { |attributes| attributes['nick'].blank? }, :allow_destroy => true\n    accepts_nested_attributes_for :phones,\n      :reject_if => proc { |attributes| attributes['number'].blank? }, :allow_destroy => true\n    accepts_nested_attributes_for :websites,\n      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true\n    accepts_nested_attributes_for :identifications,\n      :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true\n  EOV\nend\n"