Class: ParsedRow

Inherits:
Object
  • Object
show all
Defined in:
app/models/parsed_row.rb

Constant Summary collapse

EXCEPTIONS =

Fields which require special parsing such as dollar amounts

[:amount, :nongift_amount, :deductible_amount]
SHARED_FIELDS =
{
  :first            => [ "First name", "First" ],
  :middle           => [ "Middle name", "Middle" ],
  :last             => [ "Last name", "Last" ],
  :email            => [ "Email", "Email address" ],
  :suffix           => [ "Suffix" ],
}
ADDRESS_FIELDS =
{
  :address1         => [ "Address 1", "Address1" ],
  :address2         => [ "Address 2", "Address2" ],
  :city             => [ "City" ],
  :state            => [ "State" ],
  :zip              => [ "Zip", "Zip code" ],
  :country          => [ "Country" ]
}
PEOPLE_FIELDS =
SHARED_FIELDS.merge( {
  :salutation       => [ "Salutation" ],     
  :title            => [ "Title" ],
  :company          => [ "Company name", "Company" ],

  :phone1_type      => [ "Phone1 type", "Phone 1 type" ],
  :phone1_number    => [ "Phone1 number", "Phone 1", "Phone 1 number", "Phone1" ],
  :phone2_type      => [ "Phone2 type", "Phone 2 type" ],
  :phone2_number    => [ "Phone2 number", "Phone 2", "Phone 2 number", "Phone2" ],
  :phone3_type      => [ "Phone3 type", "Phone 3 type" ],
  :phone3_number    => [ "Phone3 number", "Phone 3", "Phone 3 number", "Phone3" ],
  :website          => [ "Website" ],
  :twitter_username => [ "Twitter handle", "Twitter", "Twitter username" ],
  :facebook_page    => [ "Facebook url", "Facebook", "Facebook address", "Facebook page" ],
  :linkedin_page    => [ "Linked in url", "LinkedIn url", "LinkedIn", "LinkedIn address", "LinkedIn page" ],
  :tags             => [ "Tags" ],
  :do_not_email     => [ "Do Not Email" ],
  :do_not_call      => [ "Do Not Call" ],
  :subtype          => [ "Type", "Subtype" ],
  :birth_month      => [ "Birth Month" ],
  :birth_day        => [ "Birth Day" ],
  :birth_year       => [ "Birth Year" ]
})
MEMBERSHIP_FIELDS =
SHARED_FIELDS.merge( {
  :membership_name       => [ "Membership Name", "Membership", "Name" ],
  :membership_plan       => [ "Membership Plan", "Plan"],           #PAYG, ALL IN ONE, OTHER
  :start_date            => [ "Start Date" ],
  :end_date              => [ "End Date" ],
  :amount                => [ "Amount" ],
  :payment_method        => [ "Payment Method" ],
  :order_date            => [ "Order Date", "Date" ],

  :number_of_memberships => [ "Quantity" ]
})
EVENT_FIELDS =
SHARED_FIELDS.merge( {
  :event_name       => [ "Event", "Event Name" ],
  :venue_name       => [ "Venue", "Venue Name" ],
  :show_date        => [ "Show Date", "Show" ],
  :amount           => [ "Amount", "Dollar Amount" ],
  :payment_method   => [ "Payment Method" ],
  :order_date       => [ "Order Date", "Date" ]
})
DONATION_FIELDS =
SHARED_FIELDS.merge( {
  :payment_method   => [ "Payment Method" ],
  :donation_date    => [ "Date", "Order Date" ],
  :donation_type    => [ "Donation Type", "Type" ],
  :amount           => [ "Amount" ],
  :deductible_amount=> [ "Deductible Amount" ],
  
  #Internally it is called nongift_amount but the rest of the world says non-deductible
  :nongift_amount  => [ "Non-Deductible Amount", "Non Deductible Amount" ]
  
  #TODO: Total contribution sanity check
})
FIELDS =
PEOPLE_FIELDS.merge(ADDRESS_FIELDS).merge(EVENT_FIELDS).merge(DONATION_FIELDS).merge(MEMBERSHIP_FIELDS)
ENUMERATIONS =

Enumerated columns default to the last value if the data value is not valid.

With the way the current code is using instance_variable_get, columns that use an enumeration cannot accept multiple column names. We can only have one column name map to subtype.

{
  :subtype => [ "Business", "Foundation", "Government", "Nonprofit", "Other", "Individual" ]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, row) ⇒ ParsedRow

Returns a new instance of ParsedRow.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/parsed_row.rb', line 98

def initialize(headers, row)
  @values_hash = HashWithIndifferentAccess.new    
  @headers = headers
  @header_hash = {}
  @headers.each_with_index { |header, index| @header_hash[header.to_s.downcase.strip] = index}
  @row = row

  downcased_fields = FIELDS
  downcased_fields.each do |field, columns|
    columns.map! {|column| column.downcase}
  end

  downcased_fields.each do |field, columns|
    columns.each do |column|
      load_value field, column
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



