Last active
October 15, 2015 02:27
-
-
Save paulcsmith/bc6806b5420ccbd6a92c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Factory do | |
use ExMachina.Ecto, repo: MyApp.Repo | |
def factory(:interest, attrs) do | |
%Interest{ | |
name: sequence(:interest_name, &"interest-#{&1}") | |
} | |
end | |
def factory(:user, attrs) do | |
%User{ | |
username: "myusername", | |
name: "Gumbo", | |
email: sequence(:email, &"test#{&1}@thoughtbot.com"), | |
daily_digest: true, | |
auto_subscribe: false | |
} | |
end | |
def factory(:announcement, attrs) do | |
%Announcement{ | |
title: sequence(:email, &"Post Title#{&1}"), | |
body: "Post Body", | |
user: assoc(:user) | |
} | |
end | |
def factory(:announcement_interest, attrs) do | |
%AnnouncementInterest{ | |
announcement_id: assoc(:announcement), | |
interest: assoc(:interest) | |
} | |
end | |
def factory(:comment, attrs) do | |
%Comment{ | |
body: "Post Body", | |
user: assoc(:user), | |
announcement: assoc(:announcement) | |
} | |
end | |
def factory(:subscription, attrs) do | |
%Subscription{ | |
user: assoc(:user), | |
announcement: assoc(:announcement) | |
} | |
end | |
def factory(:user_interest, attrs) do | |
%UserInterest{ | |
user: assoc(:user), | |
interest: assoc(:interest) | |
} | |
end | |
# All the functions that interact with the various factories are not near the factory definitions. | |
# As factory definitions grow it will be harder and harder to find functions that work with a particular factory | |
def tag_with_interest(announcement, interest) do | |
create(:announcement_interest, announcement: announcement, interest: interest) | |
announcement | |
end | |
def with_comment(announcement) do | |
create(:comment, announcement: announcement) | |
announcement | |
end | |
def with_interest(user, interest \\ create(:interest)) do | |
create(:user_interest, user: user, interest: interest) | |
user | |
end | |
def with_subscription(user, announcement \\ create(:announcement)) do | |
create(:subscription, user: user, announcement: announcement) | |
user | |
end | |
def with_subscriber(announcement, user) do | |
create(:subscription, announcement: announcement, user: user) | |
announcement | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment