FactoryGirl.define do
  factory :file_attachment do
    ignore do
      file { File.open(Rails.root.join('test', 'fixtures', 'greenpaper.pdf')) }
    end

    sequence(:title) { |index| "file-attachment-title-#{index}" }
    sequence(:ordering)
    after(:build) do |attachment, evaluator|
      attachment.attachment_data ||= build(:attachment_data, file: evaluator.file)
    end
  end

  factory :csv_attachment, parent: :file_attachment do
    ignore do
      file { File.open(Rails.root.join('test', 'fixtures', 'sample.csv')) }
    end
  end

  factory :html_attachment do
    sequence(:title) { |index| "html-attachment-title-#{index}" }
    sequence(:ordering)
    body 'Attachment body'
  end
end

FactoryGirl.define do
  sequence(:attachment_ordering)

  factory :file_attachment do
    ignore do
      file { File.open(Rails.root.join('test', 'fixtures', 'greenpaper.pdf')) }
    end

    sequence(:title) { |index| "file-attachment-title-#{index}" }
    ordering { generate(:attachment_ordering) }
    after(:build) do |attachment, evaluator|
      attachment.attachment_data ||= build(:attachment_data, file: evaluator.file)
    end
  end

  factory :csv_attachment, parent: :file_attachment do
    ignore do
      file { File.open(Rails.root.join('test', 'fixtures', 'sample.csv')) }
    end
  end

  factory :html_attachment do
    sequence(:title) { |index| "html-attachment-title-#{index}" }
    ordering { generate(:attachment_ordering) }
    body 'Attachment body'
  end
end

Use a global sequence for attachment ordering
Use a global sequence for attachment ordering

This should fix an intermittent test failure when HTML and File
attachments are built in the same test without being given an explicit
ordering.
