refile 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of refile might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f88d8e0859054884b85f7fc478d6e93ddf05d84
4
- data.tar.gz: f48b85dfeb3f29e91dd1b7871c5d74216bc95eda
3
+ metadata.gz: d93614e14a47419b97ee9062afbbe4d1df8fbdeb
4
+ data.tar.gz: 352d8c297f223caeddd1bc9b2fe0381ae8f2ce80
5
5
  SHA512:
6
- metadata.gz: af3a13531db636c5b4c611911a7d03cced2c43c2f8c4edf4a4f300f4de55b90e7b86cf173d6c5f1c69cbc672de59d248014ac593cd1e9b5ea1e14f727035f921
7
- data.tar.gz: 73f3e4a8c8503f3445313bb363900ff394edb8ce974267cb0c491bc3552c273d910be64e6d546afcf4162a1bef447d8bdd4759280712dcbe9ab9ecf9bd06e2bc
6
+ metadata.gz: ecbafbc35dc7a1fac5fbd9d0bd92681362bc09f188dd494e7e76d2a2d5a4fb2c2538c19c64bf167c508f1ed62d853e0ad64f6dfbd5cf99a94b1ca236ca7c6eef
7
+ data.tar.gz: 7f1e0714723dcabd5d884d3e031664a2153d987d464df0a43547aaba94de10d68fc761de7a87e2075abca911cfce4db5b2d527120ba4323f01bd9a5a0bf72214
data/History.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 0.5.2
2
+
3
+ Release date: 2015-01-13
4
+
5
+ - [ADDED] Can generate URLs without using the Rails helper via `Refile.attachment_url`
6
+ - [FIXED] Regression in `attachment_image_tag`, was not using `Refile.host`.
7
+ - [FIXED] Record without file can be updated when content type and filename are not persisted
8
+ - [FIXED] Remove `id` attribute from hidden field, so it doesn't get confused with the file field
9
+
1
10
  # 0.5.1
2
11
 
3
12
  Release date: 2015-01-11
data/README.md CHANGED
@@ -506,9 +506,9 @@ need to do is add columns for these:
506
506
  ``` ruby
507
507
  class StoreMetadata < ActiveRecord::Migration
508
508
  def change
509
- add_column :profile_image_filename
510
- add_column :profile_image_size
511
- add_column :profile_image_content_type
509
+ add_column :users, :profile_image_filename, :string
510
+ add_column :users, :profile_image_size, :integer
511
+ add_column :users, :profile_image_content_type, :string
512
512
  end
513
513
  end
514
514
  ```
data/lib/refile.rb CHANGED
@@ -217,6 +217,55 @@ module Refile
217
217
  end
218
218
  end
219
219
  end
220
+
221
+ # Generate a URL to an attachment. This method receives an instance of a
222
+ # class which has used the {Refile::Attachment#attachment} macro to
223
+ # generate an attachment column, and the name of this column, and based on
224
+ # this generates a URL to a {Refile::App}.
225
+ #
226
+ # Optionally the name of a processor and arguments to it can be appended.
227
+ #
228
+ # If the filename option is not given, the filename is taken from the
229
+ # metadata stored in the attachment, or eventually falls back to the
230
+ # `name`.
231
+ #
232
+ # The host defaults to {Refile.host}, which is useful for serving all
233
+ # attachments from a CDN. You can also override the host via the `host`
234
+ # option.
235
+ #
236
+ # Returns `nil` if there is no file attached.
237
+ #
238
+ # @example
239
+ # attachment_url(@post, :document)
240
+ #
241
+ # @example With processor
242
+ # attachment_url(@post, :image, :fill, 300, 300, format: "jpg")
243
+ #
244
+ # @param [Refile::Attachment] object Instance of a class which has an attached file
245
+ # @param [Symbol] name The name of the attachment column
246
+ # @param [String, nil] filename The filename to be appended to the URL
247
+ # @param [String, nil] format A file extension to be appended to the URL
248
+ # @param [String, nil] host Override the host
249
+ # @param [String, nil] prefix Adds a prefix to the URL if the application is not mounted at root
250
+ # @return [String, nil] The generated URL
251
+ def attachment_url(object, name, *args, prefix: nil, filename: nil, format: nil, host: nil)
252
+ attacher = object.send(:"#{name}_attacher")
253
+ file = attacher.get
254
+ return unless file
255
+
256
+ host ||= Refile.host
257
+ filename ||= attacher.basename || name.to_s
258
+ format ||= attacher.extension
259
+
260
+ backend_name = Refile.backends.key(file.backend)
261
+
262
+ filename = Rack::Utils.escape(filename)
263
+ filename << "." << format.to_s if format
264
+
265
+ uri = URI(host.to_s)
266
+ uri.path = ::File.join("", *prefix, backend_name, *args.map(&:to_s), file.id.to_s, filename)
267
+ uri.to_s
268
+ end
220
269
  end
