Created
October 15, 2015 02:30
-
-
Save paulcsmith/c957d45bb8e148f7a23d 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.AnnouncementTraits do | |
def with_subscriber(announcement, user) do | |
create(:subscription, announcement: announcement, user: user) | |
announcement | |
end | |
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 | |
end |
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 __using__(_opts) do | |
quote do | |
import MyApp.Factory | |
import MyApp.UserTraits | |
import MyApp.AnnouncementTraits | |
# etc. | |
end | |
end | |
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 | |
end |
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.AnnouncementTraits do | |
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 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment