expiring_asset_links 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/.rvmrc +47 -0
- data/MIT-LICENSE +20 -0
- data/README.md +21 -0
- data/Rakefile +22 -0
- data/expiring_asset_links.gemspec +31 -0
- data/lib/expiring_asset_links/version.rb +17 -0
- data/lib/expiring_asset_links.rb +71 -0
- data/test/expiring_asset_links_test.rb +32 -0
- data/test/test_helper.rb +36 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
+
environment_id="ruby-1.9.2-p290@attr_encrypted"
|
8
|
+
|
9
|
+
#
|
10
|
+
# Uncomment following line if you want options to be set only for given project.
|
11
|
+
#
|
12
|
+
# PROJECT_JRUBY_OPTS=( --1.9 )
|
13
|
+
|
14
|
+
#
|
15
|
+
# First we attempt to load the desired environment directly from the environment
|
16
|
+
# file. This is very fast and efficient compared to running through the entire
|
17
|
+
# CLI and selector. If you want feedback on which environment was used then
|
18
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
19
|
+
#
|
20
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
21
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
22
|
+
then
|
23
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
24
|
+
|
25
|
+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
26
|
+
then
|
27
|
+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
28
|
+
fi
|
29
|
+
else
|
30
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
31
|
+
if ! rvm --create "$environment_id"
|
32
|
+
then
|
33
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
34
|
+
exit 1
|
35
|
+
fi
|
36
|
+
fi
|
37
|
+
|
38
|
+
#
|
39
|
+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
40
|
+
# it be automatically loaded. Uncomment the following and adjust the filename if
|
41
|
+
# necessary.
|
42
|
+
#
|
43
|
+
# filename=".gems"
|
44
|
+
# if [[ -s "$filename" ]] ; then
|
45
|
+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
46
|
+
# fi
|
47
|
+
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 chaunce - chaunce.slc@gmail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# attr_encryptor
|
2
|
+
|
3
|
+
CarrierWave FOG Expiring Asset Links
|
4
|
+
|
5
|
+
Handles storing and generating CarrierWave FOG expiring asset link stored in string and text attributes of ActiveRecord objects
|
6
|
+
|
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
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
gem install expiring_asset_links
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
### Your model
|
16
|
+
|
17
|
+
class Document
|
18
|
+
attr_expiring_asset_links :body
|
19
|
+
|
20
|
+
end
|
21
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the expiring_asset_links gem.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the expiring_asset_links gem.'
|
16
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'expiring_asset_links'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path('../lib/', __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
|
4
|
+
require 'expiring_asset_links/version'
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'expiring_asset_links'
|
9
|
+
s.version = ExpiringAssetLinks::Version.string
|
10
|
+
s.date = Date.today
|
11
|
+
|
12
|
+
s.summary = 'CarrierWave FOG Expiring Asset Links'
|
13
|
+
s.description = 'Handles storing and generating CarrierWave FOG expiring asset link stored in string and text attributes of ActiveRecord objects'
|
14
|
+
|
15
|
+
s.author = 'chaunce'
|
16
|
+
s.email = 'chaunce.slc@gmail.com'
|
17
|
+
s.homepage = 'http://github.com/chaunce/expiring_asset_links'
|
18
|
+
|
19
|
+
s.has_rdoc = false
|
20
|
+
s.rdoc_options = ['--line-numbers', '--inline-source', '--main', 'README.rdoc']
|
21
|
+
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
+
|
27
|
+
s.add_dependency('carrierwave')
|
28
|
+
s.add_dependency('fog')
|
29
|
+
s.add_dependency('rails', ['>= 3.0.0'])
|
30
|
+
s.add_development_dependency('sqlite3')
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ExpiringAssetLinks
|
2
|
+
# Contains information about this gem's version
|
3
|
+
module Version
|
4
|
+
MAJOR = 1
|
5
|
+
MINOR = 0
|
6
|
+
PATCH = 0
|
7
|
+
|
8
|
+
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
|
9
|
+
#
|
10
|
+
# Example
|
11
|
+
#
|
12
|
+
# Version.string # '1.0.2'
|
13
|
+
def self.string
|
14
|
+
[MAJOR, MINOR, PATCH].join('.')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module ExpiringAssetLinks
|
4
|
+
autoload :Version, 'expiring_asset_links/version'
|
5
|
+
|
6
|
+
def self.extended(base)
|
7
|
+
base.class_eval do
|
8
|
+
@expiring_asset_link_attributes = []
|
9
|
+
include InstanceMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def attr_expiring_asset_links(*attributes)
|
14
|
+
before_save :remove_all_asset_tags!
|
15
|
+
attributes.each do |attribute|
|
16
|
+
define_method(attribute) do
|
17
|
+
add_asset_tags(attribute.to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
expiring_asset_link_attributes << attribute.to_sym
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def attr_expiring_asset_links?(attribute)
|
25
|
+
expiring_asset_link_attributes.include?(attribute.to_sym)
|
26
|
+
end
|
27
|
+
|
28
|
+
def expiring_asset_link_attributes
|
29
|
+
@expiring_asset_link_attributes ||= superclass.expiring_asset_link_attributes.dup
|
30
|
+
end
|
31
|
+
|
32
|
+
module InstanceMethods
|
33
|
+
protected
|
34
|
+
|
35
|
+
def remove_asset_tags(attribute)
|
36
|
+
self[attribute.to_sym].gsub(/https:\/\/#{CarrierWave::Uploader::Base.fog_directory}\.s3.\S+\/([a-z_]+)\/[a-z_]+\/(\d+)\/\S+Expires=[\d]{10}/) { "#{$1.classify}{{\\1}}" }
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_asset_tags(attribute)
|
40
|
+
self[attribute.to_sym].gsub(/([A-Za-z]+)\{\{(\d+)\}\}/) { $1.constantize.find($2).asset.url(:default) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def remove_all_asset_tags!
|
44
|
+
self.class.expiring_asset_link_attributes.each{ |attribute| self.send(:"#{attribute}=", remove_asset_tags(attribute.to_sym)) }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module AttrExpiringAssetLinks
|
51
|
+
module ActiveRecord
|
52
|
+
def self.extended(base) # :nodoc:
|
53
|
+
base.class_eval do
|
54
|
+
class << self
|
55
|
+
alias_method_chain :attr_expiring_asset_links, :defined_attributes
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def attr_expiring_asset_links_with_defined_attributes(*attrs)
|
63
|
+
define_attribute_methods rescue nil
|
64
|
+
attr_expiring_asset_links_without_defined_attributes *attrs
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
Object.extend ExpiringAssetLinks
|
71
|
+
ActiveRecord::Base.extend AttrExpiringAssetLinks::ActiveRecord
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class ExpiringAssetLinksTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
CarrierWave.configure do |config|
|
7
|
+
# config.cache_dir = "#{Rails.root}/tmp/assets"
|
8
|
+
#
|
9
|
+
# config.root = Rails.root.join('tmp')
|
10
|
+
# config.cache_dir = "assets"
|
11
|
+
# config.fog_credentials = {
|
12
|
+
# :provider => 'AWS',
|
13
|
+
# :aws_access_key_id => ENV['AWS_KEY'],
|
14
|
+
# :aws_secret_access_key => ENV['S3_SECRET'],
|
15
|
+
# :region => 'us-west-1'
|
16
|
+
# }
|
17
|
+
config.fog_directory = "test"
|
18
|
+
# config.fog_public = false
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_assert_true
|
25
|
+
document = Document.new(: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=\"https://test.s3-us-east-1.amazonaws.com/uploads/test/file_attachment/asset/1/sample.jpg?AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX%3D&Expires=2222222222\">")
|
26
|
+
document.save!
|
27
|
+
# if we had a actual FileAttachment object it would connect to AWS to get a real URL
|
28
|
+
# to make this work in testing we need to stub CarrierWave
|
29
|
+
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.first.body
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$:.unshift(File.dirname(__FILE__))
|
3
|
+
require 'expiring_asset_links'
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'carrierwave'
|
8
|
+
require 'fog'
|
9
|
+
require 'test/unit'
|
10
|
+
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(:version => 1) do
|
15
|
+
create_table :documents do |t|
|
16
|
+
t.integer :id
|
17
|
+
t.string :title
|
18
|
+
t.text :body
|
19
|
+
end
|
20
|
+
create_table :file_attachments do |t|
|
21
|
+
t.integer :id
|
22
|
+
t.integer :url
|
23
|
+
end
|
24
|
+
create_table :image_attachments do |t|
|
25
|
+
t.integer :id
|
26
|
+
t.integer :url
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Document < ActiveRecord::Base
|
31
|
+
attr_expiring_asset_links :body
|
32
|
+
end
|
33
|
+
class FileAttachment < ActiveRecord::Base
|
34
|
+
end
|
35
|
+
class ImageAttachment < ActiveRecord::Base
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: expiring_asset_links
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- chaunce
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: carrierwave
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: fog
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.0.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sqlite3
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Handles storing and generating CarrierWave FOG expiring asset link stored
|
79
|
+
in string and text attributes of ActiveRecord objects
|
80
|
+
email: chaunce.slc@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rvmrc
|
87
|
+
- MIT-LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- expiring_asset_links.gemspec
|
91
|
+
- lib/expiring_asset_links.rb
|
92
|
+
- lib/expiring_asset_links/version.rb
|
93
|
+
- test/expiring_asset_links_test.rb
|
94
|
+
- test/test_helper.rb
|
95
|
+
homepage: http://github.com/chaunce/expiring_asset_links
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options:
|
99
|
+
- --line-numbers
|
100
|
+
- --inline-source
|
101
|
+
- --main
|
102
|
+
- README.rdoc
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.8.25
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: CarrierWave FOG Expiring Asset Links
|
123
|
+
test_files:
|
124
|
+
- test/expiring_asset_links_test.rb
|
125
|
+
- test/test_helper.rb
|