Class: Rails::Contact::Contact

Inherits:
ApplicationRecord show all
Defined in:
app/models/rails/contact/contact.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#labels_csvObject

Returns the value of attribute labels_csv.



7
8
9
# File 'app/models/rails/contact/contact.rb', line 7

def labels_csv
  @labels_csv
end

Instance Method Details

#full_nameObject



26
27
28
# File 'app/models/rails/contact/contact.rb', line 26

def full_name
  [ given_name, family_name ].compact.join(" ").strip
end

#meta(key) ⇒ Object



38
39
40
# File 'app/models/rails/contact/contact.rb', line 38

def meta(key)
  [key.to_s]
end

#metadata_hashObject



51
52
53
54
55
56
57
58
59
60
# File 'app/models/rails/contact/contact.rb', line 51

def 
  value = 
  return value if value.is_a?(Hash)
  return {} if value.blank?

  parsed = JSON.parse(value)
  parsed.is_a?(Hash) ? parsed : {}
rescue JSON::ParserError
  {}
end

#primary_emailObject



30
31
32
# File 'app/models/rails/contact/contact.rb', line 30

def primary_email
  emails.find(&:primary?) || emails.first
end

#primary_phoneObject



34
35
36
# File 'app/models/rails/contact/contact.rb', line 34

def primary_phone
  phones.find(&:primary?) || phones.first
end

#set_labels_from_csv!(csv_string) ⇒ Object



42
43
44
45
# File 'app/models/rails/contact/contact.rb', line 42

def set_labels_from_csv!(csv_string)
  names = csv_string.to_s.split(",").map(&:strip).reject(&:empty?).uniq
  self.labels = names.map { |name| Rails::Contact::Label.find_or_create_by!(name: name) }
end