221
270
 
222
271
  require "refile/version"
@@ -148,7 +148,7 @@ module Refile
148
148
  end
149
149
 
150
150
  def present?
151
- id or not @metadata.empty?
151
+ not @metadata.empty?
152
152
  end
153
153
 
154
154
  def valid?
@@ -14,44 +14,16 @@ module Refile
14
14
  # to the {Refile::App} which is assumed to be mounted in the Rails
15
15
  # application.
16
16
  #
17
- # Optionally the name of a processor and a arguments to it can be appended.
17
+ # @see Refile.attachment_url
18
18
  #
19
- # If the filename option is not given, the filename falls back to the
20
- # `name`.
21
- #
22
- # The host defaults to {Refile.host}, which is useful for serving all
23
- # attachments from a CDN. You can also override the host via the `host`
24
- # option.
25
- #
26
- # Returns `nil` if there is no file attached.
27
- #
28
- # @example
29
- # attachment_url(@post, :document)
30
- #
31
- # @example With processor
32
- # attachment_url(@post, :image, :fill, 300, 300, format: "jpg")
33
- #
34
- # @param [Refile::Attachment] record Instance of a class which has an attached file
19
+ # @param [Refile::Attachment] object Instance of a class which has an attached file
35
20
  # @param [Symbol] name The name of the attachment column
36
21
  # @param [String, nil] filename The filename to be appended to the URL
37
22
  # @param [String, nil] format A file extension to be appended to the URL
38
23
  # @param [String, nil] host Override the host
39
24
  # @return [String, nil] The generated URL
40
- def attachment_url(record, name, *args, filename: nil, format: nil, host: nil)
41
- attacher = record.send(:"#{name}_attacher")
42
- file = attacher.get
43
- return unless file
44
-
45
- filename ||= attacher.basename || name.to_s
46
- format ||= attacher.extension
47
-
48
- backend_name = Refile.backends.key(file.backend)
49
- host = host || Refile.host || request.base_url
50
-
51
- filename = filename.parameterize("_")
52
- filename << "." << format.to_s if format
53
-
54
- ::File.join(host, main_app.refile_app_path, backend_name, *args.map(&:to_s), file.id.to_s, filename)
25
+ def attachment_url(record, name, *args, **opts)
26
+ Refile.attachment_url(record, name, *args, prefix: main_app.refile_app_path, **opts)
55
27
  end
56
28
 
57
29
  # Generates an image tag for the given attachment, adding appropriate
@@ -106,7 +78,7 @@ module Refile
106
78
  options[:data].merge!(direct: true).merge!(attacher.cache.presign.as_json)
107
79
  end
108
80
 
109
- html = hidden_field(object_name, method, value: attacher.data.to_json, object: object)
81
+ html = hidden_field(object_name, method, value: attacher.data.to_json, object: object, id: nil)
110
82
  html + file_field(object_name, method, options)
