Module: IsContacted::InstanceMethods

Defined in:
lib/is_contacted.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_contactObject

Returns the value of attribute _contact.



21
22
23
# File 'lib/is_contacted.rb', line 21

def _contact
  @_contact
end

Class Method Details

.included(model) ⇒ Object



22
23
24
25
26
# File 'lib/is_contacted.rb', line 22

def self.included(model)
  model.class_eval do
    before_save   :update_contact
  end
end

Instance Method Details

#update_contactObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/is_contacted.rb', line 28

def update_contact
  unless self._contact.blank?
    _test_contact = Contact.build_for(:value =>self._contact)
    _test_contact.valid? 
    _new_contact = Contact.find_by_value(_test_contact.value)
    if _new_contact
      self.contact_id = _new_contact.id
      self.user_id = _new_contact.user_id
    else
      _test_contact.save
      self.contact_id = _test_contact.id
    end
  end
  if contact_id.blank? && self.respond_to?(:user_id)
    user = User.find_by_id(self.user_id)
    contact = Contact.find_by_value(user.) if user
    self.contact_id = contact.id if contact
  end
end