govuk_content_models 10.3.0 → 10.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/app/models/attachment.rb +1 -1
- data/app/traits/attachable.rb +7 -2
- data/govuk_content_models.gemspec +1 -1
- data/lib/govuk_content_models/version.rb +1 -1
- data/test/traits/attachable_test.rb +88 -17
- metadata +5 -5
data/CHANGELOG.md
CHANGED
data/app/models/attachment.rb
CHANGED
data/app/traits/attachable.rb
CHANGED
@@ -55,9 +55,14 @@ module Attachable
|
|
55
55
|
define_method("upload_#{field}") do
|
56
56
|
raise ApiClientNotPresent unless Attachable.asset_api_client
|
57
57
|
begin
|
58
|
-
|
58
|
+
if options[:update_existing] && !self.send("#{field}_id").nil?
|
59
|
+
response = Attachable.asset_api_client.update_asset(self.send("#{field}_id"), file: instance_variable_get("@#{field}_file"))
|
60
|
+
else
|
61
|
+
response = Attachable.asset_api_client.create_asset(:file => instance_variable_get("@#{field}_file"))
|
62
|
+
self.send("#{field}_id=", response.id.split('/').last)
|
63
|
+
end
|
64
|
+
|
59
65
|
self.send("#{field}_url=", response.file_url) if self.respond_to?("#{field}_url=")
|
60
|
-
self.send("#{field}_id=", response.id.split('/').last)
|
61
66
|
rescue StandardError
|
62
67
|
errors.add("#{field}_id".to_sym, "could not be uploaded")
|
63
68
|
end
|
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
|
18
18
|
gem.add_dependency "bson_ext"
|
19
19
|
gem.add_dependency "differ"
|
20
|
-
gem.add_dependency "gds-api-adapters"
|
20
|
+
gem.add_dependency "gds-api-adapters", ">= 10.9.0"
|
21
21
|
|
22
22
|
gem.add_dependency "gds-sso", ">= 7.0.0", "< 10.0.0"
|
23
23
|
gem.add_dependency "govspeak", ">= 1.0.1", "< 2.0.0"
|
@@ -1,9 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class MockAssetApi
|
4
|
-
class MockError < StandardError; end
|
5
|
-
end
|
6
|
-
|
7
3
|
class ModelWithAttachments
|
8
4
|
include Attachable
|
9
5
|
include Mongoid::Document
|
@@ -20,13 +16,23 @@ class ModelWithAttachmentsAndUrl
|
|
20
16
|
attaches :image, with_url_field: true
|
21
17
|
end
|
22
18
|
|
19
|
+
class ModelWithUpdatableAttachments
|
20
|
+
include Attachable
|
21
|
+
include Mongoid::Document
|
22
|
+
|
23
|
+
field :title, type: String
|
24
|
+
attaches :image, update_existing: true, with_url_field: true
|
25
|
+
end
|
26
|
+
|
23
27
|
class AttachableTest < ActiveSupport::TestCase
|
24
28
|
|
25
29
|
setup do
|
26
30
|
@edition = ModelWithAttachments.new
|
27
31
|
@edition_with_url_field = ModelWithAttachmentsAndUrl.new
|
32
|
+
@edition_with_update_option = ModelWithUpdatableAttachments.new
|
28
33
|
@previous_api_client = Attachable.asset_api_client
|
29
|
-
|
34
|
+
@mock_asset_api = mock("mock_asset_api")
|
35
|
+
Attachable.asset_api_client = @mock_asset_api
|
30
36
|
end
|
31
37
|
|
32
38
|
teardown do
|
@@ -47,7 +53,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
47
53
|
@edition.image_id = "an_image_id"
|
48
54
|
|
49
55
|
asset = OpenStruct.new(:file_url => "/path/to/image")
|
50
|
-
|
56
|
+
@mock_asset_api.expects(:asset).with("an_image_id").returns(asset)
|
51
57
|
|
52
58
|
assert_equal "/path/to/image", @edition.image.file_url
|
53
59
|
end
|
@@ -56,7 +62,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
56
62
|
@edition.image_id = "an_image_id"
|
57
63
|
|
58
64
|
asset = OpenStruct.new(:something => "one", :something_else => "two")
|
59
|
-
|
65
|
+
@mock_asset_api.expects(:asset).once.with("an_image_id").returns(asset)
|
60
66
|
|
61
67
|
assert_equal "one", @edition.image.something
|
62
68
|
assert_equal "two", @edition.image.something_else
|
@@ -64,13 +70,12 @@ class AttachableTest < ActiveSupport::TestCase
|
|
64
70
|
|
65
71
|
should "assign a file and detect it has changed" do
|
66
72
|
file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
|
67
|
-
|
68
73
|
@edition.image = file
|
69
74
|
assert @edition.image_has_changed?
|
70
75
|
end
|
71
76
|
end
|
72
77
|
|
73
|
-
context "saving an edition" do
|
78
|
+
context "saving an edition without update_existing set" do
|
74
79
|
setup do
|
75
80
|
@file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
|
76
81
|
@asset = OpenStruct.new(
|
@@ -79,20 +84,27 @@ class AttachableTest < ActiveSupport::TestCase
|
|
79
84
|
)
|
80
85
|
end
|
81
86
|
|
82
|
-
should "
|
83
|
-
|
87
|
+
should "create another asset even if an asset already exists" do
|
88
|
+
@edition.image_id = "foo"
|
89
|
+
@mock_asset_api.expects(:create_asset).returns(@asset)
|
90
|
+
|
91
|
+
@edition.image = @file
|
92
|
+
@edition.save!
|
93
|
+
end
|
94
|
+
|
95
|
+
should "create an asset when one does not exist" do
|
96
|
+
@mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
|
84
97
|
|
85
98
|
@edition.image = @file
|
86
99
|
@edition.save!
|
87
100
|
end
|
88
101
|
|
89
102
|
should "not upload an asset if it has not changed" do
|
90
|
-
ModelWithAttachments.any_instance.expects(:upload_image).never
|
91
103
|
@edition.save!
|
92
104
|
end
|
93
105
|
|
94
106
|
should "assign the asset id to the attachment id attribute" do
|
95
|
-
|
107
|
+
@mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
|
96
108
|
|
97
109
|
@edition.image = @file
|
98
110
|
@edition.save!
|
@@ -101,7 +113,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
101
113
|
end
|
102
114
|
|
103
115
|
should "assign the asset url to the attachment url attribute if requested" do
|
104
|
-
|
116
|
+
@mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
|
105
117
|
|
106
118
|
@edition_with_url_field.image = @file
|
107
119
|
@edition_with_url_field.save!
|
@@ -110,7 +122,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
110
122
|
end
|
111
123
|
|
112
124
|
should "not create the attachment url attribute if not requested" do
|
113
|
-
|
125
|
+
@mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
|
114
126
|
|
115
127
|
@edition.image = @file
|
116
128
|
@edition.save!
|
@@ -128,7 +140,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
128
140
|
end
|
129
141
|
|
130
142
|
should "catch any errors raised by the api client" do
|
131
|
-
|
143
|
+
@mock_asset_api.expects(:create_asset).raises(StandardError)
|
132
144
|
|
133
145
|
assert_nothing_raised do
|
134
146
|
@edition.image = @file
|
@@ -139,7 +151,7 @@ class AttachableTest < ActiveSupport::TestCase
|
|
139
151
|
end
|
140
152
|
|
141
153
|
should "not stop the edition from being saved when an uploading error is raised" do
|
142
|
-
|
154
|
+
@mock_asset_api.expects(:create_asset).raises(StandardError)
|
143
155
|
|
144
156
|
@edition.image = @file
|
145
157
|
@edition.title = "foo"
|
@@ -170,4 +182,63 @@ class AttachableTest < ActiveSupport::TestCase
|
|
170
182
|
end
|
171
183
|
end
|
172
184
|
|
185
|
+
context "with update_existing option set" do
|
186
|
+
setup do
|
187
|
+
@file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
|
188
|
+
|
189
|
+
@asset_id = 'an_image_id'
|
190
|
+
|
191
|
+
@asset_response = OpenStruct.new(
|
192
|
+
id: "http://asset-manager.dev.gov.uk/assets/#{@asset_id}",
|
193
|
+
file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg'
|
194
|
+
)
|
195
|
+
end
|
196
|
+
|
197
|
+
context "saving an edition without an existing asset" do
|
198
|
+
should "create a new asset" do
|
199
|
+
@mock_asset_api.expects(:create_asset).with(:file => @file).returns(@asset_response)
|
200
|
+
|
201
|
+
@edition_with_update_option.image = @file
|
202
|
+
@edition_with_update_option.save!
|
203
|
+
end
|
204
|
+
|
205
|
+
should "assign the asset id and file url" do
|
206
|
+
@mock_asset_api.stubs(:create_asset).returns(@asset_response)
|
207
|
+
|
208
|
+
@edition_with_update_option.image = @file
|
209
|
+
@edition_with_update_option.save!
|
210
|
+
|
211
|
+
assert_equal @asset_id, @edition_with_update_option.image_id
|
212
|
+
assert_equal @asset_response.file_url, @edition_with_update_option.image_url
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
context "saving an edition with and existing asset" do
|
217
|
+
setup do
|
218
|
+
@existing_asset = OpenStruct.new(
|
219
|
+
id: "http://asset-manager.dev.gov.uk/assets/#{@asset_id}",
|
220
|
+
file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/old_image.jpg'
|
221
|
+
)
|
222
|
+
|
223
|
+
@edition_with_update_option.image_id = @asset_id
|
224
|
+
@edition_with_update_option.image_url = @existing_asset.file_url
|
225
|
+
end
|
226
|
+
|
227
|
+
should "update the asset on save" do
|
228
|
+
@mock_asset_api.expects(:update_asset).with(@asset_id, :file => @file).returns(@asset_response)
|
229
|
+
|
230
|
+
@edition_with_update_option.image = @file
|
231
|
+
@edition_with_update_option.save!
|
232
|
+
end
|
233
|
+
|
234
|
+
should "update the file url for the asset" do
|
235
|
+
@mock_asset_api.stubs(:update_asset).returns(@asset_response)
|
236
|
+
|
237
|
+
@edition_with_update_option.image = @file
|
238
|
+
@edition_with_update_option.save!
|
239
|
+
|
240
|
+
assert_equal @asset_response.file_url, @edition_with_update_option.image_url
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
173
244
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_content_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 10.
|
4
|
+
version: 10.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 10.9.0
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 10.9.0
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: gds-sso
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -465,7 +465,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
465
465
|
version: '0'
|
466
466
|
segments:
|
467
467
|
- 0
|
468
|
-
hash:
|
468
|
+
hash: 2179825358001692589
|
469
469
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
470
470
|
none: false
|
471
471
|
requirements:
|
@@ -474,7 +474,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
474
474
|
version: '0'
|
475
475
|
segments:
|
476
476
|
- 0
|
477
|
-
hash:
|
477
|
+
hash: 2179825358001692589
|
478
478
|
requirements: []
|
479
479
|
rubyforge_project:
|
480
480
|
rubygems_version: 1.8.23
|