Permalink
Browse files

PI Import: prevent duplicate agency assignments & allow multi agency …

…name assignments
  • Loading branch information...
1 parent b72cf55 commit 8dffdeaf98aeab1bc593f7ee17064ecd523f67bc @andrewcarpenter andrewcarpenter committed Apr 30, 2012
Showing with 13 additions and 8 deletions.
  1. +13 −8 lib/content/public_inspection_importer.rb
@@ -23,7 +23,7 @@ def self.perform
issue.save!
new_documents = []
- parser.document.pi_documents.each do |attr|
+ parser.document.grouped_pi_documents.each do |attr|
doc_importer = Content::PublicInspectionImporter.import(attr)
issue.public_inspection_documents << doc_importer.document unless issue.public_inspection_document_ids.include?(doc_importer.document.id)
new_documents << doc_importer.document if doc_importer.new_record?
@@ -96,13 +96,9 @@ def details=(val)
self.docket_numbers = docket_numbers.map{|number| DocketNumber.new(:number => number)}
end
- def agency=(val)
- if val.present?
- agency_name = AgencyName.find_or_create_by_name(val)
- @pi.agency_names = [agency_name]
- else
- @pi.agency_names = []
- end
+ def agency_names=(names)
+ @pi.agency_names = names.map{|name| AgencyName.find_or_create_by_name(name)}
+ @pi.agency_ids = @pi.agency_names.map(&:agency_id)
end
def url=(url)
@@ -164,6 +160,15 @@ def initialize(*args)
super
end
+ def grouped_pi_documents
+ @pi_documents.group_by{|attr| attr[:document_number]}.map do |document_number, attrs|
+ grouped = attrs.last.dup
+ grouped.delete(:agency)
+ grouped[:agency_names] = attrs.map{|attr| attr[:agency]}
+ grouped
+ end
+ end
+
def start_element(name, raw_attributes = [])
if @str.present?
handle_characters

0 comments on commit 8dffdea

Please sign in to comment.