magic_lamp 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +163 -33
  3. data/VERSION +1 -0
  4. data/app/assets/javascripts/magic_lamp/genie.js +1 -1
  5. data/lib/magic_lamp.rb +50 -39
  6. data/lib/magic_lamp/configuration.rb +2 -1
  7. data/lib/magic_lamp/constants.rb +24 -0
  8. data/lib/magic_lamp/defaults_manager.rb +62 -0
  9. data/lib/magic_lamp/fixture_creator.rb +13 -3
  10. data/lib/magic_lamp/version.rb +1 -1
  11. data/spec/dummy/app/views/orders/needs_extending.html.erb +2 -0
  12. data/spec/dummy/log/development.log +51060 -0
  13. data/spec/dummy/log/test.log +244175 -0
  14. data/spec/dummy/spec/magical/a_lamp_file_with_a_different_name_lamp.rb +57 -0
  15. data/spec/dummy/tmp/cache/assets/development/sprockets/04a1e26a9e3fde1f448cb2a9d382fbbc +0 -0
  16. data/spec/dummy/tmp/cache/assets/development/sprockets/09bc89d8ac4ccacfcf2f4db466e7735f +0 -0
  17. data/spec/dummy/tmp/cache/assets/development/sprockets/0b3144442052dbb8af7f57ca27cd7a15 +0 -0
  18. data/spec/dummy/tmp/cache/assets/development/sprockets/18650d8ff9b0a83b32e55b58917ec791 +0 -0
  19. data/spec/dummy/tmp/cache/assets/development/sprockets/292405ba7ff52dbb279572aee49ba174 +0 -0
  20. data/spec/dummy/tmp/cache/assets/development/sprockets/2ccef21649a61deac2c84f622753b77b +0 -0
  21. data/spec/dummy/tmp/cache/assets/development/sprockets/32b0c6aea545968d6da4939bcbab9b5e +0 -0
  22. data/spec/dummy/tmp/cache/assets/development/sprockets/3823434d99b44359ee75dffba2817055 +0 -0
  23. data/spec/dummy/tmp/cache/assets/development/sprockets/3a2dd1069a3d0cd57769e33920561e09 +0 -0
  24. data/spec/dummy/tmp/cache/assets/development/sprockets/3c8cdd0e5d5eee39df490048c656a2c5 +0 -0
  25. data/spec/dummy/tmp/cache/assets/development/sprockets/40f78fd7fa893a3ba0bb52949d4d6e21 +0 -0
  26. data/spec/dummy/tmp/cache/assets/development/sprockets/51333e0ed6511483c511470803d3803a +0 -0
  27. data/spec/dummy/tmp/cache/assets/development/sprockets/5b03eeac2e4ecbaaf227c5aa35808fee +0 -0
  28. data/spec/dummy/tmp/cache/assets/development/sprockets/5bbf09297502a156801e9c65f4bd3db7 +0 -0
  29. data/spec/dummy/tmp/cache/assets/development/sprockets/6f968441247482ba7e1ac7ce9f34dfdb +0 -0
  30. data/spec/dummy/tmp/cache/assets/development/sprockets/759e97d6d411bc4cef7055a778e1bef3 +0 -0
  31. data/spec/dummy/tmp/cache/assets/development/sprockets/7f5b2f45d9f12db1fb26a605759b360a +0 -0
  32. data/spec/dummy/tmp/cache/assets/development/sprockets/7f7dcf7837dbb7f150b7364ad7920712 +0 -0
  33. data/spec/dummy/tmp/cache/assets/development/sprockets/80a68d71946643e61d91cc216f7aa32f +0 -0
  34. data/spec/dummy/tmp/cache/assets/development/sprockets/86023efec2465a88617e5a47a38f1f55 +0 -0
  35. data/spec/dummy/tmp/cache/assets/development/sprockets/87c6d9a5c985ccb321255862fd8c2f48 +0 -0
  36. data/spec/dummy/tmp/cache/assets/development/sprockets/88fe64427f1fff8e41ca8ad0170da88a +0 -0
  37. data/spec/dummy/tmp/cache/assets/development/sprockets/8a0862dca044d19cadb655c53acb20ab +0 -0
  38. data/spec/dummy/tmp/cache/assets/development/sprockets/8ed4b4f20fb65446181984e6df7d7c9b +0 -0
  39. data/spec/dummy/tmp/cache/assets/development/sprockets/8f4a370f455559b7a209ecd758a3bb3d +0 -0
  40. data/spec/dummy/tmp/cache/assets/development/sprockets/96210dc070e479fcee862294afe5efc1 +0 -0
  41. data/spec/dummy/tmp/cache/assets/development/sprockets/972b6b0bb1fc0b1123077b4b2f4394bd +0 -0
  42. data/spec/dummy/tmp/cache/assets/development/sprockets/9825272284630c2926f661645205382a +0 -0
  43. data/spec/dummy/tmp/cache/assets/development/sprockets/acbd4771ecb2a173a35382fbff9317cf +0 -0
  44. data/spec/dummy/tmp/cache/assets/development/sprockets/b226a96bfa85a5a853dd6566d1706231 +0 -0
  45. data/spec/dummy/tmp/cache/assets/development/sprockets/b2a142b210c2cbfe4a58040684b3c33c +0 -0
  46. data/spec/dummy/tmp/cache/assets/development/sprockets/bb57f340ac9df9041014540af079a7f3 +0 -0
  47. data/spec/dummy/tmp/cache/assets/development/sprockets/bfa154e8a12acdbc85028ad8130d9043 +0 -0
  48. data/spec/dummy/tmp/cache/assets/development/sprockets/c84af5f8f55b15818667b16b15416431 +0 -0
  49. data/spec/dummy/tmp/cache/assets/development/sprockets/cf573b52d944e8aea7f1dc03f9be9b2c +0 -0
  50. data/spec/dummy/tmp/cache/assets/development/sprockets/d98de652252dc6ae11b5b6db5588705b +0 -0
  51. data/spec/dummy/tmp/cache/assets/development/sprockets/f0aa53eb377a5d7cc83657ef33b02d25 +0 -0
  52. data/spec/dummy/tmp/cache/assets/development/sprockets/f12e838fbbcc1647156ec47ebbeaac9a +0 -0
  53. data/spec/dummy/tmp/cache/assets/development/sprockets/f30abc368796858ff89a5ff6daf33e6a +0 -0
  54. data/spec/dummy/tmp/cache/assets/development/sprockets/f5d1b7d2ec1c89dc32cc982b43502dc8 +0 -0
  55. data/spec/dummy/tmp/cache/assets/development/sprockets/f5e8036e8de24870e4db21ccb209e272 +0 -0
  56. data/spec/dummy/tmp/cache/assets/development/sprockets/f6256b2e4e609981d9eb420aae5bef46 +0 -0
  57. data/spec/dummy/tmp/cache/assets/development/sprockets/f62fb3331d14ad448ad997f1fbdb7e23 +0 -0
  58. data/spec/dummy/tmp/cache/assets/development/sprockets/fd203e335fb22b3109d0354b99f45c6e +0 -0
  59. data/spec/dummy/tmp/cache/assets/development/sprockets/fdc3718e0a84d340a897f87179d95c84 +0 -0
  60. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  61. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  62. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  63. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  64. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  65. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  66. data/spec/javascripts/genie_spec.js +12 -2
  67. data/spec/javascripts/magic_lamp_spec.js +14 -1
  68. data/spec/lib/callbacks_spec.rb +1 -2
  69. data/spec/lib/configuration_spec.rb +8 -8
  70. data/spec/lib/defaults_manager_spec.rb +160 -0
  71. data/spec/lib/fixture_creator_spec.rb +31 -8
  72. data/spec/lib/magic_lamp_spec.rb +147 -54
  73. data/spec/lib/render_catcher_spec.rb +1 -2
  74. data/spec/spec_helper.rb +26 -0
  75. data/spec/tasks/fixture_names_task_spec.rb +4 -12
  76. data/spec/tasks/lint_task_spec.rb +4 -10
  77. metadata +40 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2f6a80fc4bb9835603fed87f0d08b6fad8e6564
