govuk_content_models 8.9.0 → 8.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 8.10.0
2
+
3
+ * Adds a flag to `attaches` to have the `Attachable`
4
+ mixin add a {field}_url attribute containing the
5
+ attachment's public URL.
6
+
1
7
  ## 8.9.0
2
8
 
3
9
  * Add label fields to RenderedSpecialistDocument
@@ -7,16 +7,12 @@ class Attachment
7
7
 
8
8
  field :title
9
9
  field :filename
10
- attaches :file
10
+ attaches :file, with_url_field: true
11
11
 
12
12
  embedded_in :specialist_document_edition
13
13
 
14
14
  validates_with SafeHtml
15
15
 
16
- def url
17
- file.file_url
18
- end
19
-
20
16
  # TODO: Move this to a domain object in specialist publisher
21
17
  def snippet
22
18
  "[InlineAttachment:#{filename}]"
@@ -11,9 +11,23 @@ module Attachable
11
11
 
12
12
  module ClassMethods
13
13
  def attaches(*fields)
14
+ if fields.last.is_a?(Hash)
15
+ options = fields.pop
16
+ else
17
+ options = {}
18
+ end
19
+ attaches_with_options(fields, options)
20
+ end
21
+
22
+ private
23
+
24
+ def attaches_with_options(fields, options = {})
14
25
  fields.map(&:to_s).each do |field|
15
26
  before_save "upload_#{field}".to_sym, :if => "#{field}_has_changed?".to_sym
16
27
  self.field "#{field}_id".to_sym, type: String
28
+ if options[:with_url_field]
29
+ self.field "#{field}_url".to_sym, type: String
30
+ end
17
31
 
18
32
  define_method(field) do
19
33
  raise ApiClientNotPresent unless Attachable.asset_api_client
@@ -42,7 +56,8 @@ module Attachable
42
56
  raise ApiClientNotPresent unless Attachable.asset_api_client
43
57
  begin
44
58
  response = Attachable.asset_api_client.create_asset(:file => instance_variable_get("@#{field}_file"))
45
- self.send("#{field}_id=", response.id.match(/\/([^\/]+)\z/) {|m| m[1] })
59
+ self.send("#{field}_url=", response.file_url) if self.respond_to?("#{field}_url=")
60
+ self.send("#{field}_id=", response.id.split('/').last)
46
61
  rescue StandardError
47
62
  errors.add("#{field}_id".to_sym, "could not be uploaded")
48
63
  end
@@ -1,4 +1,4 @@
1
1
  module GovukContentModels
2
2
  # Changing this causes Jenkins to tag and release the gem into the wild
3
- VERSION = "8.9.0"
3
+ VERSION = "8.10.0"
4
4
  end
@@ -1,19 +1,6 @@
1
1
  require "test_helper"
2
- require "ostruct"
3
- require 'gds_api/test_helpers/asset_manager'
4
2
 
5
3
  class AttachmentTest < ActiveSupport::TestCase
6
- include GdsApi::TestHelpers::AssetManager
7
-
8
- setup do
9
- @original_asset_api_client = Attachable.asset_api_client
10
- Attachable.asset_api_client = stub("asset_api_client")
11
- end
12
-
13
- teardown do
14
- Attachable.asset_api_client = @original_asset_api_client
15
- end
16
-
17
4
  should "generate a snippet" do
