lib/osm/member.rb in osm-1.2.18.dev.12 vs lib/osm/member.rb in osm-1.2.18.dev.13
- old
+ new
@@ -9,11 +9,10 @@
GID_CUSTOM = 5
GID_MEMBER_CONTACT = 6
GID_FLOATING = 7
# Constants for column id
- CID_TITLE = 1
CID_FIRST_NAME = 2
CID_LAST_NAME = 3
CID_ADDRESS_1 = 7
CID_ADDRESS_2 = 8
CID_ADDRESS_3 = 9
@@ -34,12 +33,10 @@
# @!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
@@ -77,11 +74,10 @@
# @!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
@@ -99,11 +95,11 @@
attribute :secondary_contact, :type => Object
attribute :emergency_contact, :type => Object
attribute :doctor, :type => Object
if ActiveModel::VERSION::MAJOR < 4
- attr_accessible :id, :section_id, :title, :first_name, :last_name,
+ attr_accessible :id, :section_id, :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
@@ -160,36 +156,34 @@
data = api_response['data'].is_a?(Hash) ? api_response['data'].values : []
structure = (api_response['meta'] || {})['structure'] || []
structure = Hash[ structure.map{ |i| [i['group_id'].to_i, i ] } ] # Make a hash of identifier to group data hash
custom_labels = {}
- var_names = {}
+ key_key = 'column_id' # the key in the data from OSM to use as the key in additional_information and labels hashes
structure.each do |gid, group|
columns = group['columns'] || []
- 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']] } ]
+ 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[key_key], c['label']] } ]
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| 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] } ]
+ member_custom = item_data[GID_MEMBER_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_MEMBER_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [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] } ]
+ 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| [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] } ]
+ 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| [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] } ]
+ 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| [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] } ]
+ 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| [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] } ]
+ custom_data = item_data[GID_CUSTOM].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_CUSTOM].map{ |k,v| [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'],
@@ -199,11 +193,10 @@
: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,15 +208,14 @@
email_2: member_contact[CID_EMAIL_2],
receive_phone_1: member_contact[CID_RECIEVE_PHONE_1],
receive_phone_2: member_contact[CID_RECIEVE_PHONE_2],
receive_email_1: member_contact[CID_RECIEVE_EMAIL_1],
receive_email_2: member_contact[CID_RECIEVE_EMAIL_2],
- additional_information: DirtyHashy[ member_custom.select{ |k,v| !['title'].include?(k) } ],
+ 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],
@@ -239,11 +231,10 @@
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],
@@ -259,11 +250,10 @@
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],
@@ -275,11 +265,10 @@
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],
@@ -289,12 +278,12 @@
phone_1: doctor_contact[CID_PHONE_1],
phone_2: doctor_contact[CID_PHONE_2],
additional_information: doctor_custom,
additional_information_labels: custom_labels[GID_DOCTOR_CONTACT],
),
- additional_information: DirtyHashy[ custom_data.select{ |k,v| !['title'].include?(k) } ],
- additional_information_labels: custom_labels[GID_CUSTOM].select{ |k,v| !['title'].include?(k) },
+ additional_information: custom_data,
+ additional_information_labels: custom_labels[GID_CUSTOM],
)
end
cache_write(api, cache_key, result)
return result
@@ -368,23 +357,10 @@
'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/customdata/?action=updateColumn§ion_id=#{section_id}", {
- '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/customdata/?action=updateColumn§ion_id=#{section_id}", {
'associated_id' => self.id,
@@ -441,14 +417,14 @@
def age_months
return age[-2..-1].to_i
end
# Get the full name
- # @param [String] seperator What to split the member's title, first name and last name with
+ # @param [String] seperator What to split the member's first name and last name with
# @return [String] this scout's full name seperated by the optional seperator
def name(seperator=' ')
- return [(title? ? "#{title}." : nil), first_name, last_name].select{ |i| !i.blank? }.join(seperator)
+ return [first_name, last_name].select{ |i| !i.blank? }.join(seperator)
end
# Check if the member is in the leaders grouping
# @return [Boolean]
def leader?
@@ -632,12 +608,10 @@
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
@@ -657,11 +631,10 @@
# @!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
@@ -671,25 +644,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 :title, :first_name, :last_name,
+ attr_accessible :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 contact's title, first name and last name with
+ # @param [String] seperator What to split the contact's first name and last name with
# @return [String] this scout's full name seperated by the optional seperator
def name(seperator=' ')
- return [(title? ? "#{title}." : nil), first_name, last_name].select{ |i| !i.blank? }.join(seperator)
+ return [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
@@ -705,10 +678,9 @@
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]',