4
- data.tar.gz: 6b90fee38734efbb331d547e3514edb9ff200d8a
3
+ metadata.gz: b87fe861fd4ccfea4db716756adf4f5a04d7e645
4
+ data.tar.gz: cfd72a4b279d340905dea883d6f2c181d1a07e76
5
5
  SHA512:
6
- metadata.gz: 46143b9947260d1da0c7e45a9cd26c16aa569e682cd1be085b91a4fcbf1714225b0232b1fbf5aa077fdbc17c0379e1c95caa12bbb68e57ffb10385077dca2719
7
- data.tar.gz: a0fbd5f0fe9c80ddfb99ff86f90133a2c4faadd30ac6ae45ccf2bb44228dd592f047e3b34615b7b96d0c5af8b8a974cd735e669304ba1a12186ef6a3547474ae
6
+ metadata.gz: 648a7ad1efcc83b2f452fc866235f200ffa203f450c8f970efb8f098e3caf264e502fe8bde2ede4490f0a5d008ad8291f32f7a5ecc78ed31d17761f1bdbcb68f
7
+ data.tar.gz: d2e5fadd3b8b04c55b3f4809a5e4b0cc004638232166ac8134119c93a6d1dab895a1ff9b75de736f130f17925e2c914235f8b00198f711c3b24c40ee8acb28ab
data/README.md CHANGED
@@ -48,15 +48,14 @@ Then drop this:
48
48
  ```js
49
49
  //= require magic_lamp
50
50
  ```
