Skip to content

Commit

Permalink
messy first pass at detecting possible duplicates. Issue #179
Browse files Browse the repository at this point in the history
  • Loading branch information
shua123 committed Aug 29, 2016
1 parent ea1ca48 commit 7a7ff8b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
10 changes: 2 additions & 8 deletions app/controllers/gift_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ def index
end
@gift_cards = GiftCard.paginate(page: params[:page]).includes(:person).where(query)
#@gift_cards = GiftCard.paginate(page: params[:page]).includes(:person).all
@recent_signups = Person.paginate(page: params[:page]).no_signup_card.where('signup_at > :startdate', { startdate: 3.months.ago }).order('signup_at DESC')
@recent_signups = Person.no_signup_card.paginate(page: params[:page]).where('signup_at > :startdate', { startdate: 3.months.ago }).order('signup_at DESC')
@new_gift_cards = []
@email_duplicates = []
@phone_duplicates = []
@recent_signups.each do |signup|
@email_duplicates << Person.where(email_address: signup.email_address).where.not(id: signup.id).order('signup_at DESC')
@phone_duplicates << Person.where(phone_number: signup.phone_number).where.not(id: signup.id).order('signup_at DESC')

end
# end
@recent_signups.length.times do
@new_gift_cards << GiftCard.new
end
Expand Down
58 changes: 56 additions & 2 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ class Person < ActiveRecord::Base

validates :phone_number, presence: true, length: { in: 9..15 },
unless: proc { |person| person.email_address.present? }
validates :phone_number, allow_blank: true, uniqueness: true
# validates :phone_number, allow_blank: true, uniqueness: true

validates :email_address, presence: true,
unless: proc { |person| person.phone_number.present? }
validates :email_address, email: true, allow_blank: true, uniqueness: true
# validates :email_address, email: true, allow_blank: true, uniqueness: true

scope :no_signup_card, -> { where('id NOT IN (SELECT DISTINCT(person_id) FROM gift_cards where gift_cards.reason = 1)') }
scope :signup_card_needed, -> { joins(:gift_cards).where('gift_cards.reason !=1') }
Expand Down Expand Up @@ -356,5 +356,59 @@ def update_neighborhood
end
end

# Compare to other records in the database to find possible duplicates.
def possible_duplicates
@duplicates = {}
last_name_duplicates = Person.where(last_name: last_name).where.not(id: id)
last_name_duplicates.each do |duplicate|
duplicate_hash = {}
duplicate_hash['person'] = duplicate
duplicate_hash['match_count'] = 1
duplicate_hash['last_name_match'] = true
duplicate_hash['matches_on'] = ["Last Name"]
@duplicates[duplicate.id] = duplicate_hash
end
email_address_duplicates = Person.where(email_address: email_address).where.not(id: id)
email_address_duplicates.each do |duplicate|
if @duplicates.has_key? duplicate.id
@duplicates[duplicate.id]['match_count'] += 1
@duplicates[duplicate.id]['matches_on'].push("Email Address")
else
@duplicates[duplicate.id] = {}
@duplicates[duplicate.id]['person'] = duplicate
@duplicates[duplicate.id]['match_count'] = 1
@duplicates[duplicate.id]['matches_on'] = ["Email Address"]
end
@duplicates[duplicate.id]['email_address_match'] = true
end
phone_number_duplicates = Person.where(phone_number: phone_number).where.not(id: id)
phone_number_duplicates.each do |duplicate|
if @duplicates.has_key? duplicate.id
@duplicates[duplicate.id]['match_count'] += 1
@duplicates[duplicate.id]['matches_on'].push("Phone Number")
else
@duplicates[duplicate.id] = {}
@duplicates[duplicate.id]['person'] = duplicate
@duplicates[duplicate.id]['match_count'] = 1
@duplicates[duplicate.id]['matches_on'] = ["Phone Number"]
end
@duplicates[duplicate.id]['phone_number_match'] = true
end
address_1_duplicates = Person.where(address_1: address_1).where.not(id: id)
address_1_duplicates.each do |duplicate|
if @duplicates.has_key? duplicate.id
@duplicates[duplicate.id]['match_count'] += 1
@duplicates[duplicate.id]['matches_on'].push("Address_1")
else
@duplicates[duplicate.id] = {}
@duplicates[duplicate.id]['person'] = duplicate
@duplicates[duplicate.id]['match_count'] = 1
@duplicates[duplicate.id]['matches_on'] = ["Address_1"]
end
@duplicates[duplicate.id]['address_1_match'] = true
end
return @duplicates
end

end
# rubocop:enable ClassLength
15 changes: 6 additions & 9 deletions app/views/gift_cards/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ul>
</div>
<% end %>
<td>
<td rowspan=2>
<%= form.text_field :person_id, :value => @recent_signups[index].id, :readonly => true, :class => "span1" %>
</td>
<td>
Expand Down Expand Up @@ -64,15 +64,12 @@
<% end %>
</tr>
<tr>
<tr><th>Email Duplicates:</th></tr>
<% @email_duplicates.each do |duplicate| %>
<% if duplicate.present? %>
<tr>
<td>ID: <%= duplicate[index].id %></td>
<td><%= duplicate[index].full_name %></td>
</tr>
<td colspan=6>
<% @recent_signups[index].possible_duplicates.each do |key, val| %>
Possible Duplicate ID #<%= link_to "#{key} #{val['person'].full_name}", person_path(val['person']) %> Matching on <%= val['match_count'] %> variables: <%= val['matches_on'].to_sentence %>
<br />
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down

0 comments on commit 7a7ff8b

Please sign in to comment.