18
5
  attachment = Attachment.new(
19
6
  title: "Supporting attachment",
@@ -23,21 +10,4 @@ class AttachmentTest < ActiveSupport::TestCase
23
10
 
24
11
  assert_equal expected_snippet, attachment.snippet
25
12
  end
26
-
27
- should "return the url via #url" do
28
- attachment = Attachment.new(
29
- title: "Photo of me",
30
- filename: "photo.jpg",
31
- file_id: "test-id"
32
- )
33
-
34
- asset_url = stub("asset url")
35
- asset_response = stub("asset response", file_url: asset_url)
36
- Attachable.asset_api_client
37
- .stubs(:asset)
38
- .with(attachment.file_id)
39
- .returns(asset_response)
40
-
41
- assert_equal asset_url, attachment.url
42
- end
43
13
  end
@@ -8,7 +8,7 @@ class SpecialistDocumentEditionTest < ActiveSupport::TestCase
8
8
 
9
9
  setup do
10
10
  @original_asset_api_client = Attachable.asset_api_client
11
- @success_response = stub("asset manager response", id: "/test-id")
11
+ @success_response = stub("asset manager response", id: "/test-id", file_url: "/test-id/document.pdf")
12
12
  Attachable.asset_api_client = stub("asset_api_client")
13
13
  Attachable.asset_api_client.stubs(:create_asset).returns(@success_response)
14
14
  end
@@ -12,10 +12,19 @@ class ModelWithAttachments
12
12
  attaches :image
13
13
  end
14
14
 
15
+ class ModelWithAttachmentsAndUrl
16
+ include Attachable
17
+ include Mongoid::Document
18
+
19
+ field :title, type: String
20
+ attaches :image, with_url_field: true
21
+ end
22
+
15
23
  class AttachableTest < ActiveSupport::TestCase
16
24
 
17
25
  setup do
18
26
  @edition = ModelWithAttachments.new
27
+ @edition_with_url_field = ModelWithAttachmentsAndUrl.new
19
28
  @previous_api_client = Attachable.asset_api_client
20
29
  Attachable.asset_api_client = MockAssetApi.new
21
30
  end
@@ -64,7 +73,10 @@ class AttachableTest < ActiveSupport::TestCase
64
73
  context "saving an edition" do
65
74
  setup do
66
75
  @file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
67
- @asset = OpenStruct.new(:id => 'http://asset-manager.dev.gov.uk/assets/an_image_id')
76
+ @asset = OpenStruct.new(
77
+ id: 'http://asset-manager.dev.gov.uk/assets/an_image_id',
78
+ file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg'
79
+ )
68
80
  end
69
81
 
70
82
  should "upload the asset" do
@@ -88,6 +100,24 @@ class AttachableTest < ActiveSupport::TestCase
88
100
  assert_equal "an_image_id", @edition.image_id
89
101
  end
90
102
 
103
+ should "assign the asset url to the attachment url attribute if requested" do
104
+ MockAssetApi.any_instance.expects(:create_asset).with({ :file => @file }).returns(@asset)
105
+
106
+ @edition_with_url_field.image = @file
107
+ @edition_with_url_field.save!
108
+
109
+ assert_equal 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg', @edition_with_url_field.image_url
110
+ end
111
+
112
+ should "not create the attachment url attribute if not requested" do
113
+ MockAssetApi.any_instance.expects(:create_asset).with({ :file => @file }).returns(@asset)
114
+
115
+ @edition.image = @file
116
+ @edition.save!
117
+
118
+ refute @edition.respond_to?(:image_url)
119
+ end
120
+
91
121
  should "raise an exception if there is no api client present" do
92
122
  Attachable.asset_api_client = nil
93
123
 
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: 8.9.0
4
+ version: 8.10.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-20 00:00:00.000000000 Z
12
+ date: 2014-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -463,7 +463,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
463
463
  version: '0'
464
464
  segments:
465
465
  - 0
466
- hash: 4213322720212660807
466
+ hash: -2794998033139020622
467
467
  required_rubygems_version: !ruby/object:Gem::Requirement
468
468
  none: false
469
469
  requirements:
@@ -472,7 +472,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
472
472
  version: '0'
473
473
  segments:
474
474
  - 0
475
- hash: 4213322720212660807
475
+ hash: -2794998033139020622
476
476
  requirements: []
477
477
  rubyforge_project:
478
478
  rubygems_version: 1.8.23