131
132
133
134
135
136
137
# File 'app/models/parsed_row.rb', line 131

def method_missing(method_name, *args)
  if @values_hash.has_key? method_name
    @values_hash[method_name]
  else
    super
  end
end

Instance Attribute Details

#rowObject

Returns the value of attribute row.



3
4
5
# File 'app/models/parsed_row.rb', line 3

def row
  @row
end

#values_hashObject

Returns the value of attribute values_hash.



4
5
6
# File 'app/models/parsed_row.rb', line 4

def values_hash
  @values_hash
end

Class Method Details

.parse(headers, row) ⇒ Object



94
95
96
# File 'app/models/parsed_row.rb', line 94

def self.parse(headers, row)
  ParsedRow.new(headers, row)
end

Instance Method Details

#address_attributesObject



191
192
193
# File 'app/models/parsed_row.rb', line 191

def address_attributes
  Hash[ADDRESS_FIELDS.keys.map{|k| [k, self.send(k)]}]
end

#amountObject



163
164
165
# File 'app/models/parsed_row.rb', line 163

def amount
  ((@amount.to_f || 0) * 100).to_i
end

#birth_monthObject



179
180
181
# File 'app/models/parsed_row.rb', line 179

def birth_month
  month_to_number(@birth_month) if @birth_month.present?
end

#check_enumeration(field, value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/parsed_row.rb', line 143

def check_enumeration(field, value)
  if enum = ENUMERATIONS[field] 
    if index = enum.map(&:downcase).index(value.to_s.downcase)
      enum[index]
    else
      enum.last
    end
  else
    value
  end
end

#deductible_amountObject



171
172
173
# File 'app/models/parsed_row.rb', line 171

def deductible_amount
  ((@deductible_amount.to_f || 0) * 100).to_i
end

#importing_event?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'app/models/parsed_row.rb', line 183

def importing_event?
  !self.event_name.blank?
end

#load_value(field, column) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/parsed_row.rb', line 117

def load_value(field, column)
  index = @header_hash[column]
  value = @row[index] if index
  
  exist = self.instance_variable_get("@#{field}")

  if exist.blank?
    value = check_enumeration(field, value)

    self.instance_variable_set("@#{field}", value)
    @values_hash[field.to_s] = value
  end
end

#month_to_number(input) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/models/parsed_row.rb', line 195

def month_to_number(input)
  case input
  when "January"   then "1"
  when "February"  then "2"
  when "March"     then "3"
  when "April"     then "4"
  when "May"       then "5"
  when "June"      then "6"
  when "July"      then "7"
  when "August"    then "8"
  when "September" then "9"
  when "October"   then "10"
  when "November"  then "11"
  when "December"  then "12"
  when "Jan" then "1"
  when "Feb" then "2"
  when "Mar" then "3"
  when "Apr" then "4"
  when "May" then "5"
  when "Jun" then "6"
  when "Jul" then "7"
  when "Aug" then "8"
  when "Sep" then "9"
  when "Oct" then "10"
  when "Nov" then "11"
  when "Dec" then "12"
  end
end

#nongift_amountObject



155
156
157
# File 'app/models/parsed_row.rb', line 155

def nongift_amount
  ((@nongift_amount.to_f || 0) * 100).to_i
end

#person_attributesObject

These are also used in person.update_from_import



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/parsed_row.rb', line 227

def person_attributes
    {
      :email           => self.email,
      :salutation      => self.salutation,
      :title           => self.title,
      :first_name      => self.first,
      :middle_name     => self.middle,
      :last_name       => self.last,
      :suffix          => self.suffix,
      :company_name    => self.company,
      :website         => self.website,
      :twitter_handle  => self.twitter_username,
      :facebook_url    => self.facebook_page,
      :linked_in_url   => self.linkedin_page,
      :subtype         => self.subtype,
      :do_not_email    => self.do_not_email,
      :do_not_call     => self.do_not_call,
      :birth_month     => self.birth_month,
      :birth_day       => self.birth_day,
      :birth_year      => self.birth_year
    }
end

#preview(field_name) ⇒ Object



187
188
189
# File 'app/models/parsed_row.rb', line 187

def preview(field_name)
  field_name.to_s.ends_with?("amount") ? self.send("unparsed_#{field_name}") : self.send(field_name)
end

#tags_listObject



139
140
141
# File 'app/models/parsed_row.rb', line 139

def tags_list
  @tags.to_s.strip.gsub(/\s+/, "-").split(/[,|]+/)
end

#unparsed_amountObject



167
168
169
# File 'app/models/parsed_row.rb', line 167

def unparsed_amount
  @amount
end

#unparsed_deductible_amountObject



175
176
177
# File 'app/models/parsed_row.rb', line 175

def unparsed_deductible_amount
  @deductible_amount
end

#unparsed_nongift_amountObject



159
160
161
# File 'app/models/parsed_row.rb', line 159

def unparsed_nongift_amount
  @nongift_amount
end