{"issue":{"state":"open","pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"user":{"avatar_url":"https://secure.gravatar.com/avatar/bb5ac46e606a18c60e08dee578294e1a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"bb5ac46e606a18c60e08dee578294e1a","url":"https://api.github.com/users/dadooda","login":"dadooda","id":126075},"milestone":null,"labels":[],"assignee":null,"updated_at":"2011-10-29T13:01:57Z","closed_at":null,"comments":2,"url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/226","body":"Hi\r\n\r\nIn some of my factories I want a particular piece of code be executed on every `.build` or `.create` invocation.\r\n\r\nBecause factories are attribute-oriented, the only choice I've got at the moment is to use the hack with `id` attribute, like this:\r\n\r\n    FactoryGirl.define do\r\n      factory :article do |f|\r\n        # Execute this code every time.\r\n        f.id do\r\n          # My code here.\r\n          ...\r\n\r\n          nil\r\n        end\r\n      end\r\n    end\r\n\r\nI'd prefer to do it in a more clear way, like, say:\r\n\r\n    before(:each) do\r\n      # My code here.\r\n    end\r\n\r\nThank you.\r\n","html_url":"https://github.com/thoughtbot/factory_girl/issues/226","number":226,"title":"Support `before` callback","id":2086452,"created_at":"2011-10-29T09:52:40Z"},"action":"created","comment":{"user":{"avatar_url":"https://secure.gravatar.com/avatar/bb5ac46e606a18c60e08dee578294e1a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"bb5ac46e606a18c60e08dee578294e1a","url":"https://api.github.com/users/dadooda","login":"dadooda","id":126075},"updated_at":"2011-10-29T13:01:57Z","url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/comments/2564795","body":"I'm trying to use factory as both factory and forgery, i.e. generator of data, which looks and feels real.\r\n\r\nIn the example below I load a pre-generated set of attributes and then fetch a sample from there.\r\n\r\n    FactoryGirl.define do\r\n      factory :article do |f|\r\n        samples = Marshal.load(Zlib::GzipReader.open(File.join(File.dirname(__FILE__), \"data/article,samples.marshal.gz\")).read)\r\n        sample = nil\r\n\r\n        f.id {nil.tap do\r\n          sample = samples.sample\r\n        end}\r\n\r\n        f.source_id {Source.find_by_name(sample[:source]).id rescue nil}\r\n\r\n        f.author {sample[:author].to_s}\r\n        f.idstring {sample[:idstring].to_s}\r\n        f.raw_content {sample[:raw_content].to_s}\r\n        f.raw_title {sample[:raw_title].to_s}\r\n        f.url {sample[:url].to_s}\r\n\r\n        f.published_at {sample[:published_at]}\r\n\r\n        f.is_forged true\r\n      end\r\n    end\r\n","id":2564795,"created_at":"2011-10-29T13:01:57Z"}}