111
83
  end
112
84
  end
@@ -1,3 +1,3 @@
1
1
  module Refile
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -13,7 +13,6 @@ class TestMigration < ActiveRecord::Migration
13
13
  create_table :posts, force: true do |t|
14
14
  t.column :title, :string
15
15
  t.column :image_id, :string
16
- t.column :image_content_type, :string
17
16
  t.column :document_id, :string
18
17
  t.column :document_filename, :string
19
18
  t.column :document_content_type, :string
@@ -47,6 +47,21 @@ feature "Normal HTTP Post file uploads" do
47
47
  expect(page).not_to have_link("Document")
48
48
  end
49
49
 
50
+ scenario "Successfully update a record with an attached file" do
51
+ visit "/normal/posts/new"
52
+ fill_in "Title", with: "A cool post"
53
+ attach_file "Image", path("image.jpg")
54
+ click_button "Create"
55
+
56
+ expect(page).to have_selector("h1", text: "A cool post")
57
+ click_link("Edit")
58
+
59
+ fill_in "Title", with: "A very cool post"
60
+
61
+ click_button "Update"
62
+ expect(page).to have_selector("h1", text: "A very cool post")
63
+ end
64
+
50
65
  # FIXME: the only reason this is js:true is because the rack_test driver
51
66
  # doesn't submit file+metadata correctly.
52
67
  scenario "Upload a file via form redisplay", js: true do
Binary file
data/spec/refile_spec.rb CHANGED
@@ -71,4 +71,78 @@ RSpec.describe Refile do
71
71
  expect(name).to be_nil
72
72
  end
73
73
  end
74
+
75
+ describe ".attachment_url" do
76
+ let(:klass) do
77
+ Class.new do
78
+ extend Refile::Attachment
79
+ attachment :document
80
+ end
81
+ end
82
+ let(:instance) { klass.new }
83
+ let(:id) { instance.document_attacher.cache_id }
84
+
85
+ before do
86
+ allow(Refile).to receive(:host).and_return(nil)
87
+ end
88
+
89
+ context "with file" do
90
+ before do
91
+ instance.document = Refile::FileDouble.new("hello")
92
+ end
93
+
94
+ it "generates a url from an attachment" do
95
+ expect(Refile.attachment_url(instance, :document)).to eq("/cache/#{id}/document")
96
+ end
97
+
98
+ it "uses supplied host option" do
99
+ expect(Refile.attachment_url(instance, :document, host: "http://example.org")).to eq("http://example.org/cache/#{id}/document")
100
+ end
101
+
102
+ it "falls back to Refile.host" do
103
+ allow(Refile).to receive(:host).and_return("http://elabs.se")
104
+
105
+ expect(Refile.attachment_url(instance, :document)).to eq("http://elabs.se/cache/#{id}/document")
106
+ end
107
+
108
+ it "adds a prefix" do
109
+ expect(Refile.attachment_url(instance, :document, prefix: "moo")).to eq("/moo/cache/#{id}/document")
110
+ end
111
+
112
+ it "adds an escaped filename" do
113
+ expect(Refile.attachment_url(instance, :document, filename: "test.png")).to eq("/cache/#{id}/test.png")
114
+ expect(Refile.attachment_url(instance, :document, filename: "tes/t.png")).to eq("/cache/#{id}/tes%2Ft.png")
115
+ end
116
+
117
+ it "adds a format" do
118
+ expect(Refile.attachment_url(instance, :document, format: "png")).to eq("/cache/#{id}/document.png")
119
+ end
120
+ end
121
+
122
+ context "with file with content type" do
123
+ before do
124
+ instance.document = Refile::FileDouble.new("hello", content_type: "image/png")
125
+ end
126
+
127
+ it "adds format inferred from content type" do
128
+ expect(Refile.attachment_url(instance, :document)).to eq("/cache/#{id}/document.png")
129
+ end
130
+ end
131
+
132
+ context "with file with filename" do
133
+ before do
134
+ instance.document = Refile::FileDouble.new("hello", "hello.html")
135
+ end
136
+
137
+ it "adds filename" do
138
+ expect(Refile.attachment_url(instance, :document)).to eq("/cache/#{id}/hello.html")
139
+ end
140
+ end
141
+
142
+ context "with no file" do
143
+ it "returns nil" do
144
+ expect(Refile.attachment_url(instance, :document)).to be_nil
145
+ end
146
+ end
147
+ end
74
148
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-11 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -313,8 +313,8 @@ files:
313
313
  - spec/refile/features/normal_upload_spec.rb
