Transient attribute as count parameter

43 views
Skip to first unread message

Jacob Evelyn

unread,
Jul 14, 2013, 2:43:39 PM7/14/13
to fabrica...@googlegroups.com
Is it possible to use a transient attribute as a count parameter?

For instance, something like:

Fabricator(:foo) do
  transient n_iterations: 5
  bars { |attrs| bars(count: attrs[:n_iterations]) { Fabricate(:bar) } }
end

Paul Elliott

unread,
Jul 14, 2013, 4:38:46 PM7/14/13
to fabrica...@googlegroups.com
Hey Jacob,

It sure is. There is no syntax for setting defaults (yet) so you would have to do it like this:

```ruby
Fabricator(:foo) do
  transient :n_iterations
  n_iterations 5
  bars …
end
```

-- Paul

--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jacob Evelyn

unread,
Jul 14, 2013, 10:19:21 PM7/14/13
to fabrica...@googlegroups.com
Ah, thanks for the help! Just a head's up, the fabrication website seems to say that that default-setting syntax is valid.

Jacob Evelyn

unread,
Jul 15, 2013, 2:25:09 AM7/15/13
to fabrica...@googlegroups.com
Actually, I don't seem to be able to get this working. Is the syntax I had (other than the default transient value) correct? Even without the transient, this format:

bars { |attrs| bars(count: 5) { Fabricate(:bar) } }

is giving me a NoMethodError complaining about the bars method.

Paul Elliott

unread,
Jul 15, 2013, 7:10:26 AM7/15/13
to fabrica...@googlegroups.com
It should be:

```ruby
bars(count: 5) { Fabricate(:bar) }
```

-- Paul

Paul Elliott

unread,
Jul 15, 2013, 7:13:40 AM7/15/13
to fabrica...@googlegroups.com
Actually, you're right. Version 2.7.0 had the transient default feature. I think your problem may have been caused entirely by `bars`. Fix that and revert back to the default transient code and you should be all set.

-- Paul

Jacob Evelyn

unread,
Jul 15, 2013, 2:00:03 PM7/15/13
to fabrica...@googlegroups.com
Sorry, I'm not sure I understand. My understanding is that I need to be in a bars block to access the transient attribute, but from within that block I can't create a collection of bars with the bars method. If you could post a full code snippet of how to do this it would be hugely helpful.

Thanks for all the help.

Paul Elliott

unread,
Jul 15, 2013, 2:34:45 PM7/15/13
to fabrica...@googlegroups.com
Oh, sorry for the confusion. You were trying to redeclare the bars block inside the bars block. Instead you need to do your own iteration and return an array. Here is what the whole thing should look like:

```ruby
Fabricator(:foo) do
  transient n_iterations: 5
  bars { |attrs| (1..attrs[:n_iterations]).map { Fabricate(:bar) } }
end
```

-- Paul

Jacob Evelyn

unread,
Jul 15, 2013, 4:19:23 PM7/15/13
to fabrica...@googlegroups.com
Ah right, of course. Forgot that I can still use Ruby. :)

Thanks for the help!
Reply all
Reply to author
Forward
0 new messages