lib/osm/member.rb in osm-1.2.18.dev.9 vs lib/osm/member.rb in osm-1.2.18.dev.10

- old
+ new

@@ -9,13 +9,11 @@ GID_CUSTOM = 5 GID_MEMBER_CONTACT = 6 GID_FLOATING = 7 # Constants for column id - CUSTOM_FIELD_IDS_START_AT = 55 - CORE_FIELD_IDS_FINISH_AT = CUSTOM_FIELD_IDS_START_AT - 1 - CORE_FIELD_IDS = (1..54).to_a + CID_TITLE = 1 CID_FIRST_NAME = 2 CID_LAST_NAME = 3 CID_ADDRESS_1 = 7 CID_ADDRESS_2 = 8 CID_ADDRESS_3 = 9 @@ -29,16 +27,19 @@ CID_RECIEVE_PHONE_1 = 19 CID_PHONE_2 = 20 CID_RECIEVE_PHONE_2 = 21 CID_GENDER = 34 CID_SURGERY = 54 + CORE_FIELD_IDS = (1..21).to_a + [34, 54] # @!attribute [rw] id # @return [Fixnum] the id for the member # @!attribute [rw] section_id # @return [Fixnum] the section the member belongs to + # @!attribute [rw] title + # @return [String] the member's title (Mr, Mrs etc.) # @!attribute [rw] first_name # @return [String] the member's first name # @!attribute [rw] last_name # @return [String] the member's last name # @!attribute [rw] grouping_id @@ -76,10 +77,11 @@ # @!attribute [rw] doctor # @return [Osm::Member::DoctorContact, nil] the member's doctor (nil if hidden in OSM) attribute :id, :type => Integer attribute :section_id, :type => Integer + attribute :title, :type => String attribute :first_name, :type => String attribute :last_name, :type => String attribute :grouping_id, :type => Integer attribute :grouping_label, :type => String attribute :grouping_leader, :type => Integer @@ -97,11 +99,12 @@ attribute :secondary_contact, :type => Object attribute :emergency_contact, :type => Object attribute :doctor, :type => Object if ActiveModel::VERSION::MAJOR < 4 - attr_accessible :id, :section_id, :first_name, :last_name, :grouping_id, :grouping_leader, + attr_accessible :id, :section_id, :title, :first_name, :last_name, + :grouping_id, :grouping_leader, :date_of_birth, :started_section, :finished_section, :joined_movement, :age, :grouping_label, :grouping_leader_label, :gender, :additional_information, :additional_information_labels, :contact, :primary_contact, :secondary_contact, :emergency_contact, :doctor end @@ -160,33 +163,33 @@ custom_labels = {} var_names = {} structure.each do |gid, group| columns = group['columns'] || [] - columns.select!{ |a| (gid == GID_CUSTOM) || (a['column_id'].to_i > CORE_FIELD_IDS_FINISH_AT) } - custom_labels[gid.to_i] = Hash[ columns.map{ |c| [c['varname'], c['label']] } ] + custom_labels[gid.to_i] = Hash[ columns.map.select{ |a| gid.eql?(GID_CUSTOM) || !CORE_FIELD_IDS.include?(a['column_id'].to_i) }.map{ |c| [c['varname'], c['label']] } ] var_names[gid.to_i] = DirtyHashy[ columns.map{ |c| [c['column_id'].to_i, c['varname']] } ] end data.each do |item| item_data = Hash[ item['custom_data'].map{ |k,v| [k.to_i, v] } ] - member_contact = item_data[GID_MEMBER_CONTACT].nil? ? nil : Hash[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] - member_custom = item_data[GID_MEMBER_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_MEMBER_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_MEMBER_CONTACT][k.to_i], v] } ] - primary_contact = item_data[GID_PRIMARY_CONTACT].nil? ? nil : Hash[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] - primary_custom = item_data[GID_PRIMARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_PRIMARY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_PRIMARY_CONTACT][k.to_i], v] } ] - secondary_contact = item_data[GID_SECONDARY_CONTACT].nil? ? nil : Hash[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] - secondary_custom = item_data[GID_SECONDARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_SECONDARY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_SECONDARY_CONTACT][k.to_i], v] } ] - emergency_contact = item_data[GID_EMERGENCY_CONTACT].nil? ? nil : Hash[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] - emergency_custom = item_data[GID_EMERGENCY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_EMERGENCY_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_EMERGENCY_CONTACT][k.to_i], v] } ] - doctor_contact = item_data[GID_DOCTOR_CONTACT].nil? ? nil : Hash[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] - doctor_custom = item_data[GID_DOCTOR_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_DOCTOR_CONTACT].select{ |k,v| k.to_i >= CUSTOM_FIELD_IDS_START_AT }.map{ |k,v| [var_names[GID_DOCTOR_CONTACT][k.to_i], v] } ] - floating_data = item_data[GID_FLOATING].nil? ? {} : Hash[ item_data[GID_FLOATING].map{ |k,v| [k.to_i, v] }.select{ |k,v| k < CUSTOM_FIELD_IDS_START_AT } ] + member_contact = item_data[GID_MEMBER_CONTACT].nil? ? nil : Hash[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] + member_custom = item_data[GID_MEMBER_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_MEMBER_CONTACT].select{ |k,v| !(CORE_FIELD_IDS - [CID_TITLE]).include?(k.to_i) }.map{ |k,v| [var_names[GID_MEMBER_CONTACT][k.to_i], v] } ] + primary_contact = item_data[GID_PRIMARY_CONTACT].nil? ? nil : Hash[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] + primary_custom = item_data[GID_PRIMARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_PRIMARY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [var_names[GID_PRIMARY_CONTACT][k.to_i], v] } ] + secondary_contact = item_data[GID_SECONDARY_CONTACT].nil? ? nil : Hash[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] + secondary_custom = item_data[GID_SECONDARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_SECONDARY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [var_names[GID_SECONDARY_CONTACT][k.to_i], v] } ] + emergency_contact = item_data[GID_EMERGENCY_CONTACT].nil? ? nil : Hash[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] + emergency_custom = item_data[GID_EMERGENCY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_EMERGENCY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [var_names[GID_EMERGENCY_CONTACT][k.to_i], v] } ] + doctor_contact = item_data[GID_DOCTOR_CONTACT].nil? ? nil : Hash[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] + doctor_custom = item_data[GID_DOCTOR_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_DOCTOR_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [var_names[GID_DOCTOR_CONTACT][k.to_i], v] } ] + floating_data = item_data[GID_FLOATING].nil? ? {} : Hash[ item_data[GID_FLOATING].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ] custom_data = item_data[GID_CUSTOM].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_CUSTOM].map{ |k,v| [var_names[GID_CUSTOM][k.to_i], v] } ] result.push Osm::Member.new( :id => Osm::to_i_or_nil(item['member_id']), :section_id => Osm::to_i_or_nil(item['section_id']), + :title => custom_data['title'], :first_name => item['first_name'], :last_name => item['last_name'], :grouping_id => Osm::to_i_or_nil(item['patrol_id']), :grouping_label => item['patrol'], :grouping_leader => item['patrol_role_level'], @@ -196,10 +199,11 @@ :started_section => Osm::parse_date(item['joined']), :finished_section => Osm::parse_date(item['end_date']), :joined_movement => Osm::parse_date(item['started']), :gender => {'male'=>:male, 'female'=>:female, 'other'=>:other, 'unspecified'=>:unspecified}[(floating_data[CID_GENDER] || '').downcase], :contact => member_contact.nil? ? nil : MemberContact.new( + title: member_contact[CID_TITLE], first_name: item['first_name'], last_name: item['last_name'], address_1: member_contact[CID_ADDRESS_1], address_2: member_contact[CID_ADDRESS_2], address_3: member_contact[CID_ADDRESS_3], @@ -215,10 +219,11 @@ receive_email_2: member_contact[CID_RECIEVE_EMAIL_2], additional_information: member_custom, additional_information_labels: custom_labels[GID_MEMBER_CONTACT], ), :primary_contact => primary_contact.nil? ? nil : PrimaryContact.new( + title: primary_contact[CID_TITLE], first_name: primary_contact[CID_FIRST_NAME], last_name: primary_contact[CID_LAST_NAME], address_1: primary_contact[CID_ADDRESS_1], address_2: primary_contact[CID_ADDRESS_2], address_3: primary_contact[CID_ADDRESS_3], @@ -234,10 +239,11 @@ receive_email_2: primary_contact[CID_RECIEVE_EMAIL_2], additional_information: primary_custom, additional_information_labels: custom_labels[GID_PRIMARY_CONTACT], ), :secondary_contact => secondary_contact.nil? ? nil : SecondaryContact.new( + title: secondary_contact[CID_TITLE], first_name: secondary_contact[CID_FIRST_NAME], last_name: secondary_contact[CID_LAST_NAME], address_1: secondary_contact[CID_ADDRESS_1], address_2: secondary_contact[CID_ADDRESS_2], address_3: secondary_contact[CID_ADDRESS_3], @@ -253,10 +259,11 @@ receive_email_2: secondary_contact[CID_RECIEVE_EMAIL_2], additional_information: secondary_custom, additional_information_labels: custom_labels[GID_SECONDARY_CONTACT], ), :emergency_contact => emergency_contact.nil? ? nil : EmergencyContact.new( + title: emergency_contact[CID_TITLE], first_name: emergency_contact[CID_FIRST_NAME], last_name: emergency_contact[CID_LAST_NAME], address_1: emergency_contact[CID_ADDRESS_1], address_2: emergency_contact[CID_ADDRESS_2], address_3: emergency_contact[CID_ADDRESS_3], @@ -268,25 +275,26 @@ email_2: emergency_contact[CID_EMAIL_2], additional_information: emergency_custom, additional_information_labels: custom_labels[GID_EMERGENCY_CONTACT], ), :doctor => doctor_contact.nil? ? nil : DoctorContact.new( + title: doctor_contact[CID_TITLE], first_name: doctor_contact[CID_FIRST_NAME], last_name: doctor_contact[CID_LAST_NAME], surgery: doctor_contact[CID_SURGERY], address_1: doctor_contact[CID_ADDRESS_1], address_2: doctor_contact[CID_ADDRESS_2], address_3: doctor_contact[CID_ADDRESS_3], address_4: doctor_contact[CID_ADDRESS_4], postcode: doctor_contact[CID_POSTCODE], phone_1: doctor_contact[CID_PHONE_1], phone_2: doctor_contact[CID_PHONE_2], - additional_information:doctor_custom, + additional_information: doctor_custom, additional_information_labels: custom_labels[GID_DOCTOR_CONTACT], ), - additional_information: custom_data, - additional_information_labels: custom_labels[GID_CUSTOM], + additional_information: custom_data.select{ |k,v| ! ['title'].include?(k) }, + additional_information_labels: custom_labels[GID_CUSTOM].select{ |k,v| ! ['title'].include?(k) }, ) end cache_write(api, cache_key, result) return result @@ -360,10 +368,24 @@ 'sectionid' => section_id, }) updated = updated && data.is_a?(Hash) && data['ok'].eql?(true) end # each attr to update + # Do title attribute + if force || changed_attributes.include?('title') + data = api.perform_query("ext/members/contact/?action=update", { + 'associated_id' => self.id, + 'associated_type' => 'member', + 'value' => title, + 'column_id' => CID_TITLE, + 'group_id' => GID_CUSTOM, + 'context' => 'members', + }) + updated = updated && data.is_a?(Hash) && data['data'].is_a?(Hash) && data['data']['value'].eql?(title) + end + + # Do 'floating' attributes if force || changed_attributes.include?('gender') new_value = {male: 'Male', female: 'Female', other: 'Other'}[gender] || 'Unspecified' data = api.perform_query("ext/members/contact/?action=update", { 'associated_id' => self.id, @@ -420,14 +442,14 @@ def age_months return age[-2..-1].to_i end # Get the full name - # @param [String] seperator What to split the scout's first name and last name with + # @param [String] seperator What to split the member's title, first name and last name with # @return [String] this scout's full name seperated by the optional seperator def name(seperator=' ') - return "#{first_name}#{seperator.to_s}#{last_name}" + return [(title? ? "#{title}." : nil), first_name, last_name].select{ |i| !i.blank? }.join(seperator) end # Check if the member is in the leaders grouping # @return [Boolean] def leader? @@ -611,10 +633,12 @@ end end class Contact < Osm::Model + # @!attribute [rw] title + # @return [String] the contact's title (Mr, Ms, Dr etc.) # @!attribute [rw] first_name # @return [String] the contact's first name # @!attribute [rw] last_name # @return [String] the contact's last name # @!attribute [rw] address_1 @@ -634,10 +658,11 @@ # @!attribute [rw] additional_information # @return [DirtyHashy] the additional information (key is OSM's variable name, value is the data) # @!attribute [rw] additional_information_labels # @return [DirtyHashy] the labels for the additional information (key is OSM's variable name, value is the label) + attribute :title, :type => String attribute :first_name, :type => String attribute :last_name, :type => String attribute :address_1, :type => String attribute :address_2, :type => String attribute :address_3, :type => String @@ -647,24 +672,25 @@ attribute :phone_2, :type => String attribute :additional_information, :type => Object, :default => DirtyHashy.new attribute :additional_information_labels, :type => Object, :default => DirtyHashy.new if ActiveModel::VERSION::MAJOR < 4 - attr_accessible :first_name, :last_name, :address_1, :address_2, :address_3, :address_4, + attr_accessible :title, :first_name, :last_name, + :address_1, :address_2, :address_3, :address_4, :postcode, :phone_1, :phone_2, :additional_information, :additional_information_labels end # @!method initialize # Initialize a new Contact # @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key) # Get the full name - # @param [String] seperator What to split the scout's first name and last name with + # @param [String] seperator What to split the contact's title, first name and last name with # @return [String] this scout's full name seperated by the optional seperator def name(seperator=' ') - return "#{first_name}#{seperator.to_s}#{last_name}" + return [(title? ? "#{title}." : nil), first_name, last_name].select{ |i| !i.blank? }.join(seperator) end # Get an array of all phone numbers for the contact # @return [Array<String>] def all_phones @@ -680,10 +706,11 @@ 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 = { + 'title' => 'data[title]', 'first_name' => 'data[firstname]', 'last_name' => 'data[lastname]', 'surgery' => 'data[surgery]', 'address_1' => 'data[address1]', 'address_2' => 'data[address2]', @@ -826,42 +853,24 @@ if ActiveModel::VERSION::MAJOR < 4 attr_accessible :email_1, :email_2 end - # Get the full name - # @param [String] seperator What to split the scout's first name and last name with - # @return [String] this scout's full name seperated by the optional seperator - def name(seperator=' ') - return "#{first_name}#{seperator.to_s}#{last_name}" - end end # class EmergencyContact class DoctorContact < Osm::Member::Contact GROUP_ID = Osm::Member::GID_DOCTOR_CONTACT - # @!attribute [rw] first_name - # @return [String] the contact's first name - # @!attribute [rw] last_name - # @return [String] the contact's last name # @!attribute [rw] surgery # @return [String] the surgery name - attribute :first_name, :type => String - attribute :last_name, :type => String attribute :surgery, :type => String if ActiveModel::VERSION::MAJOR < 4 - attr_accessible :first_name, :last_name, :surgery + attr_accessible :surgery end - # Get the full name - # @param [String] seperator What to split the scout's first name and last name with - # @return [String] this scout's full name seperated by the optional seperator - def name(seperator=' ') - return "Dr. #{first_name}#{seperator.to_s}#{last_name}" - end end # class DoctorContact end # Class Member end # Module