{"action":"created","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":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-03-01T15:22:05Z","updated_at":"2016-03-01T16:11:30Z","closed_at":null,"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."},"comment":{"url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/comments/190788432","html_url":"https://github.com/thoughtbot/ex_machina/issues/101#issuecomment-190788432","issue_url":"https://api.github.com/repos/thoughtbot/ex_machina/issues/101","id":190788432,"user":{"login":"derekprior","id":152152,"avatar_url":"https://avatars.githubusercontent.com/u/152152?v=3","gravatar_id":"","url":"https://api.github.com/users/derekprior","html_url":"https://github.com/derekprior","followers_url":"https://api.github.com/users/derekprior/followers","following_url":"https://api.github.com/users/derekprior/following{/other_user}","gists_url":"https://api.github.com/users/derekprior/gists{/gist_id}","starred_url":"https://api.github.com/users/derekprior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/derekprior/subscriptions","organizations_url":"https://api.github.com/users/derekprior/orgs","repos_url":"https://api.github.com/users/derekprior/repos","events_url":"https://api.github.com/users/derekprior/events{/privacy}","received_events_url":"https://api.github.com/users/derekprior/received_events","type":"User","site_admin":false},"created_at":"2016-03-01T16:11:30Z","updated_at":"2016-03-01T16:11:30Z","body":"This makes `create(:user)` just a tad more magical to me. Not enough to turn me off completely, but worth noting, perhaps.\r\n\r\nI agree that the helper functions/traits/callbacks at the bottom of the factory file are really difficult to keep track of. I often have no idea which helpers/traits are intended to work with which factories.\r\n\r\nSometimes, though, the helpers are generic enough that they can work for multiple factories. In which case, the inline ordering also breaks down.\r\n\r\nI guess I'm saying I'm not really sure right now if I'd like this or not.\r\n"}}