Class: Osm::Member::Contact

Inherits:
Osm::Model show all
Defined in:
lib/osm/member.rb

Constant Summary

Constants inherited from Osm::Model

Osm::Model::SORT_BY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Osm::Model

#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Contact



# File 'lib/osm/member.rb', line 660


Instance Attribute Details

#additional_informationDirtyHashy



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#additional_information_labelsDirtyHashy



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_1String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_2String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_3String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_4String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#first_nameString



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#last_nameString



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#phone_1String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#phone_2String



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#postcodeString



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

Instance Method Details

#all_phonesArray<String>

Get an array of all phone numbers for the contact



673
674
675
# File 'lib/osm/member.rb', line 673

def all_phones
  [phone_1, phone_2].select{ |n| !n.blank? }.map{ |n| n.gsub(/[^\d\+]/, '') }
end

#name(seperator = ' ') ⇒ String

Get the full name



667
668
669
# File 'lib/osm/member.rb', line 667

def name(seperator=' ')
  return [first_name, last_name].select{ |i| !i.blank? }.join(seperator)
end

#update(api, member, force = false) ⇒ Boolean

Update the contact in OSM

Raises:



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/osm/member.rb', line 683

def update(api, member, force=false)
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
  require_ability_to(api, :write, :member, member.section_id)

  attribute_map = {
    'first_name' => 'data[firstname]',
    'last_name' => 'data[lastname]',
    'surgery' => 'data[surgery]',
    'address_1' => 'data[address1]',
    'address_2' => 'data[address2]',
    'address_3' => 'data[address3]',
    'address_4' => 'data[address4]',
    'postcode' => 'data[postcode]',
    'phone_1' => 'data[phone1]',
    'receive_phone_1' => 'data[phone1_sms]',
    'phone_2' => 'data[phone2]',
    'receive_phone_2' => 'data[phone2_sms]',
    'email_1' => 'data[email1]',
    'receive_email_1' => 'data[email1_leaders]',
    'email_2' => 'data[email2]',
    'receive_email_2' => 'data[email2_leaders]',
  } # our name => OSM name

  data = {}
  attributes.keys.select{ |a| !['additional_information', 'additional_information_labels'].include?(a) }.select{ |a| force || changed_attributes.include?(a) }.each do |attr|
    value = send(attr)
    value = 'yes' if value.eql?(true)
    data[attribute_map[attr]] = value
  end
  additional_information.keys.select{ |a| force || additional_information.changes.keys.include?(a) }.each do |attr|
    data["data[#{attr}]"] = additional_information[attr]
  end

  updated = true
  unless data.empty?
    result = api.perform_query("ext/customdata/?action=update&section_id=#{member.section_id}", {
      'associated_id' => member.id,
      'associated_type' => 'member',
      'context' => 'members',
      'group_id' => self.class::GROUP_ID,
    }.merge(data))
    updated = result.is_a?(Hash) && result['status'].eql?(true)
  end

  # Finish off
  if updated
    reset_changed_attributes
    additional_information.clean_up!
  end
  return updated
end