314
314
  - spec/refile/features/presigned_upload_spec.rb
315
315
  - spec/refile/fixtures/hello.txt
316
+ - spec/refile/fixtures/image.jpg
316
317
  - spec/refile/fixtures/large.txt
317
- - spec/refile/rails/attachment_helper_spec.rb
318
318
  - spec/refile/spec_helper.rb
319
319
  - spec/refile/test_app.rb
320
320
  - spec/refile/test_app/app/assets/javascripts/application.js
@@ -375,8 +375,8 @@ test_files:
375
375
  - spec/refile/features/normal_upload_spec.rb
376
376
  - spec/refile/features/presigned_upload_spec.rb
377
377
  - spec/refile/fixtures/hello.txt
378
+ - spec/refile/fixtures/image.jpg
378
379
  - spec/refile/fixtures/large.txt
379
- - spec/refile/rails/attachment_helper_spec.rb
380
380
  - spec/refile/spec_helper.rb
381
381
  - spec/refile/test_app.rb
382
382
  - spec/refile/test_app/app/assets/javascripts/application.js
@@ -1,61 +0,0 @@
1
- require "refile"
2
- require "active_support/inflector"
3
- require "refile/rails/attachment_helper"
4
-
5
- describe Refile::AttachmentHelper do
6
- describe "#attachment_url", pending: "rewrite without mocks" do
7
- let(:view) { Struct.new(:main_app, :request).new(main_app, request) }
8
- let(:main_app) { double :main_app, refile_app_path: "attachments" }
9
- let(:request) { double :request, base_url: "http://www.example.com" }
10
- let(:record) { double :record, name: name, image: file }
11
- let(:name) { :image }
12
- let(:file) { double :file, backend: backend, id: "123abc" }
13
- let(:backend) { double :backend }
14
- before do
15
- view.extend(Refile::AttachmentHelper)
16
- Refile.backends["test_backend"] = backend
17
- end
18
- after do
19
- Refile.backends.delete("test_backend")
20
- end
21
-
22
- context "with a host passed in" do
23
- let(:host) { "//cdn.example.com" }
24
-
25
- it "creates the expected url" do
26
- url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg", host: host)
27
- expect(url).to eq "//cdn.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
28
- end
29
- end
30
-
31
- context "with no host passed in, but Refile.host set" do
32
- before do
33
- @original_host = Refile.host
34
- Refile.host = "//cdn.example.com"
35
- end
36
- after do
37
- Refile.host = @original_host
38
- end
39
-
40
- it "creates the expected url" do
41
- url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg")
42
- expect(url).to eq "//cdn.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
43
- end
44
- end
45
-
46
- context "with no host passed in, and no Refile.host set" do
47
- before do
48
- @original_host = Refile.host
49
- Refile.host = nil
50
- end
51
- after do
52
- Refile.host = @original_host
53
- end
54
-
55
- it "creates the expected url" do
56
- url = view.attachment_url(record, name, "fill", 300, 300, filename: "test", format: "jpg")
57
- expect(url).to eq "http://www.example.com/attachments/test_backend/fill/300/300/123abc/test.jpg"
58
- end
59
- end
60
- end
61
- end