diff options
| -rw-r--r-- | app/models/public_body.rb | 18 | ||||
| -rw-r--r-- | spec/fixtures/files/fake-authority-type-with-field-names.csv | 4 | ||||
| -rw-r--r-- | spec/models/public_body_spec.rb | 13 | 
3 files changed, 28 insertions, 7 deletions
| diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 0e32a5164..f1c6582ae 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -313,26 +313,30 @@ class PublicBody < ActiveRecord::Base      def self.import_csv(csv, tag, dry_run, editor)          errors = []          notes = [] - +                  begin              ActiveRecord::Base.transaction do -                existing_bodies = PublicBody.find_by_tag(tag) -                  bodies_by_name = {}                  set_of_existing = Set.new() -                for existing_body in existing_bodies +                for existing_body in PublicBody.find_by_tag(tag)                      bodies_by_name[existing_body.name] = existing_body                      set_of_existing.add(existing_body.name)                  end                  set_of_importing = Set.new() +                field_names = { 'name'=>1, 'email'=>2 }     # Default values in case no field list is given                  line = 0 -                  CSV::Reader.parse(csv) do |row| +                    # Parse the first line as a field list if it starts with '#' +                    if line==0 and row.to_s =~ /^#(.*)$/ +                        row.each_with_index {|field, i| field_names[field] = i} +                        next +                    end +                                          line = line + 1 -                    name = row[1] -                    email = row[2] +                    name = row[field_names['name']] +                    email = row[field_names['email']]                      next if name.nil?                      if email.nil?                          email = '' # unknown/bad contact is empty string diff --git a/spec/fixtures/files/fake-authority-type-with-field-names.csv b/spec/fixtures/files/fake-authority-type-with-field-names.csv new file mode 100644 index 000000000..5032f2783 --- /dev/null +++ b/spec/fixtures/files/fake-authority-type-with-field-names.csv @@ -0,0 +1,4 @@ +#id,email,name +,north_west_foi@localhost,North West Fake Authority +,scottish_foi@localhost,Scottish Fake Authority +,ni_foi@localhost,Fake Authority of Northern Ireland diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5bbf03d27..e532d0d50 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -242,5 +242,18 @@ describe PublicBody, " when loading CSV files" do      end  end +describe PublicBody, " when loading CSV files with field names and fields out of order" do +    it "should do a dry run successfully" do +        original_count = PublicBody.count +        csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv") +        errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run +        errors.should == [] +        notes.size.should == 3 +        notes.should == ["line 1: new authority 'North West Fake Authority' with email north_west_foi@localhost",  +            "line 2: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",  +            "line 3: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"] +        PublicBody.count.should == original_count +    end +end | 
