-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
executable file
·1664 lines (1290 loc) · 125 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Atwam]]></title>
<link href="http://blog.atwam.com/atom.xml" rel="self"/>
<link href="http://blog.atwam.com/"/>
<updated>2016-02-04T12:07:15+00:00</updated>
<id>http://blog.atwam.com/</id>
<author>
<name><![CDATA[Atwam]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Motorbike Prices]]></title>
<link href="http://blog.atwam.com/blog/2016/02/03/motorbike-prices-part-1/"/>
<updated>2016-02-03T20:13:00+00:00</updated>
<id>http://blog.atwam.com/blog/2016/02/03/motorbike-prices-part-1</id>
<content type="html"><![CDATA[<p>Being a <em>data scientist</em> (yup, that what we are called nowadays, apparently we are all the rage) and looking for my first bike to buy made me think (a lot) about what ride wanted.</p>
<p>Hundreds of pages on various forums talking about which bike is good, which one is better, reliability, position, torque, sound, etc etc.
Yet in the end, once you have set your mind on one or two models, you still have dozens (if not hundreds) of classifieds for these models, and it is sometime hard to know whether the price is fair, a good deal, or an overpriced deal.</p>
<p>So, having lots of time on my hands at the moment, I started taking a quantitative approach.</p>
<ul>
<li>Step 1 : Get all the data (or as much as I can).</li>
<li>Step 2 : Analysis, model</li>
<li>Step 3 : Profit (now that I have a model, I can know whether any ad is a good deal or not).</li>
</ul>
<!--more-->
<h1>Step 1 : Get all the data</h1>
<p>The basis of my data is a request on a reputable motorbike classifieds website, looking for bikes which cost more than £500 (to avoid all the “wanted”) posts, and with more than 200cc. I’m not claiming the data is perfect, far from it, but if you know a better source, let me know.</p>
<h1>Step 2 : Analysis</h1>
<div class="highlight"><pre><code class="language-r" data-lang="r">libs<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">'data.table'</span><span class="p">,</span><span class="s">'knitr'</span><span class="p">,</span><span class="s">'ggplot2'</span><span class="p">,</span><span class="s">'stringr'</span><span class="p">,</span><span class="s">'pander'</span><span class="p">,</span><span class="s">'dplyr'</span><span class="p">,</span><span class="s">'lubridate'</span><span class="p">)</span>
<span class="kp">invisible</span><span class="p">(</span><span class="kp">lapply</span><span class="p">(</span>libs<span class="p">,</span> <span class="kn">require</span><span class="p">,</span> character.only<span class="o">=</span><span class="kc">TRUE</span><span class="p">))</span>
theme_set<span class="p">(</span>theme_bw<span class="p">())</span>
data <span class="o">=</span> fread<span class="p">(</span><span class="s">'~/Dev/autoray/out.csv'</span><span class="p">)</span>
data <span class="o">=</span> <span class="kp">within</span><span class="p">(</span>data<span class="p">,</span> <span class="p">{</span>
cc <span class="o">=</span> <span class="kp">as.integer</span><span class="p">(</span>cc<span class="p">)</span>
cc<span class="p">[</span>cc <span class="o">></span> <span class="m">4000</span><span class="p">]</span> <span class="o">=</span> <span class="kc">NA</span>
seller_type <span class="o">=</span> <span class="kp">as.factor</span><span class="p">(</span>seller_type<span class="p">)</span>
<span class="kt">category</span> <span class="o">=</span> <span class="kp">as.factor</span><span class="p">(</span><span class="kp">category</span><span class="p">)</span>
year <span class="o">=</span> <span class="kp">as.integer</span><span class="p">(</span>year<span class="p">)</span>
year<span class="p">[</span>year <span class="o">></span> <span class="m">2016</span> <span class="o">|</span> year <span class="o"><</span> <span class="m">1915</span><span class="p">]</span> <span class="o">=</span> <span class="kc">NA</span>
mileage <span class="o">=</span> <span class="kp">as.integer</span><span class="p">(</span>mileage<span class="p">)</span>
age <span class="o">=</span> <span class="m">2016</span> <span class="o">-</span> year
title <span class="o">=</span> str_to_lower<span class="p">(</span>title<span class="p">)</span>
description <span class="o">=</span> str_to_lower<span class="p">(</span>description<span class="p">)</span>
search <span class="o">=</span> <span class="kp">paste</span><span class="p">(</span>title<span class="p">,</span> <span class="s">' '</span><span class="p">,</span> description<span class="p">)</span>
abs <span class="o">=</span> str_detect<span class="p">(</span><span class="kp">search</span><span class="p">,</span> <span class="s">'abs'</span><span class="p">)</span>
panniers <span class="o">=</span> str_detect<span class="p">(</span><span class="kp">search</span><span class="p">,</span> <span class="s">'panniers'</span><span class="p">)</span>
ad_age <span class="o">=</span> <span class="kp">as.integer</span><span class="p">(</span><span class="kp">Sys.time</span><span class="p">()</span> <span class="o">-</span> parse_date_time<span class="p">(</span>str_extract<span class="p">(</span><span class="kp">url</span><span class="p">,</span> <span class="s">"20\\d{6}"</span><span class="p">),</span> <span class="s">'Ymd'</span><span class="p">))</span>
ad_age<span class="p">[</span>ad_age <span class="o">></span> <span class="m">365</span><span class="o">*</span><span class="m">2</span><span class="p">]</span> <span class="o">=</span> <span class="kc">NA</span> <span class="c1"># Let's ignore all ads older than 2 years for ad_age</span>
<span class="p">})</span></code></pre></div>
<p>As you can see below, I gathered prices and information for 17043 bikes.
Let’s have a look at our data :</p>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>data<span class="p">[,</span><span class="kp">sapply</span><span class="p">(</span>data<span class="p">,</span> <span class="kp">is.numeric</span><span class="p">),</span> with<span class="o">=</span><span class="kc">FALSE</span><span class="p">])</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">## price distance year mileage
## Min. : 500 Min. : 1.0 Min. :1926 Min. : 1
## 1st Qu.: 4137 1st Qu.: 54.0 1st Qu.:2008 1st Qu.: 2000
## Median : 6000 Median :114.0 Median :2012 Median : 7266
## Mean : 6857 Mean :116.7 Mean :2010 Mean : 10844
## 3rd Qu.: 8695 3rd Qu.:168.0 3rd Qu.:2015 3rd Qu.: 15900
## Max. :55950 Max. :445.0 Max. :2016 Max. :333902
## NA's :521 NA's :1832
## cc ad_age age
## Min. : 191.0 Min. : 3.00 Min. : 0.000
## 1st Qu.: 650.0 1st Qu.: 33.75 1st Qu.: 1.000
## Median : 900.0 Median : 99.00 Median : 4.000
## Mean : 921.3 Mean :133.55 Mean : 5.573
## 3rd Qu.:1170.0 3rd Qu.:182.00 3rd Qu.: 8.000
## Max. :3072.0 Max. :730.00 Max. :90.000
## NA's :19 NA's :2011 NA's :521</code></pre></div>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>data<span class="p">[,</span><span class="o">!</span><span class="kp">sapply</span><span class="p">(</span>data<span class="p">,</span> <span class="kp">is.numeric</span><span class="p">),</span> with<span class="o">=</span><span class="kc">FALSE</span><span class="p">])</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">## title brand seller_type category
## Length:17043 Length:17043 Private seller: 558 :16950
## Class :character Class :character Trade seller :16485 C: 57
## Mode :character Mode :character D: 36
##
## url description panniers abs
## Length:17043 Length:17043 Mode :logical Mode :logical
## Class :character Class :character FALSE:16123 FALSE:13347
## Mode :character Mode :character TRUE :920 TRUE :3696
## NA's :0 NA's :0
## search
## Length:17043
## Class :character
## Mode :character
##</code></pre></div>
<p>Here are the factors I managed to get to explain the prices.</p>
<ul>
<li><em>cc</em>: The engine size, in cc</li>
<li><em>seller_type</em> : Whether the ad was posted by a private or a trade seller</li>
<li><em>category</em> : Whether the bike is in an insurance category (that is, a totaled bike).</li>
<li><em>year</em> : Year of registration of the bike</li>
<li><em>mileage</em> : Mileage, duh</li>
<li><em>title</em>, <em>description</em>, <em>search</em> : Plain text about the bike, used to add some other factors or infer the brand, model, etc.</li>
<li><em>abs</em>, <em>panniers</em> : I look for these words in the titles, description. Not perfect, especially because I look them as strings, not as words. So a description about an <em>abs</em>olute steal would match an <em>abs</em>. Yeah, that sucks, but I haven’t had the time to research what NLP tools R offers for stemming.</li>
<li><em>ad_age</em> : The age of the ad, calculated from a time stamp in the url. This one actually gets lots of <code>N/A</code>, because some urls are for ads and don’t show the timestamp.</li>
</ul>
<h1>Step 2 : Analysis</h1>
<h2>Impact of the age on the price</h2>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>age<span class="p">,</span> color<span class="o">=</span><span class="kp">category</span><span class="p">))</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">90</span><span class="p">,</span><span class="m">10</span><span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-3-1.png" title="center" alt="center" width="800px" /></p>
<p>What can we see on this graph ?</p>
<ul>
<li>Prices go down almost linearly during the first 15 years.</li>
<li>After that, you can expect prices to remain kind-of stable for bikes which are just about to become vintage.</li>
<li>Motorbikes more than 30 years old start to see a constant rise in value.</li>
<li>Cat C and Cat D bikes have a value that is constantly lower than the value of non-category motorbikes. They decrease in value steadily.</li>
</ul>
<p>Now, let’s focus on bikes which are less than 15 years old, not in a category.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r">data <span class="o">=</span> <span class="kp">subset</span><span class="p">(</span>data<span class="p">,</span> age <span class="o"><=</span> <span class="m">15</span> <span class="o">&</span> <span class="kt">category</span> <span class="o">==</span> <span class="s">""</span><span class="p">)</span></code></pre></div>
<p>We can evaluate this discount :</p>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>lm<span class="p">(</span>price <span class="o">~</span> age<span class="p">,</span> data<span class="p">))</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">##
## Call:
## lm(formula = price ~ age, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8461 -2020 -479 1308 47431
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8961.18 40.13 223.28 <2e-16 ***
## age -442.60 6.81 -64.99 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3236 on 15503 degrees of freedom
## Multiple R-squared: 0.2141, Adjusted R-squared: 0.2141
## F-statistic: 4224 on 1 and 15503 DF, p-value: < 2.2e-16</code></pre></div>
<p>We see whe should expect a loss of value of £442/year, but it makes more sense to assume this loss in value should be a percentage of the original value, which would be <code>442/8961</code>, or about 5%/year for the first 15 years.</p>
<h2>Impact of brands</h2>
<p>As a first approximation, we can take the first word of the ad title to get the brand. Brands that use several words will be trimmed, but that should be good enough for now.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r">data<span class="o">$</span>brand <span class="o">=</span> <span class="kp">as.factor</span><span class="p">(</span>word<span class="p">(</span>data<span class="o">$</span>title<span class="p">))</span>
brands <span class="o">=</span> data <span class="o">%>%</span> group_by<span class="p">(</span>brand<span class="p">)</span> <span class="o">%>%</span> tally<span class="p">(</span>sort <span class="o">=</span> <span class="kc">TRUE</span><span class="p">)</span>
big_brands <span class="o">=</span> brands<span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">8</span><span class="p">]</span><span class="o">$</span>brand
data<span class="o">$</span>big_brand <span class="o">=</span> data<span class="o">$</span>brand <span class="o">%in%</span> big_brands
<span class="c1"># Let's try a colorblind friendly version</span>
cbPalette <span class="o"><-</span> <span class="kt">c</span><span class="p">(</span><span class="s">"#999999"</span><span class="p">,</span> <span class="s">"#E69F00"</span><span class="p">,</span> <span class="s">"#56B4E9"</span><span class="p">,</span> <span class="s">"#009E73"</span><span class="p">,</span> <span class="s">"#F0E442"</span><span class="p">,</span> <span class="s">"#0072B2"</span><span class="p">,</span> <span class="s">"#D55E00"</span><span class="p">,</span> <span class="s">"#CC79A7"</span><span class="p">)</span>
ggplot<span class="p">(</span>data<span class="p">[</span>data<span class="o">$</span>big_brand<span class="p">])</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>age<span class="p">,</span> color<span class="o">=</span>brand<span class="p">),</span> se <span class="o">=</span> <span class="kc">FALSE</span><span class="p">)</span> <span class="o">+</span> scale_colour_manual<span class="p">(</span>values<span class="o">=</span>cbPalette<span class="p">)</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-6-1.png" title="center" alt="center" width="800px" /></p>
<p>Here we can see not only the difference in average price between brands (be careful, I haven’t distinguished between models here), but also how these decay with time.</p>
<p>We can see that Harleys are consistently more expensive, but seem to hold their value relative to other bikes quite well. Ducatis see a sharp drop at around 5 years.
BMW are more expensive than the other brands as well in their first 7 years or so, but then drop in price to be actually cheaper than other brands. My guess is that because they are more expensive, they tend to be favoured by people able to buy them new, but the second-hand market for them isn’t that great.</p>
<h2>Engine size</h2>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>cc<span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-7-1.png" title="center" alt="center" width="800px" /></p>
<p>Not much to see here by default, we can just see that the price of a bike grows (almost linearly) with the engine size. That is until 1800cc (Goldwings), then beyond 2000cc you enter the realm of the Rocket III which is just less expensive than the goldwing.</p>
<h2>Mileage</h2>
<p>Some bikes seem to have crazy mileage (300k), so let’s stick to stuff with reasonable values, less than 100k.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span><span class="kp">subset</span><span class="p">(</span>data<span class="p">,</span> mileage <span class="o"><</span> <span class="m">100000</span><span class="p">))</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>mileage<span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-8-1.png" title="center" alt="center" width="800px" /></p>
<p>Big surprise, the price drops as the mileage increase. The effect seems almost linear to start with, then seems to decay. We can probably work with a linear decay for the first 25k, beyond that it just makes sense to use a log decay…</p>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>lm<span class="p">(</span>price <span class="o">~</span> mileage<span class="p">,</span> <span class="kp">subset</span><span class="p">(</span>data<span class="p">,</span> mileage <span class="o"><</span> <span class="m">25000</span><span class="p">)))</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">##
## Call:
## lm(formula = price ~ mileage, data = subset(data, mileage < 25000))
##
## Residuals:
## Min 1Q Median 3Q Max
## -7708 -2237 -612 1667 47778
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.208e+03 4.409e+01 186.18 <2e-16 ***
## mileage -1.569e-01 4.401e-03 -35.65 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3349 on 12764 degrees of freedom
## Multiple R-squared: 0.09055, Adjusted R-squared: 0.09048
## F-statistic: 1271 on 1 and 12764 DF, p-value: < 2.2e-16</code></pre></div>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>lm<span class="p">(</span>price <span class="o">~</span> <span class="kp">log</span><span class="p">(</span><span class="m">1</span> <span class="o">+</span> mileage<span class="p">),</span> data<span class="p">))</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">##
## Call:
## lm(formula = price ~ log(1 + mileage), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8961 -2347 -603 1613 48229
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9715.54 91.98 105.6 <2e-16 ***
## log(1 + mileage) -366.54 10.97 -33.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3370 on 14061 degrees of freedom
## (1442 observations deleted due to missingness)
## Multiple R-squared: 0.0735, Adjusted R-squared: 0.07344
## F-statistic: 1115 on 1 and 14061 DF, p-value: < 2.2e-16</code></pre></div>
<p>What we can see here is that the price of the bike seems to drop by 0.1569 per mile, or £156/1000miles.
Translating again to a percentage of the intercept, we get 0.0190058 or about 1.9% per 1000 miles.</p>
<p>This figure could depend on the brand quite heavily (and more importantly on the type/model) of bike. You’d expect tourers, VFRs and R1200RT to hold their value with mileage a lot better than some shiny supersport bikes. We’ll have a look at that in another post.</p>
<h2>Mileage + age</h2>
<p>Mileage and age are obviously very (read 0.6531262 = 65%) correlated, so we’ll have to use both in our price regression.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r"><span class="kp">summary</span><span class="p">(</span>lm<span class="p">(</span>price <span class="o">~</span> age <span class="o">*</span> <span class="kp">log</span><span class="p">(</span><span class="m">1</span><span class="o">+</span>mileage<span class="p">),</span> <span class="kp">subset</span><span class="p">(</span>data<span class="p">,</span> mileage <span class="o"><</span> <span class="m">100000</span><span class="p">)))</span></code></pre></div>
<div class="highlight"><pre><code class="language-text" data-lang="text">##
## Call:
## lm(formula = price ~ age * log(1 + mileage), data = subset(data,
## mileage < 1e+05))
##
## Residuals:
## Min 1Q Median 3Q Max
## -7869 -1921 -400 1292 47533
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8325.324 98.211 84.770 < 2e-16 ***
## age -3.961 45.923 -0.086 0.931
## log(1 + mileage) 62.846 13.223 4.753 2.02e-06 ***
## age:log(1 + mileage) -45.364 4.654 -9.748 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3075 on 14055 degrees of freedom
## Multiple R-squared: 0.2289, Adjusted R-squared: 0.2287
## F-statistic: 1390 on 3 and 14055 DF, p-value: < 2.2e-16</code></pre></div>
<p>We can pretty much ignore the coef in front of age (low tstat), we get something that is roughly :
$ price = 8325 + logMileage * (62 - 45 * age) $</p>
<h2>Are private seller cheaper than trade sellers ?</h2>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>age<span class="p">,</span> color<span class="o">=</span>seller_type<span class="p">))</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">90</span><span class="p">,</span><span class="m">10</span><span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-11-1.png" title="center" alt="center" width="800px" /></p>
<p>The answer seems consistently <em>yes</em> !</p>
<h2>Do good deals go quickly ?</h2>
<p>It seems intuitive to think that good deals will be sold quickly, and the longer an ad has been there, the more likely it is that the bike is above its market price. Trade sellers and their automated posting software would skew the results, by having software that refreshes ads every-so-often, but we should still see some effect.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>ad_age<span class="p">))</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">365</span><span class="o">*</span><span class="m">2</span><span class="p">,</span><span class="m">30</span><span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-12-1.png" title="center" alt="center" width="800px" /></p>
<p>So <em>yes</em>, our intuition holds, it looks like the average price for new ads is around £1000 less than for ads that are 2 months old.
There is a bias here, because many ads for new models are put by dealers and left forever.
Let’s run the query again, this time only looking a bikes which are more than 2 years old, and comparing dealers/private sellers.</p>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">[</span>age <span class="o">>=</span> <span class="m">2</span><span class="p">])</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>ad_age<span class="p">,</span> color<span class="o">=</span>seller_type<span class="p">))</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">365</span><span class="o">*</span><span class="m">2</span><span class="p">,</span><span class="m">30</span><span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-13-1.png" title="center" alt="center" width="800px" /></p>
<p>What we can see here is still that good deals move quickly, and this difference is even more pronounced for private sellers than it is for dealers. An ad that has been out for two months is on average £1000 more expensive than the one that will get sold within its first few days.</p>
<p>We’ll see in a next post whether it still holds when looking at data for one single model.</p>
<h2>ABS</h2>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>age<span class="p">,</span> color<span class="o">=</span><span class="kp">abs</span><span class="p">))</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>breaks <span class="o">=</span> <span class="kp">seq</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">90</span><span class="p">,</span><span class="m">10</span><span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-14-1.png" title="center" alt="center" width="800px" /></p>
<p>Well, we can see that ABS definitely seems to keep its increased price, at least for recent bikes. One has to be careful though, because ABS is usually found on higher end bikes, so this difference in price could just be a bias. One would have to do a price comparison for a specific model (with enough data).</p>
<h2>Distance to london</h2>
<div class="highlight"><pre><code class="language-r" data-lang="r">ggplot<span class="p">(</span>data<span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>aes<span class="p">(</span>y<span class="o">=</span>price<span class="p">,</span> x<span class="o">=</span>distance<span class="p">))</span></code></pre></div>
<p><img src="http://blog.atwam.com/figs/2015-02-03-motorbike-prices-part-1/unnamed-chunk-15-1.png" title="center" alt="center" width="800px" /></p>
<p>Hard to say anything here. Could be some bias from big cities that appear at some distance from London. Given we don’t have the postcode for all ads, it’s hard to go beyond this useless graph.</p>
<h1>Conclusion</h1>
<p>I’ve only looked at a few things, and it seems obvious here that many results are probably biased given I’m not filtering to a specific model.</p>
<p>Next time, I’ll look at the most frequent bike of the most frequent brand, and we’ll see if these results still hold. Plus yeah, we’ll have a first look at our pricing model and try to find some underpriced bikes.</p>
<p>Stay tuned (and feel free to guess what model we’ll be looking at in the comments, or let me know if you’d be interested in another specific analysis).</p>
<p><strong>If you want to re-publish all or part of this data/graphs/article, get in touch. Just ask.</strong></p>
<p><em>I’d like to get some data on insurance as well, but it’s a lot harder to gather. If you know a way, let me know…</em></p>
<p><em>Edit 1 : 2015-02-04 11:24 GMT - Changed the brand graph to only use 8 brands but with colorblinds-friendly palette.</em></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[From pow to a deployed rails app using chef, capistrano and vagrant - Part 3]]></title>
<link href="http://blog.atwam.com/blog/2013/06/12/adding-an-app-server-to-our-box/"/>
<updated>2013-06-12T08:47:00+01:00</updated>
<id>http://blog.atwam.com/blog/2013/06/12/adding-an-app-server-to-our-box</id>
<content type="html"><![CDATA[<p>Now that we have a server running our database, we want to make it also able to run our rails app.
As I said in the previous post, I could use the full <code>application</code> cookbook, but I’d have to create my
own cookbook and recipe to use that properly. For now, I’ll just rely on <code>rackbox</code> (and its dependency <code>appbox</code>).</p>
<h2>Managing with <code>appbox</code></h2>
<p>The <code>appbox</code> cookbook does pretty much the stuff that we configured earlier (setting up users etc). Since it’s a dependency of <code>rackbox</code>,
we are pretty much forced to use it (<code>rackbox</code> does call <code>appbox</code> default recipe).</p>
<p>Having added <code>appbox</code> to our <code>Berksfile</code>, I had to modify the <code>roles/base.rb</code> :</p>
<ul>
<li>Let app box know that my admin user name is wam.</li>
<li>Generate a ssh key for to login as the deploy user.</li>
<li>Make sure the app box recipe is run before I run my <code>sudo</code> and <code>users::sysadmins</code> recipe.</li>
</ul>
<p>We’ll start by creating the deploy key :</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>ssh-keygen -t rsa -C <span class="s2">"wam@scube"</span> -f ~/.ssh/scube_deploy_rsa
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>roles/base.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">name</span> <span class="s2">"base"</span>
</span><span class='line'><span class="n">description</span> <span class="s2">"Base role applied to all nodes."</span>
</span><span class='line'><span class="n">run_list</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"recipe[apt]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[build-essential]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[git]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"appbox"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[users::sysadmins]"</span><span class="p">,</span> <span class="c1"># Necessary to run after appbox to add our stuff</span>
</span><span class='line'> <span class="s2">"recipe[sudo]"</span> <span class="c1"># Same</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'><span class="n">override_attributes</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"appbox"</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="s2">"deploy_keys"</span> <span class="o">=></span> <span class="o">[</span>
</span><span class='line'> <span class="s2">"ssh-rsa [...]"</span> <span class="c1"># content of ~/.ssh/scube_deploy_rsa.pub</span>
</span><span class='line'> <span class="o">]</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"admin_user"</span> <span class="o">=></span> <span class="s2">"wam"</span><span class="p">,</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Adding a rack server</h2>
<p>I’ve chosen to use passenger + nginx, which is a popular choice among the rails community. I was tempted for a moment by puma on jruby, but I want
my app online faster and will bother changing this kind of thing later (chef makes it easy to test new nodes with new recipes..)</p>
<p>Let’s create a <code>roles/app_server.rb</code> :</p>
<figure class='code'><figcaption><span>roles/app_server.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">name</span> <span class="s2">"app_server"</span>
</span><span class='line'><span class="n">description</span> <span class="s2">"Serving http requests for the app. Main app server"</span>
</span><span class='line'><span class="n">run_list</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"rackbox"</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'><span class="n">override_attributes</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"rackbox"</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="s2">"ruby"</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="s2">"versions"</span><span class="o">=></span> <span class="o">[</span><span class="s2">"1.9.3-p385"</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"global"</span><span class="o">=></span> <span class="s2">"1.9.3-p385"</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="s2">"apps"</span><span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="s2">"passenger"</span><span class="o">=></span> <span class="o">[</span>
</span><span class='line'> <span class="p">{</span><span class="s2">"appname"</span><span class="o">=></span> <span class="s2">"scube"</span><span class="p">,</span> <span class="s2">"hostname"</span><span class="o">=></span> <span class="s2">"my.hostname.com"</span><span class="p">}</span>
</span><span class='line'> <span class="o">]</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now to make it work with vagrant, two changes are necessary in our <code>Vagrantfile</code> :</p>
<ul>
<li>We need to make sure that the <code>chef</code> version we are using is 11 or more. By default my vagrant was using chef 10, and the <code>rackbox</code>
(more specifically the <code>runit</code> it uses) was throwing an error (NameError: Cannot find a resource for load_new_resource_state on ubuntu version 12.04).</li>
<li>We add a port mapping to access the http port of our server on <code>localhost:8888</code></li>
</ul>
<figure class='code'><figcaption><span>Vagrantfile</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Stuff here</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">network</span> <span class="ss">:forwarded_port</span><span class="p">,</span> <span class="ss">guest</span><span class="p">:</span> <span class="mi">80</span><span class="p">,</span> <span class="ss">host</span><span class="p">:</span> <span class="mi">8888</span>
</span><span class='line'><span class="c1"># Put this line just before your config.vm.provision :chef_solo line</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">vm</span><span class="o">.</span><span class="n">provision</span> <span class="ss">:shell</span><span class="p">,</span> <span class="ss">:inline</span> <span class="o">=></span> <span class="s2">"gem install chef --version 11.4.2 --no-rdoc --no-ri --conservative"</span>
</span></code></pre></td></tr></table></div></figure>
<p>The <code>:shell</code> provision makes sure that vagrant updates the chef gem before actually running our <code>chef-solo</code> provision.
Now it’s on to <code>vagrant provision</code>, stuff should appear in green and <a href="http://localhost:8888">http://localhost:8888</a> should show a 404 error.
Yes of course, we haven’t deployed our app yet. That’ll be next.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[From pow to a deployed rails app using chef, capistrano and vagrant - Part 2]]></title>
<link href="http://blog.atwam.com/blog/2013/06/10/configuring-chef-solo-for-our-servers/"/>
<updated>2013-06-10T23:03:00+01:00</updated>
<id>http://blog.atwam.com/blog/2013/06/10/configuring-chef-solo-for-our-servers</id>
<content type="html"><![CDATA[<p>Now that we have our vagrant box working with chef, let’s use chef to configure our services and our app.</p>
<h2>Chef roles</h2>
<p>Assuming that you have read some basics about chef, you’ll know that the cookbooks we have downloaded provide recipes for installing various software.
We could ask vagrant to install a few recipes, but it’s probably better to assemble them in roles.
We’ll then assign the roles to one or several nodes, or use all of them on our box for testing.</p>
<p>For now, we probably want to have one <code>base</code> role (to install common software on all our nodes) and two roles to serve our application :</p>
<ul>
<li><code>database\_master</code> : a simple install of postgres should be fine here.</li>
<li><code>app\_server</code> : this one will serve our RoR app.</li>
</ul>
<p>One could think of other roles (workers, redis etc), but for my purpose and for now these two (and the <code>base</code> role) should be fine.</p>
<h2>Base role</h2>
<p>We want our base role to include the following :</p>
<ul>
<li><em>apt</em>, <em>git</em>, <em>sudo</em> and <em>build-essential</em> should be installed. We’ll use the default cookbooks/recipes for each of these.</li>
<li><em>users setup</em> : Should create the users (with their ssh key), give them sudo rights. We’ll use the <code>users</code> cookbook.</li>
</ul>
<p>We start by editing our <code>Berksfile</code> to make sure all the cookbooks are included (<code>sudo</code>, <code>apt</code>, <code>git</code>, <code>build-essential</code>, <code>users</code>).
Then let’s create a role file.</p>
<figure class='code'><figcaption><span>roles/base.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">name</span> <span class="s2">"base"</span>
</span><span class='line'><span class="n">description</span> <span class="s2">"Base role applied to all nodes."</span>
</span><span class='line'><span class="n">run_list</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"recipe[apt]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[build-essential]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[git]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[users::sysadmins]"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"recipe[sudo]"</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>The order matters here !</em> <em>apt</em> should appear first (it’s used to handle packages), <em>build-essential</em> is used by pretty much
everything, and especially by <code>ruby-shadow</code> which is a gem dependency of <code>users</code>.</p>
<p>Reading <a href="http://community.opscode.com/cookbooks/users">the documentation of the users cookbook</a>, we see that we should define the users in a data bag
(a way of telling chef about some data, list, including potentially encrypted password and ssh keys).</p>
<p>Chef solo doesn’t work very well with data bags (or the CLI doesn’t work very well), so we’ll just create the file manually.
Also, we see in the users cookbook that it requires <code>chef-solo-search</code> to run with chef-solo.</p>
<p>Adding <code>cookbook 'chef-solo-search', git: "https://github.com/edelight/chef-solo-search.git"</code> to our <code>Berksfile</code> should be good enough.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>mkdir data_bags/users
</span><span class='line'><span class="nv">$ </span>vim data_bags/users/wam.json
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>data_bags/users/wam.json</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="nt">"id"</span><span class="p">:</span> <span class="s2">"wam"</span><span class="p">,</span> <span class="err">//or</span> <span class="err">your</span> <span class="err">user</span> <span class="err">name</span>
</span><span class='line'> <span class="err">//</span> <span class="err">The</span> <span class="err">following</span> <span class="err">should</span> <span class="err">be</span> <span class="err">the</span> <span class="err">result</span> <span class="err">of</span> <span class="err">openssl</span> <span class="err">passwd</span> <span class="err">-1</span> <span class="err">plainpasswd</span>
</span><span class='line'> <span class="err">//</span> <span class="err">but</span> <span class="err">that's</span> <span class="err">md5</span> <span class="err">on</span> <span class="err">a</span> <span class="err">mac.</span> <span class="err">Alternatively</span> <span class="err">run</span> <span class="err">mkpasswd</span> <span class="err">-m</span> <span class="err">sha-512</span> <span class="err">-S</span> <span class="err">mySalt</span> <span class="err">on</span> <span class="err">a</span> <span class="err">linux</span> <span class="err">machine</span>
</span><span class='line'> <span class="nt">"password"</span><span class="p">:</span> <span class="s2">"$6$[...]098/"</span><span class="p">,</span>
</span><span class='line'> <span class="nt">"ssh_keys"</span><span class="p">:</span> <span class="p">[</span>
</span><span class='line'> <span class="err">//</span> <span class="err">Copy</span> <span class="err">paste</span> <span class="err">from</span> <span class="err">your</span> <span class="err">ssh</span> <span class="err">public</span> <span class="err">key</span>
</span><span class='line'> <span class="s2">"ssh-rsa AAA123...xyz== foo"</span>
</span><span class='line'> <span class="p">],</span>
</span><span class='line'> <span class="nt">"groups"</span><span class="p">:</span> <span class="p">[</span> <span class="s2">"sysadmin"</span> <span class="p">],</span>
</span><span class='line'> <span class="nt">"uid"</span><span class="p">:</span> <span class="mi">2001</span><span class="p">,</span>
</span><span class='line'> <span class="nt">"shell"</span><span class="p">:</span> <span class="s2">"\/bin\/bash"</span><span class="p">,</span>
</span><span class='line'> <span class="nt">"comment"</span><span class="p">:</span> <span class="s2">""</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now we need to modify our <code>Vagrantfile</code> to use this role (and not the dummy git recipe we were using). An extra bit of precaution is needed here :
the <code>sudo</code> cookbook/recipe will install <code>sudo</code> qnd configure it by default for the sysadmin group (lucky us, our user is a member).
<em>It will override vagrant’s sudo config, breaking vagrant provision using chef-solo</em>. To avoid that, we use vagrant’s <code>chef.json</code> config
to override the <code>sudo</code> configuration attributes for vagrant :</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'> <span class="n">chef</span><span class="o">.</span><span class="n">add_role</span> <span class="s2">"base"</span>
</span><span class='line'> <span class="n">chef</span><span class="o">.</span><span class="n">json</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="ss">:authorization</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="ss">:sudo</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="ss">:users</span> <span class="o">=></span> <span class="o">[</span><span class="s2">"vagrant"</span><span class="o">]</span><span class="p">,</span>
</span><span class='line'> <span class="ss">:passwordless</span> <span class="o">=></span> <span class="kp">true</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Then it’s on to <code>vagrant provision</code>, and ssh to whatever port was forwarded to 22 (for me it was <code>ssh localhost -p 2222</code>) to see that you log in
using your ssh key.</p>
<p>If you hit a json parsing exception when chef reads your user json file, make sure you don’t have trailing commas.
You can check your JSON easily in <code>irb</code> using <code>require 'json'; JSON.parse(File.read('data_bags/users/wam.json'))</code>.</p>
<h3>Creating a custom cookbook …</h3>
<p>There’s a big choice to do here. We could either create a whole separate cookbook just for our app, configured with many
default recipes, or for now just use an already created one.</p>
<p>It is very likely that I’ll have to create a cookbook at some point, because it’s the only way to have your own recipes
and reach a high enough level of customization.</p>
<h3>.. or use and existing one</h3>
<p>I originally had a look at the <a href="http://community.opscode.com/cookbooks/database">database cookbook</a> but finally decided
to go the fast way by using two very neat cookbooks, <a href="https://github.com/teohm/rackbox-cookbook">rackbox</a> and <a href="https://github.com/teohm/databox-cookbook">databox</a>.
It will probably make sense to use <code>database</code> and <code>application</code> cookbooks, but they seem to be easier to work with when you are using a proper chef server
and your own cookbook/recipes.</p>
<p><code>rackbox</code> includes <code>appbox</code> by default, which creates its own users for deployment/app running.
I have found that these cookbooks are a bit limited for my taste (for example, they don’t use data_bags, which are a proper way of encrypting
password instead of storing them in your chef repository… Well, next time.</p>
<h2>Setting up our roles</h2>
<p>Let’s start by adding the cookbooks to our <code>Berksfile</code> and run <code>berks install</code></p>
<figure class='code'><figcaption><span>Berksfile</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">cookbook</span> <span class="s2">"runit"</span><span class="p">,</span> <span class="s2">">= 1.1.2"</span> <span class="c1"># HACK: force-use this version</span>
</span><span class='line'><span class="n">cookbook</span> <span class="s2">"databox"</span>
</span><span class='line'><span class="n">cookbook</span> <span class="s2">"rackbox"</span>
</span></code></pre></td></tr></table></div></figure>
<p>and create our <code>roles/database_master.rb</code>. We are using non encrypted passwords here, which isn’t very secure.
We should actually use encrypted data bags, but they don’t play very nicely with roles (they are supposed to be used with recipes, which
would mean custom cookbook), nor do they play nicely with <code>knife solo</code> (although a plugin exist, but it didn’t work very well
in my tests). Let’s start this way, we’ll see later to move to a more robust non solo chef.</p>
<figure class='code'><figcaption><span>roles/database_master.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">name</span> <span class="s2">"database_master"</span>
</span><span class='line'><span class="n">description</span> <span class="s2">"Master postgresql node"</span>
</span><span class='line'>
</span><span class='line'><span class="n">run_list</span><span class="p">(</span>
</span><span class='line'> <span class="s2">"databox::postgresql"</span> <span class="c1"># Or "databox" to include mysql as well</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'><span class="n">default_attributes</span><span class="p">(</span>
</span><span class='line'> <span class="ss">:databox</span> <span class="o">=></span> <span class="p">{</span>
</span><span class='line'> <span class="ss">:db_root_password</span> <span class="o">=></span> <span class="s2">"PASSWORD_HERE"</span><span class="p">,</span>
</span><span class='line'> <span class="ss">:postgresql</span> <span class="o">=></span> <span class="o">[</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="s2">"database_name"</span> <span class="o">=></span> <span class="s2">"myapp_production"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"username"</span> <span class="o">=></span> <span class="s2">"myapp"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"password"</span> <span class="o">=></span> <span class="s2">"ANOTHER_PASSWORD_HERE"</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="o">]</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now running <code>vagrant provision</code> (or <code>vagrant up</code> or <code>vagrant reload</code> depending on whether your current vagrant box is up or not) should run this recipe, adding
the <code>myapp</code> database. We can test that in <code>vagrant ssh</code></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>psql -h localhost -d myapp_production -U myapp -W
</span><span class='line'>Password <span class="k">for </span>user myapp:
</span><span class='line'><span class="c">#psql (9.1.9)</span>
</span><span class='line'><span class="c">#[...] Yeepee</span>
</span></code></pre></td></tr></table></div></figure>
<h2>What’s next</h2>
<p>Next post will be about configuring a proper rails box using <code>rackbox</code>, setting up capistrano to deploy … then deploy to a vps and get closer to production.
I’m still not entirely happy with this deployment today. I should move to a proper cookbook, as I said, to get more customization options.
For now, I want my app out, and will probably work a bit more later depending on how successful it is. The beauty of chef, after all, is that it makes
it easy to set up new nodes and new deployments.</p>
<h3>References</h3>
<p><a href="http://teohm.github.io/blog/2013/04/17/chef-cookbooks-for-busy-ruby-developers/">Chef cookbooks for busy ruby developers</a></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Using Rspec with inherited_resources helpers and inherited views]]></title>
<link href="http://blog.atwam.com/blog/2013/05/14/using-rspec-with-inherited-resources-helpers-and-inherited-views/"/>
<updated>2013-05-14T19:51:00+01:00</updated>
<id>http://blog.atwam.com/blog/2013/05/14/using-rspec-with-inherited-resources-helpers-and-inherited-views</id>
<content type="html"><![CDATA[<p>I have updated a rails app I have been working on recently to a more recent version of rails 3.2, and all my tests where failing.
Finally managed to have that working, figured I’d show how.</p>
<h3>Mocking inherited_resources helpers in views specs.</h3>
<p>I know I shouldn’t be using <a href="https://github.com/josevalim/inherited_resources"><code>inherited_resources</code></a> anymore (<a href="http://stackoverflow.com/questions/9599201/inherited-resources-deperecated-on-rails-3-responders">see here</a> and <a href="http://archives.ryandaigle.com/articles/2009/8/10/what-s-new-in-edge-rails-default-restful-rendering">here</a>) but I want to release my app before I change everything to use responders.</p>
<p>So, my tests where failing because I was using the <code>resource</code>, <code>collection</code> and <code>resource_class</code> helpers from some views I was using. So first my tests are failing because <code>resource_class</code> isn’t available in my views. I would have thought that the controller helpers were available in the views, but they aren’t.
The solution is easy. Let’s add the following to our <code>spec/support</code> directory :</p>
<figure class='code'><figcaption><span>spec/support/view_resource_macros.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">ViewResourceMacros</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">has_resource</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span>
</span><span class='line'> <span class="n">before</span> <span class="k">do</span>
</span><span class='line'> <span class="c1"># Creates the resource</span>
</span><span class='line'> <span class="vi">@resource</span> <span class="o">||=</span> <span class="k">yield</span>
</span><span class='line'> <span class="c1"># Assign to the symbol we wanted, so it's available in the view</span>
</span><span class='line'> <span class="n">assign</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span> <span class="vi">@resource</span><span class="p">)</span>
</span><span class='line'> <span class="c1"># Assigns to @name so that we can use that in our assertions</span>
</span><span class='line'> <span class="nb">instance_variable_set</span><span class="p">(</span><span class="s2">"@</span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="vi">@resource</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># If we pass an array, it's for stubing a collection, if not it's for stubbing a single object</span>
</span><span class='line'> <span class="k">if</span> <span class="vi">@resource</span><span class="o">.</span><span class="n">is_a?</span><span class="p">(</span><span class="nb">Array</span><span class="p">)</span>
</span><span class='line'> <span class="n">view</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:collection</span><span class="p">)</span> <span class="p">{</span> <span class="vi">@resource</span> <span class="p">}</span>
</span><span class='line'> <span class="n">view</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:resource_class</span><span class="p">)</span> <span class="p">{</span><span class="vi">@resource</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">class</span><span class="p">}</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="n">view</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:resource</span><span class="p">)</span> <span class="p">{</span><span class="vi">@resource</span><span class="p">}</span>
</span><span class='line'> <span class="n">view</span><span class="o">.</span><span class="n">stub</span><span class="p">(</span><span class="ss">:resource_class</span><span class="p">)</span> <span class="p">{</span><span class="vi">@resource</span><span class="o">.</span><span class="n">class</span><span class="p">}</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">RSpec</span><span class="o">.</span><span class="n">configure</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
</span><span class='line'> <span class="n">config</span><span class="o">.</span><span class="n">extend</span> <span class="no">ViewResourceMacros</span><span class="p">,</span> <span class="ss">:type</span> <span class="o">=></span> <span class="ss">:view</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>And see how to transform our old (failing) test :</p>
<figure class='code'><figcaption><span>spec/views/cars/edit.html.haml_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">'spec_helper'</span>
</span><span class='line'>
</span><span class='line'><span class="n">describe</span> <span class="s2">"cars/edit.html.haml"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">before</span><span class="p">(</span><span class="ss">:each</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'> <span class="n">assign</span><span class="p">(</span><span class="ss">:car</span><span class="p">,</span> <span class="vi">@car</span> <span class="o">=</span> <span class="no">Factory</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:car</span><span class="p">))</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"renders the edit view"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">render</span>
</span><span class='line'> <span class="n">rendered</span><span class="o">.</span><span class="n">should</span> <span class="n">contain</span><span class="p">(</span><span class="vi">@car</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>becomes :</p>
<figure class='code'><figcaption><span>spec/views/cars/edit.html.haml_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">'spec_helper'</span>
</span><span class='line'>
</span><span class='line'><span class="n">describe</span> <span class="s2">"cars/edit.html.haml"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">has_resource</span><span class="p">(</span><span class="ss">:car</span><span class="p">)</span> <span class="p">{</span> <span class="no">Factory</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:car</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"renders the edit view"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">render</span>
</span><span class='line'> <span class="n">rendered</span><span class="o">.</span><span class="n">should</span> <span class="n">contain</span><span class="p">(</span><span class="vi">@car</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h3>Using shared inherited partial in our views specs</h3>
<p>Rails 3.1+ offers views inheritance, so I changed my code to have the following :</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">BaseController</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">CarsController</span> <span class="o"><</span> <span class="no">BaseController</span>
</span><span class='line'> <span class="n">inherit_resources</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">PlanesController</span> <span class="o"><</span> <span class="no">BaseController</span>
</span><span class='line'> <span class="n">inherit_resources</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Then I just created a <code>base/new.html.haml</code> and a <code>base/edit.html.haml</code> views, to use the views inheritance.</p>
<figure class='code'><figcaption><span>base/new.html.haml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haml'><span class='line'><span class="nt">%h1</span> Create <span class="si">#{</span><span class="n">resource_class</span><span class="o">.</span><span class="n">model_name</span><span class="o">.</span><span class="n">human</span><span class="si">}</span>
</span><span class='line'><span class="p">=</span> <span class="n">render</span> <span class="ss">:partial</span> <span class="o">=></span> <span class="s2">"form"</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>base/edit.html.haml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='haml'><span class='line'><span class="nt">%h1</span> Edit <span class="si">#{</span><span class="n">resource</span><span class="si">}</span>
</span><span class='line'><span class="p">=</span> <span class="n">render</span> <span class="ss">:partial</span> <span class="o">=></span> <span class="s2">"form"</span>
</span></code></pre></td></tr></table></div></figure>
<p>And I have two <code>_form.html.haml</code> partials, one for each controller.
Now the next issue is that our edit and new views are shared, but we still want to test the <code>_form.html.haml</code> partial.</p>
<figure class='code'><figcaption><span>spec/views/cars/_form.html.haml_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="err">'</span><span class="n">spec_helper</span>
</span><span class='line'>
</span><span class='line'><span class="n">describe</span> <span class="s2">"cars/_form.html.haml"</span> <span class="k">do</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="kp">new</span><span class="p">:</span> <span class="o">-></span> <span class="p">{</span> <span class="no">Car</span><span class="o">.</span><span class="n">new</span> <span class="p">}</span>
</span><span class='line'> <span class="n">edit</span><span class="p">:</span> <span class="o">-></span> <span class="p">{</span> <span class="no">Factory</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">:car</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="p">,</span> <span class="n">block</span><span class="o">|</span>
</span><span class='line'> <span class="n">context</span> <span class="s2">"when called in #</span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">has_resource</span><span class="p">(</span><span class="nb">name</span><span class="p">,</span> <span class="n">block</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"renders the form"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">render</span>
</span><span class='line'> <span class="n">rendered</span><span class="o">.</span><span class="n">should</span> <span class="n">have_selector</span><span class="p">(</span><span class="s2">"form"</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h3>Shared partial</h3>
<p>Finally, when testing for example <code>cars/index.html.haml</code> which uses a partial <code>toolbar.html.haml</code> that actually exists in <code>base</code> views, the following lines are necessary :</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">before</span> <span class="k">do</span>
</span><span class='line'> <span class="n">views</span><span class="o">.</span><span class="n">lookup_context</span><span class="o">.</span><span class="n">prefixes</span> <span class="o"><<</span> <span class="s2">"base"</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>This was <a href="https://github.com/rails/rails/issues/5213">raised as an issue to the rails team</a>, but they commented (rightly I think) that the inheritance
is related to the controller, not the views, so the test case shouldn’t know about it and you’ll have to declare it manually using the lines above.</p>
<p>Now let’s go back and make these tests green again.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[From pow to a deployed rails app using chef, capistrano and vagrant - Part 1]]></title>
<link href="http://blog.atwam.com/blog/2013/04/27/preparing-a-rails-app-for-deployment-using-chef-solo/"/>
<updated>2013-04-27T22:09:00+01:00</updated>
<id>http://blog.atwam.com/blog/2013/04/27/preparing-a-rails-app-for-deployment-using-chef-solo</id>