Skip to content

Instantly share code, notes, and snippets.

@jathayde
Created October 12, 2017 19:51
Show Gist options
  • Save jathayde/7b7e8e6d7e18d48826fa61a86da7f5ea to your computer and use it in GitHub Desktop.
Save jathayde/7b7e8e6d7e18d48826fa61a86da7f5ea to your computer and use it in GitHub Desktop.
Take 2
FactoryGirl.define do
factory :issue do
association :issueable, factory: :lodge
issue_number { Faker::Number.number(2) }
quantity_issued { Faker::Number.number(2) }
description 'Test description for test issue'
oa_bb_category_id '1'
category_id '1'
issue_type_id '1'
border_color_id '1'
border_type_id '1'
background_color_id '1'
lettering_color_id '1'
fdl_bsa_color_id '1'
variety { Faker::Lorem.character }
transient do
combined_value nil
end
before(:each) do
@lodge_issue = FactoryGirl.create(:lodge_issue)
@issue = @lodge_issue.issue
end
# Assign an issue if we can
after(:create) do |issue, factory|
# Assign an issue based on a provided :combined value
if factory.combined_value
string = factory.combined_value
type_abbrev,
second_abbrev,
issue_number,
variety = string.match(/([A-Z])([A-Z]?)([\d]*)([a-z])?/).captures
if second_abbrev
issue.issue_type = IssueType.find_by_abbreviation(second_abbrev)
else
issue.issue_type = IssueType.find_by_abbreviation(type_abbrev)
end
issue.issue_number = issue_number
issue.variety = variety.to_s
issue.combined = factory.combined_value
elsif issue.issue_type.nil?
# If an IssueType was not specified, assign a random one
issue_type = IssueType.order_by_rand.first
issue.issue_type = issue_type
issue.variety = Faker::Lorem.character
end
end
end
factory :lodge_issue, class: "Issue" do
association :issueable, factory: :lodge
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment