{"action":"closed","issue":{"url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/101","repository_url":"https://api.github.com/repos/thoughtbot/ex_machina","labels_url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/101/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/101/comments","events_url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/101/events","html_url":"https://github.com/thoughtbot/ex_machina/issues/101","id":137602766,"number":101,"title":"Proposal to change factory definition","user":{"login":"paulcsmith","id":22394,"avatar_url":"https://avatars.githubusercontent.com/u/22394?v=3","gravatar_id":"","url":"https://api.github.com/users/paulcsmith","html_url":"https://github.com/paulcsmith","followers_url":"https://api.github.com/users/paulcsmith/followers","following_url":"https://api.github.com/users/paulcsmith/following{/other_user}","gists_url":"https://api.github.com/users/paulcsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/paulcsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulcsmith/subscriptions","organizations_url":"https://api.github.com/users/paulcsmith/orgs","repos_url":"https://api.github.com/users/paulcsmith/repos","events_url":"https://api.github.com/users/paulcsmith/events{/privacy}","received_events_url":"https://api.github.com/users/paulcsmith/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/thoughtbot/ex_machina/milestones/1","html_url":"https://github.com/thoughtbot/ex_machina/milestones/Release%201.0","labels_url":"https://api.github.com/repos/thoughtbot/ex_machina/milestones/1/labels","id":1494070,"number":1,"title":"Release 1.0","description":"","creator":{"login":"paulcsmith","id":22394,"avatar_url":"https://avatars.githubusercontent.com/u/22394?v=3","gravatar_id":"","url":"https://api.github.com/users/paulcsmith","html_url":"https://github.com/paulcsmith","followers_url":"https://api.github.com/users/paulcsmith/followers","following_url":"https://api.github.com/users/paulcsmith/following{/other_user}","gists_url":"https://api.github.com/users/paulcsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/paulcsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulcsmith/subscriptions","organizations_url":"https://api.github.com/users/paulcsmith/orgs","repos_url":"https://api.github.com/users/paulcsmith/repos","events_url":"https://api.github.com/users/paulcsmith/events{/privacy}","received_events_url":"https://api.github.com/users/paulcsmith/received_events","type":"User","site_admin":false},"open_issues":5,"closed_issues":6,"state":"open","created_at":"2016-01-08T22:09:19Z","updated_at":"2016-03-26T23:01:13Z","due_on":null,"closed_at":null},"comments":6,"created_at":"2016-03-01T15:22:05Z","updated_at":"2016-03-26T23:01:13Z","closed_at":"2016-03-26T23:01:13Z","body":"The factory definition syntax right now uses the same function name, but pattern matches on the first argument. This works ok, but it does cause a few problems\r\n\r\n* You can't easily use ctags/symbol view to jump to factories because they are all the same function\r\n* You can't add functions that are used by an factory in between other factories since they are all the same function and elixir will raise a warning.\r\n\r\nI propose that we change the factory definition syntax to {factory_name}_factory. This will also make it so we can add callbacks that are grouped near the factory in the future\r\n\r\n```elixir\r\ndefmodule Factory do\r\n  # This is nice because you can add functions for the user factory near it instead of at the bottom\r\n  # Also works better with symbol view so it doesn't show a bunch of `factory` functions.\r\n  def user_factory do\r\n    %User{\r\n      first_name: \"Paul\",\r\n      last_name: \"Smith\",\r\n    }\r\n  end\r\n  \r\n  # You can now define helper functions near the factory that it applies to\r\n  def make_admin(user) do\r\n    assing_to_admin_group(user)\r\n  end\r\n  \r\n  # In the future we could add callbacks like this. If we keep the current syntax you'd have to put these\r\n  # at the bottom of the file which makes it hard to see what a factory can do\r\n  def after_build_user(user, attrs) do\r\n    email = attrs.first_name <> attrs.last_name <> \"@foo.com\"\r\n    Map.put_new(user, :email, email)\r\n  end\r\n  \r\n  def comment_factory do\r\n    %Comment{\r\n      body: \"This is my comment\"\r\n    }\r\n  end\r\nend\r\n```\r\n\r\nYou would still call these with the same function `build(:user)`, etc."}}