This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaperclip_tips.html
145 lines (99 loc) · 3.39 KB
/
paperclip_tips.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Paperclip Tips</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="lib/reveal.js/css/reveal.min.css">
<link rel="stylesheet" href="lib/reveal.js/css/theme/night.css">
<link rel="stylesheet" href="lib/template.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/reveal.js/lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides" id="content">
<section data-markdown
data-separator="==="
data-vertical="---"><script type="text/template">
# Paperclip Tips
===
## Firefox and mime types
---
### The bug
* Opened since 2007
* Basically, Firefox sends content with a bad mime type
* These mime types aren't recognized by Paperclip validator
---
### The fix
* `config/initializers/fix_paperclip_mime_types.rb`
* Force detecting the mime type of uploaded files if the current mime type is suspicious
* You can add more suspicious mime types to `MimeTypeFixer::BAD_MIME_TYPES`
===
## File fixtures
---
### The need
```ruby
# You can find those everywhere in the specs
File.new("#{Rails.root}/spec/fixtures/cv_for_test.pdf")
File.open(Rails.root.join('spec', 'fixtures', 'jobteaser_update.xml'))
Rails.root.join('spec', 'fixtures', 'other_images', '42.jpg').to_s
```
It's a bit ugly isn't it?
---
### A solution
`spec/support/file_fixture.rb`
```ruby
FileFixture.path('other_images', '42.jpg')
# => #<Pathname:/path/to/my/apps/jobteaser/spec/fixtures/other_images/42.jpg>
FileFixture.get('jobteaser_update.xml')
# => #<File:/path/to/my/apps/jobteaser/spec/fixtures/jobteaser_update.xml>
FileFixture.read('cv_for_test.pdf')
# => The whole content of the cv_for_test.pdf fixture file
```
===
## File content
---
### The need
* Sometimes we need to access to the content of a Paperclip attachment
* We need a method which works for local and distant files (Paperclip uses both)
```ruby
# The previous code looked like this
HTTParty.get attachment.url, ...
```
---
### The solution
Paperclip provides a gracious way of handling this
```ruby
Paperclip.io_adapters.for(attachment).read
```
This has been added to some models as the `#content` method
```ruby
# You can now simply use
cover_letter = Candidacy::CoverLetter.last
cover_letter.content # => Reads content of the file wherever it is stored
```
</script></section>
</div>
</div>
<script src="lib/reveal.js/lib/js/head.min.js"></script>
<script src="lib/reveal.js/js/reveal.min.js"></script>
<script src="lib/jquery/dist/jquery.min.js"></script>
<script src="config.js"></script>
</body>
</html>