51
- at the top of your `spec_helper.js` (assuming your using [Teaspoon](https://github.com/modeset/teaspoon) or another JavaScript spec runner for Rails that
52
- allows for the use of Sprockets directives).
51
+ at the top of your `spec_helper.js` (assuming you're using [Teaspoon](https://github.com/modeset/teaspoon) or another JavaScript spec runner for Rails that allows the use of Sprockets directives).
52
+
53
+ (I highly recommend that you use [Teaspoon](https://github.com/modeset/teaspoon) as your JavaScript spec runner.)
53
54
 
54
55
  Now you've got the basic setup.
55
56
 
56
57
  In case you need it, [here's an example app](https://github.com/crismali/magic_lamp/tree/master/example).
57
58
 
58
- Note: I highly recommend that you use [Teaspoon](https://github.com/modeset/teaspoon) as your JavaScript spec runner.
59
-
60
59
  ### With Database Cleaner
61
60
 
62
61
  You don't need [Database Cleaner](https://github.com/DatabaseCleaner/database_cleaner) to use this gem, but this is probably the setup most people want.
@@ -78,8 +77,7 @@ MagicLamp.configure do |config|
78
77
  end
79
78
  end
80
79
  ```
81
- in a file called `magic_lamp_config.rb` which you can place anywhere in your `spec` or `test` directories. You could
82
- configure Magic Lamp in your `teaspoon_env.rb` file as well.
80
+ in a file called `magic_lamp_config.rb` which you can place anywhere in your `spec` or `test` directories.
83
81
 
84
82
  This way you can take advantage of `after_create` callbacks for your fixture setup without polluting
85
83
  your database every time you run your JavaScript specs.
@@ -90,19 +88,19 @@ Magic Lamp will load all files in your `spec` or `test` directory that end with
90
88
  "lamp files). I'd recommend starting with a single `magic_lamp.rb` file and breaking it into smaller
91
89
  files once it gets unwieldy (one for each controller would be a good approach).
92
90
 
93
- In your lamp files you just call [`MagicLamp.register_fixture`](#register_fixture) like so:
91
+ In your lamp files you just call [`MagicLamp.fixture`](#fixture) like so:
94
92
  ```ruby
95
- MagicLamp.register_fixture do
93
+ MagicLamp.fixture do
96
94
  @order = Order.new
97
95
  render partial: "orders/form"
98
96
  end
99
97
  ```
100
- Inside the block you pass to `register_fixture` you're in the scope of a controller so you
101
- can set up any instance variables your templates depend on. In this case we're using the
102
- default controller which is your `ApplicationController`. We're also using the default
103
- name for the fixture which is whatever `render` receives to identify the template (ie
104
- the symbol or string argument to `render` or whatever is at the `:template` or `:partial`
105
- key in the argument hash).
98
+ Inside the block you pass to `fixture` you're in the scope of a controller so you
99
+ can set up any instance variables your templates depend on. In this case we're
100
+ using the default controller which is your `ApplicationController`. We're also
101
+ using the default name for the fixture which is whatever `render` receives to
102
+ identify the template (ie the symbol or string argument to `render` or
103
+ whatever is at the `:template` or `:partial` key in the argument hash).
106
104
 
107
105
  `render` here also works normally except that it won't render the layout by default.
108
106
 
@@ -112,14 +110,14 @@ beforeEach(function() {
112
110
  MagicLamp.load("orders/form");
113
111
  });
114
112
  ```
115
- which will put the `orders/form` partial in a div with a class of `magic-lamp`. Then you
116
- can go nuts testing your JavaScript against your actual template. If you'd like to only make
113
+ which will put the `orders/form` partial in a div with a class of `magic-lamp` (this all happens synchronously).
114
+ Then you can go nuts testing your JavaScript against your actual template. If you'd like to only make
117
115
  one request for your templates, simply call [`MagicLamp.preload();`](#preload) in your `spec_helper.js` to
118
116
  populate Magic Lamp's cache.
119
117
 
120
118
  ### A few more examples
121
119
  Here we're specifying which controller should render the template via the arguments hash
122
- to `register_fixture`. This gives us access to helper methods in the `register_fixture` block
120
+ to `fixture`. This gives us access to helper methods in the `fixture` block
123
121
  and in the template. It also means we don't need a fully qualified path to the rendered template
124
122
  or partial.
125
123
 
@@ -128,17 +126,17 @@ to render the template the fixture will be named "orders/order".
128
126
 
129
127
  We're also taking advantage of `render`'s `:collection` option.
130
128
  ```ruby
131
- MagicLamp.register_fixture(controller: OrdersController) do
129
+ MagicLamp.fixture(controller: OrdersController) do
132
130
  orders = 3.times.map { Order.new }
133
131
  render partial: "order", collection: orders
134
132
  end
135
133
  ```
136
134
 
137
- Here we're specifying a name with the `:name` option that's passed to `register_fixture`.
135
+ Here we're specifying a name with the `:name` option that's passed to `fixture`.
138
136
  This way we can load the fixture in our JavaScript with `MagicLamp.load("custom/name")` instead
139
- of the default `MagicLamp.load("orders/foo")`. Custom names for fixtures must be url safe strings.
137
+ of the default `MagicLamp.load("orders/foo")`. Custom names for fixtures must be url safe strings. We're also extending the controller and its view with the `AuthStub` module to stub some methods that we don't want executing in our fixtures.
140
138
  ```ruby
141
- MagicLamp.register_fixture(name: "custom/name") do
139
+ MagicLamp.fixture(name: "custom/name", extend: AuthStub) do
142
140
  render "orders/foo"
143
141
  end
144
142
  ```
@@ -146,12 +144,50 @@ end
146
144
  Here we're specifying both a controller and custom name. We're also setting the `params[:foo]`
147
145
  mostly to demonstrate that we have access to all of the usual controller methods.
148
146
  ```ruby
149
- MagicLamp.register_fixture(controller: OrdersController, name: "other_custom_name") do
147
+ MagicLamp.fixture(controller: OrdersController, name: "other_custom_name") do
150
148
  params[:foo] = "test"
151
149
  render :foo
152
150
  end
153
151
  ```
154
152
  If you're interested, [here's an example app](https://github.com/crismali/magic_lamp/tree/master/example).
153
+
154
+ ### Drying up your fixtures
155
+ If you have several fixtures that depend on the same setup (same controller, extensions, etc), you can use the `define` method to dry things up:
156
+
157
+ ```ruby
158
+ MagicLamp.define(controller: OrdersController, extend: AuthStub) do
159
+ fixture do # orders/new
160
+ @order = Order.new
161
+ render :new
162
+ end
163
+
164
+ fixture(name: "customized_new") do # orders/customized_new
165
+ session[:custom_user_info] = "likes movies"
166
+ @order = Order.new
167
+ render :new
168
+ end
169
+
170
+ fixture do # orders/form
171
+ @order = Order.new
172
+ render partial: :form
173
+ end
174
+
175
+ define(namespace: "errors", extend: DeadBeatUserStub)
176
+ fixture(name: "form_without_price") do # orders/errors/form_without_price
177
+ @order = Order.new
178
+ @order.errors.add(:price, "can't be blank")
179
+ render partial: :form
180
+ end
181
+
182
+ fixture do # orders/errors/form
183
+ @order = Order.new
184
+ @order.errors.add(:address, "can't be blank")
185
+ render partial: :form
186
+ end
187
+ end
188
+ end
189
+ ```
190
+
155
191
  Where the files go
156
192
  ------------------------
157
193
  ### Config File
@@ -169,57 +205,145 @@ Call `rake magic_lamp:lint` to see if there are any errors when registering or r
169
205
 
170
206
  Ruby API
171
207
  --------
172
- ### register_fixture
208
+ ### fixture
209
+ (also aliased to `register_fixture` and `register`)
210
+
173
211
  It requires a block that invokes `render` which is invoked in the context of a controller.
174
212
  It also takes an optional hash of arguments. The arguments hash recognizes:
175
213
  * `:controller`
176
214
  * specifies any controller class that you'd like to have render the template or partial.
177
215
  * if specified it removes the need to pass fully qualified paths to templates to `render`
216
+ * the controller's name becomes the default `namespace`, ie `OrdersController` provides a default namespace of `orders` resulting in a template named `orders/foo`
178
217
  * `:name`
179
218
  * whatever you'd like name the fixture.
180
219
  * Specifying this option also prevents the block from being executed twice which could be a performance win. See [configure](#configure) for more.
220
+ * `:extend`
221
+ * takes a module or an array of modules
222
+ * extends the controller and view context (via Ruby's `extend`)
181
223
  Also note that only symbol keys are recognized.
182
224
 
183
- `register_fixture` will also execute any callbacks you've specified. See [configure](#configure) for more.
225
+ `fixture` will also execute any callbacks you've specified. See [configure](#configure) for more.
184
226
 
185
227
  Example:
186
228
  ```ruby
187
- MagicLamp.register_fixture(name: "foo", controller: OrdersController) do
229
+ MagicLamp.fixture(name: "foo", controller: OrdersController) do
188
230
  @order = Order.new
189
231
  render partial: :form
190
232
  end
191
233
  ```
234
+
235
+ ### define
236
+ Allows you scope groups of fixtures with defaults and can be nested arbitrarily. It takes an optional hash and a required block. The hash accepts the following options:
237
+ * `:controller`
238
+ * specifies any controller class that you'd like to have render the template or partial.
239
+ * if specified it removes the need to pass fully qualified paths to templates to `render`
240
+ * the controller's name becomes the default `namespace` if no namespace is provided, ie `OrdersController` provides a default namespace of `orders` resulting in a template named `orders/foo`
241
+ * `:name`
242
+ * whatever you'd like name the fixture.
243
+ * Specifying this option also prevents the block from being executed twice which could be a performance win. See [configure](#configure) for more.
244
+ * `:extend`
245
+ * takes a module or an array of modules
246
+ * extends the controller and view context (via Ruby's `extend`)
247
+ * `:namespace`
248
+ * namespaces all fixtures defined within it
249
+ * overrides the default controller namespace if passed
250
+ Also note that only symbol keys are recognized.
251
+
252
+ Inside of the block you can nest more calls to `define` and create fixtures
253
+ via the `fixture` method or one of its aliases.
254
+
255
+ Example:
256
+ ```ruby
257
+ module DefinesFoo
258
+ def foo
259
+ "foo!"
260
+ end
261
+ end
262
+
263
+ module AlsoDefinesFoo
264
+ def foo
265
+ "also foo!"
266
+ end
267
+ end
268
+
269
+ MagicLamp.define(controller: OrdersController, extend: DefinesFoo) do
270
+
271
+ fixture do # orders/edit
272
+ foo #=> "foo!"
273
+ @order = Order.create!
274
+ render :edit
275
+ end
276
+
277
+ fixture do # orders/new
278
+ foo #=> "foo!"
279
+ @order = Order.new
280
+ render :new
281
+ end
282
+
283
+ define(namespace: "errors", extend: AlsoDefinesFoo) do
284
+
285
+ fixture do # orders/errors/edit
286
+ foo #=> "also foo!"
287
+ @order = Order.create!
288
+ @order.errors.add(:price, "Can't be negative")
289
+ render :edit
290
+ end
291
+
292
+ fixture(extend: DefinesFoo) do # orders/errors/new
293
+ foo #=> "foo!"
294
+ @order = Order.new
295
+ @order.errors.add(:price, "Can't be negative")
296
+ render :new
297
+ end
298
+ end
299
+ end
300
+ ```
301
+
192
302
  ### configure
193
303
  It requires a block to which it yields the configuration object. Here you can set:
194
304
  * `before_each`
195
305
  * takes a block
196
306
  * defaults to `nil`
197
- * called before each block you pass to `register_fixture` is executed
307
+ * called before each block you pass to `fixture` is executed
198
308
  * note: if you call it a second time with a second block, only the second block will be executed
199
309
  * `after_each`
200
310
  * takes a block
201
311
  * defaults to `nil`
202
- * called after each block you pass to `register_fixture` is executed
312
+ * called after each block you pass to `fixture` is executed
203
313
  * note: if you call it a second time with a second block, only the second block will be executed
314
+ * `global_defaults`
315
+ * can be set to a hash of default options that every fixture generated will inherit from. Options passed to `define` and `fixture` take precedence.
316
+ * accepts all of the keys `define` accepts
204
317
  * `infer_names`
205
318
  * defaults to `true`
206
319
  * if set to true, Magic Lamp will try to infer the name of the fixture when not provided with a name parameter.
207
- * if set to false, the name parameter becomes required for `MagicLamp.register_fixture` (this can be done to improve performance or force your team to be more explicit)
320
+ * if set to false, the name parameter becomes required for `MagicLamp.fixture` (this can be done to improve performance or force your team to be more explicit)
208
321
 
209
322
  Example:
210
323
 
211
324
  ```ruby
325
+ module AuthStub
326
+ def current_user
327
+ @current_user ||= User.create!(
328
+ email: "foo@example.com",
329
+ password: "password"
330
+ )
331
+ end
332
+ end
333
+
212
334
  MagicLamp.configure do |config|
213
335
 
214
- # if you want to require the name parameter for `MagicLamp.register_fixture`
336
+ # if you want to require the name parameter for the `fixture` method
215
337
  config.infer_names = false
216
338
 
339
+ config.global_defaults = { extend: AuthStub }
340
+
217
341
  config.before_each do
218
- puts "I appear before the block passed to register fixture executes!"
342
+ puts "I appear before the block passed to `fixture` executes!"
219
343
  end
220
344
 
221
345
  config.after_each do
222
- puts "I appear after the block passed to register fixture executes!"
346
+ puts "I appear after the block passed to `fixture` executes!"
223
347
  end
224
348
  end
225
349
  ```
@@ -245,6 +369,8 @@ container if present so you never end up with duplicate fixture containers or en
245
369
  dom elements from previous loads. It will hit the network only on the first request for a given
246
370
  fixture. If you never want `load` to hit the network, call `MagicLamp.preload()` before your specs.
247
371
 
372
+ The call to get your template is completely synchronous.
373
+
248
374
  Example:
249
375
  ```js
250
376
  beforeEach(function() {
@@ -256,6 +382,8 @@ Call `MagicLamp.preload` to load all of your templates into MagicLamp's cache. T
256
382
  only hit the network once, so the rest of your specs will be quicker and you can go wild stubbing the
257
383
  network.
258
384
 
385
+ The call to get your templates is completely synchronous.
386
+
259
387
  Example:
260
388
  ```js
261
389
  // Probably should be in your `spec_helper.js`
@@ -310,8 +438,10 @@ Sweet aliases
310
438
  ### Ruby
311
439
 
312
440
  ```ruby
313
- MagicLamp.rub => register_fixture
314
- MagicLamp.wish => register_fixture
441
+ MagicLamp.register_fixture => fixture
442
+ MagicLamp.register => fixture
443
+ MagicLamp.rub => fixture
444
+ MagicLamp.wish => fixture
315
445
  ```
316
446
 
317
447
  ### JavaScript
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -76,7 +76,7 @@
76
76
  if (this.xhrStatus(xhr) === 400) {
77
77
  this.handleError(xhr.responseText);
78
78
  } else if (this.xhrStatus(xhr) === 500) {
79
- this.handleError('Something went wrong while generating the fixture, please check the server log or run `rake magic_lamp:lint` for more information');
79
+ this.handleError('Something went wrong, please check the server log or run `rake magic_lamp:lint` for more information');
80
80
  }
81
81
  return xhr;
82
82
  },
data/lib/magic_lamp.rb CHANGED
@@ -1,34 +1,18 @@
1
1
  require "rake"
2
2
 
3
+ require "magic_lamp/engine"
4
+ require "magic_lamp/constants"
3
5
  require "magic_lamp/callbacks"
4
6
 
5
7
  require "magic_lamp/configuration"
8
+ require "magic_lamp/defaults_manager"
6
9
  require "magic_lamp/fixture_creator"
7
10
  require "magic_lamp/render_catcher"
8
- require "magic_lamp/engine"
9
11
 
10
12
  require "tasks/lint_task"
11
13
  require "tasks/fixture_names_task"
12
14
 
13
15
  module MagicLamp
14
- LAMP = "_lamp"
15
- SPEC = "spec"
16
- STARS = "**"
17
- TEST = "test"
18
- Genie = Engine
19
-
20
- class AmbiguousFixtureNameError < StandardError
21
- end
22
-
23
- class UnregisteredFixtureError < StandardError
24
- end
25
-
26
- class AlreadyRegisteredFixtureError < StandardError
27
- end
28
-
29
- class ArgumentError < StandardError
30
- end
31
-
32
16
  class << self
33
17
  attr_accessor :registered_fixtures, :configuration
34
18
 
@@ -36,20 +20,25 @@ module MagicLamp
36
20
  Rails.root.join(directory_path)
37
21
  end
38
22
 
39
- def register_fixture(options = {}, &block)
40
- controller_class = options.fetch(:controller, ::ApplicationController)
41
- raise_missing_block_error(block, __method__)
42
- fixture_name = fixture_name_or_raise(options[:name], controller_class, block)
23
+ def register_fixture(options = {}, &render_block)
24
+ raise_missing_block_error(render_block, __method__)
25
+
26
+ options[:controller] ||= ::ApplicationController
27
+ options[:namespace] ||= options[:controller].controller_name
28
+ options[:extend] = Array(options[:extend])
29
+ options[:render_block] = render_block
30
+ fixture_name = namespaced_fixture_name_or_raise(options)
43
31
 
44
32
  if registered?(fixture_name)
45
33
  raise AlreadyRegisteredFixtureError, "a fixture called '#{fixture_name}' has already been registered"
46
34
  end
47
35
 
48
- registered_fixtures[fixture_name] = [controller_class, block]
36
+ registered_fixtures[fixture_name] = options
49
37
  end
50
38
 
51
- alias_method :rub, :register_fixture
52
- alias_method :wish, :register_fixture
39
+ REGISTER_FIXTURE_ALIASES.each do |method_name|
40
+ alias_method method_name, :register_fixture
41
+ end
53
42
 
54
43
  def configure(&block)
55
44
  raise_missing_block_error(block, __method__)
@@ -57,6 +46,13 @@ module MagicLamp
57
46
  block.call(configuration)
58
47
  end
59
48
 
49
+ def define(options = {}, &block)
50
+ raise_missing_block_error(block, __method__)
51
+ defaults_manager = DefaultsManager.new(configuration, options)
52
+ defaults_manager.instance_eval(&block)
53
+ defaults_manager
54
+ end
55
+
60
56
  def registered?(fixture_name)
61
57
  registered_fixtures.key?(fixture_name)
62
58
  end
@@ -75,8 +71,8 @@ module MagicLamp
75
71
  unless registered?(fixture_name)
76
72
  raise UnregisteredFixtureError, "'#{fixture_name}' is not a registered fixture"
77
73
  end
78
- controller_class, block = registered_fixtures[fixture_name]
79
- FixtureCreator.new(configuration).generate_template(controller_class, &block)
74
+ controller_class, block, extensions = registered_fixtures[fixture_name].values_at(:controller, :render_block, :extend)
75
+ FixtureCreator.new(configuration).generate_template(controller_class, extensions, &block)
80
76
  end
81
77
 
82
78
  def generate_all_fixtures
@@ -88,6 +84,32 @@ module MagicLamp
88
84
 
89
85
  private
90
86
 
87
+ def namespaced_fixture_name_or_raise(options)
88
+ fixture_name = options.delete(:name)
89
+ controller_class, render_block = options.values_at(:controller, :render_block)
90
+ fixture_name = fixture_name_or_raise(fixture_name, controller_class, render_block)
91
+ namespace_fixture_name(fixture_name, options[:namespace])
92
+ end
93
+
94
+ def namespace_fixture_name(fixture_name, namespace)
95
+ namespace_without_application = strip_application(namespace)
96
+ full_name = compose_full_name(namespace_without_application, fixture_name)
97
+
98
+ full_name.split(FORWARD_SLASH).each do |namespace_piece|
99
+ namespace_piece_doubled = [namespace_piece, namespace_piece].join(FORWARD_SLASH)
100
+ full_name.gsub!(namespace_piece_doubled, namespace_piece)
101
+ end
102
+ full_name
103
+ end
104
+
105
+ def strip_application(namespace)
106
+ namespace.gsub(APPLICATION_MATCHER, EMPTY_STRING)
107
+ end
108
+
109
+ def compose_full_name(namespace, fixture_name)
110
+ [namespace, fixture_name].select(&:present?).join(FORWARD_SLASH)
111
+ end
112
+
91
113
  def fixture_name_or_raise(fixture_name, controller_class, block)
92
114
  if fixture_name.nil? && configuration.infer_names
93
115
  default_fixture_name(controller_class, block)
@@ -118,7 +140,6 @@ module MagicLamp
118
140
  if fixture_name.blank?
119
141
  raise AmbiguousFixtureNameError, "Unable to infer fixture name"
120
142
  end
121
- fixture_name = prepend_controller_name(fixture_name, controller_class)
122
143
  fixture_name
123
144
  end
124
145
 
@@ -135,16 +156,6 @@ module MagicLamp
135
156
  end
136
157
  end
137
158
 
138
- def prepend_controller_name(fixture_name, controller_class)
139
- controller_name = controller_class.controller_name
140
- controller_name_regex = Regexp.new("\\A#{controller_name}")
141
- if controller_name == "application" || fixture_name.match(controller_name_regex)
142
- fixture_name
143
- else
144
- "#{controller_name}/#{fixture_name}"
145
- end
146
- end
147
-
148
159
  def directory_path
149
160
  Dir.exist?(Rails.root.join(SPEC)) ? SPEC : TEST
150
161
  end