expiring_asset_links 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,27 +6,84 @@ Handles storing and generating CarrierWave FOG expiring asset link stored in str
6
6
 
7
7
  This is helpful when using a WYSIWYG that inserts and previews assets directly from S3 using full S3 URLs. These URLs need to be updated each time the object is reloaded. When used in conjunction with CarrierWave and FOG gems, this gem will automatically update these links for you.
8
8
 
9
+ ## Dependencies
10
+
11
+ the following gems are required
12
+
13
+ gem 'rails', '>= 4.0.0'
14
+ gem 'carrierwave'
15
+ gem 'fog'
16
+
9
17
  ## Installation
10
18
 
19
+ install manually
20
+
11
21
  gem install expiring_asset_links
12
22
 
23
+ or add it to your Gemfile
24
+
25
+ gem 'carrierwave'
26
+
13
27
  ## Usage
14
28
 
15
- ### name your uploader mount `:asset`
29
+ ### configure CarrierWave
30
+
31
+ your configuration must satisfy the following requirements:
32
+
33
+ * specify `storage :fog`
34
+ * use the default `store_dir` string or one that ends with `folder/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}`
35
+
36
+ any of the following will work
37
+
38
+ <pre>
39
+ uploads/#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
40
+ uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
41
+ #{Rails.env}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
42
+ #{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
43
+ a/series/of/folder/names/or/1/numbers/2/that_do/not/contain/spaces/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}
44
+ </pre>
45
+
46
+ * `s3_headers` must include a value for `Expires`
47
+
48
+ for example
49
+
50
+ class AssetUploader < CarrierWave::Uploader::Base
51
+ storage :fog
52
+
53
+ def s3_headers
54
+ { "Expires" => 5.minutes.from_how.httpdate }
55
+ end
56
+
57
+ def store_dir
58
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
59
+ end
60
+
61
+ end
16
62
 
17
- class Images
18
- mount_uploader :asset, ImageUploader
19
-
63
+ ### mount an uploader for any class containing an assets
64
+
65
+ you may use as many different classes as you would like, but each may only contain one mounted uploader
66
+
67
+ class Image
68
+ mount_uploader :image, ImageUploader
69
+
70
+ end
71
+
72
+ class Attachment
73
+ mount_uploader :file, FileUploader
74
+
20
75
  end
21
76
 
22
- ### specify the fields that will contain :asset links
77
+ ### specify the attributes that will contain asset links
23
78
 
24
- class Document
79
+ the attributes specified should contain fully qualified asset URLs generated by CarrierWave with the `Expires` value specified when saved, these URLs will be automatically updated each time the attribute is accessed
80
+
81
+ class Document < ActiveRecord::Base
25
82
  attr_expiring_asset_links :body
26
-
83
+
27
84
  end
28
85
 
29
- class Article
86
+ class Article < ActiveRecord::Base
30
87
  attr_expiring_asset_links :summary, :body
31
-
88
+
32
89
  end
@@ -27,6 +27,6 @@ Gem::Specification.new do |s|
27
27
 
28
28
  s.add_dependency('carrierwave')
29
29
  s.add_dependency('fog')
30
- s.add_dependency('rails')
30
+ s.add_dependency('rails', ['>= 4.0.0'])
31
31
  s.add_development_dependency('sqlite3')
32
32
  end
@@ -2,8 +2,8 @@ module ExpiringAssetLinks
2
2
  # Contains information about this gem's version
3
3
  module Version
4
4
  MAJOR = 1
5
- MINOR = 1
6
- PATCH = 3
5
+ MINOR = 2
6
+ PATCH = 0
7
7
 
8
8
  # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
9
9
  #
@@ -37,7 +37,8 @@ module ExpiringAssetLinks
37
37
  end
38
38
 
39
39
  def add_asset_tags(attribute)
40
- self[attribute.to_sym].gsub(/([A-Za-z]+)\{\{(\d+)\}\}/) { $1.constantize.find($2).asset.url(:default) }
40
+ linked_attribute =
41
+ self[attribute.to_sym].gsub(/([A-Za-z]+)\{\{(\d+)\}\}/) { $1.constantize.find($2).send($1.constantize.uploaders.keys.first).url(:default) }
41
42
  end
42
43
 
43
44
  def remove_all_asset_tags!
@@ -21,7 +21,7 @@ class ExpiringAssetLinksTest < Test::Unit::TestCase
21
21
 
22
22
  def test_should_assert_true
23
23
  file_attachment = FileAttachment.create(name: "test", asset: AssetUploader.new("https://test.s3-us-east-1.amazonaws.com/uploads/test/file_attachment/asset/1/sample.jpg"))
24
- document = Document.create(title: "This is the Document Title", body: "<h2>Section One</h2><p>This is the first section in the body of the document. It includes an image.</p><img src=\"#{file_attachment.asset.url}\">")
24
+ document = Document.create(title: "This is the Document Title", body: "<h2>Section One</h2><p>This is the first section in the body of the document. It includes an image.</p><img src=\"#{file_attachment.send(FileAttachment.uploaders.keys.first).url}\">")
25
25
 
26
26
  assert_equal "<h2>Section One</h2><p>This is the first section in the body of the document. It includes an image.</p><img src=\"FileAttachment{{1}}\">", Document.find(document.id).attributes["body"]
27
27
  assert_equal "<h2>Section One</h2><p>This is the first section in the body of the document. It includes an image.</p><img src=\"https://test.s3-us-east-1.amazonaws.com/uploads/test/file_attachment/asset/1/sample.jpg?AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&amp;Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX%3D&amp;Expires=2222222222\">", Document.find(document.id).body
data/test/test_helper.rb CHANGED
@@ -8,7 +8,6 @@ require 'carrierwave'
8
8
  require 'fog'
9
9
  require 'test/unit'
10
10
 
11
-
12
11
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
13
12
 
14
13
  ActiveRecord::Schema.define(:version => 1) do
@@ -29,16 +28,31 @@ ActiveRecord::Schema.define(:version => 1) do
29
28
  end
30
29
  end
31
30
 
31
+ module HasAssets
32
+ module ClassMethods
33
+ def serialize_asset
34
+ serialize :asset
35
+ end
36
+ def uploaders
37
+ { asset: AssetUploader }
38
+ end
39
+ end
40
+
41
+ def self.included(base)
42
+ base.extend(ClassMethods).serialize_asset
43
+ end
44
+ end
45
+
32
46
  class Document < ActiveRecord::Base
33
47
  attr_expiring_asset_links :body
34
48
  end
35
49
 
36
50
  class FileAttachment < ActiveRecord::Base
37
- serialize :asset
51
+ include HasAssets
38
52
  end
39
53
 
40
54
  class ImageAttachment < ActiveRecord::Base
41
- serialize :asset
55
+ include HasAssets
42
56
  end
43
57
 
44
58
  class AssetUploader < Struct.new(:my_url)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expiring_asset_links
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.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: 2013-09-11 00:00:00.000000000 Z
12
+ date: 2013-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carrierwave
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 4.0.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: '0'
61
+ version: 4.0.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: sqlite3
64
64
  requirement: !ruby/object:Gem::Requirement