Skip to content

Commit

Permalink
ActiveVault -> ActiveStorage
Browse files Browse the repository at this point in the history
Yaroslav agreed to hand over the gem name ❤️
  • Loading branch information
dhh committed Jul 6, 2017
1 parent 5869045 commit c624df3
Show file tree
Hide file tree
Showing 38 changed files with 173 additions and 180 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
activevault (0.1)
activestorage (0.1)
actionpack (>= 5.1)
activejob (>= 5.1)
activerecord (>= 5.1)
Expand Down Expand Up @@ -223,7 +223,7 @@ PLATFORMS
ruby

DEPENDENCIES
activevault!
activestorage!
aws-sdk
bundler (~> 1.15)
byebug
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -19,7 +19,7 @@ user.avatar.exist? # => true
user.avatar.purge
user.avatar.exist? # => false

user.image.url(expires_in: 5.minutes) # => /rails/blobs/<encoded-key>
user.avatar.url(expires_in: 5.minutes) # => /rails/blobs/<encoded-key>

class AvatarsController < ApplicationController
def update
Expand Down Expand Up @@ -55,7 +55,7 @@ end

## Configuration

Add `require "active_vault"` to config/application.rb and create a `config/initializers/active_vault_sites.rb` with the following:
Add `require "active_storage"` to config/application.rb and create a `config/initializers/active_storage_sites.rb` with the following:

```ruby

Expand Down
4 changes: 2 additions & 2 deletions activevault.gemspec → activestorage.gemspec
@@ -1,10 +1,10 @@
Gem::Specification.new do |s|
s.name = "activevault"
s.name = "activestorage"
s.version = "0.1"
s.authors = "David Heinemeier Hansson"
s.email = "david@basecamp.com"
s.summary = "Store files in Rails applications"
s.homepage = "https://github.com/rails/activevault"
s.homepage = "https://github.com/rails/activestorage"
s.license = "MIT"

s.required_ruby_version = ">= 1.9.3"
Expand Down
4 changes: 2 additions & 2 deletions lib/active_vault.rb → lib/active_storage.rb
@@ -1,7 +1,7 @@
require "active_record"
require "active_vault/railtie" if defined?(Rails)
require "active_storage/railtie" if defined?(Rails)

module ActiveVault
module ActiveStorage
extend ActiveSupport::Autoload

autoload :Blob
Expand Down
18 changes: 9 additions & 9 deletions lib/active_vault/attached.rb → lib/active_storage/attached.rb
@@ -1,10 +1,10 @@
require "active_vault/blob"
require "active_vault/attachment"
require "active_storage/blob"
require "active_storage/attachment"

require "action_dispatch/http/upload"
require "active_support/core_ext/module/delegation"

class ActiveVault::Attached
class ActiveStorage::Attached
attr_reader :name, :record

def initialize(name, record)
Expand All @@ -14,21 +14,21 @@ def initialize(name, record)
private
def create_blob_from(attachable)
case attachable
when ActiveVault::Blob
when ActiveStorage::Blob
attachable
when ActionDispatch::Http::UploadedFile
ActiveVault::Blob.create_after_upload! \
ActiveStorage::Blob.create_after_upload! \
io: attachable.open,
filename: attachable.original_filename,
content_type: attachable.content_type
when Hash
ActiveVault::Blob.create_after_upload!(attachable)
ActiveStorage::Blob.create_after_upload!(attachable)
else
nil
end
end
end

require "active_vault/attached/one"
require "active_vault/attached/many"
require "active_vault/attached/macros"
require "active_storage/attached/one"
require "active_storage/attached/many"
require "active_storage/attached/macros"
23 changes: 23 additions & 0 deletions lib/active_storage/attached/macros.rb
@@ -0,0 +1,23 @@
module ActiveStorage::Attached::Macros
def has_one_attached(name, dependent: :purge_later)
define_method(name) do
instance_variable_get("@active_storage_attached_#{name}") ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
end

if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
end

def has_many_attached(name, dependent: :purge_later)
define_method(name) do
instance_variable_get("@active_storage_attached_#{name}") ||
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
end

if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
end
end
@@ -1,13 +1,13 @@
class ActiveVault::Attached::Many < ActiveVault::Attached
class ActiveStorage::Attached::Many < ActiveStorage::Attached
delegate_missing_to :attachments

def attachments
@attachments ||= ActiveVault::Attachment.where(record_gid: record.to_gid.to_s, name: name)
@attachments ||= ActiveStorage::Attachment.where(record_gid: record.to_gid.to_s, name: name)
end

def attach(*attachables)
@attachments = attachments | Array(attachables).flatten.collect do |attachable|
ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
end
end

Expand Down
@@ -1,12 +1,12 @@
class ActiveVault::Attached::One < ActiveVault::Attached
class ActiveStorage::Attached::One < ActiveStorage::Attached
delegate_missing_to :attachment

def attachment
@attachment ||= ActiveVault::Attachment.find_by(record_gid: record.to_gid.to_s, name: name)
@attachment ||= ActiveStorage::Attachment.find_by(record_gid: record.to_gid.to_s, name: name)
end

def attach(attachable)
@attachment = ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
@attachment = ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
end

def attached?
Expand Down
@@ -1,12 +1,12 @@
require "active_vault/blob"
require "active_storage/blob"
require "global_id"
require "active_support/core_ext/module/delegation"

# Schema: id, record_gid, blob_id, created_at
class ActiveVault::Attachment < ActiveRecord::Base
self.table_name = "active_vault_attachments"
class ActiveStorage::Attachment < ActiveRecord::Base
self.table_name = "active_storage_attachments"

belongs_to :blob, class_name: "ActiveVault::Blob"
belongs_to :blob, class_name: "ActiveStorage::Blob"

delegate_missing_to :blob

Expand All @@ -25,6 +25,6 @@ def purge
end

def purge_later
ActiveVault::PurgeJob.perform_later(self)
ActiveStorage::PurgeJob.perform_later(self)
end
end
14 changes: 7 additions & 7 deletions lib/active_vault/blob.rb → lib/active_storage/blob.rb
@@ -1,10 +1,10 @@
require "active_vault/site"
require "active_vault/filename"
require "active_vault/purge_job"
require "active_storage/site"
require "active_storage/filename"
require "active_storage/purge_job"

# Schema: id, key, filename, content_type, metadata, byte_size, checksum, created_at
class ActiveVault::Blob < ActiveRecord::Base
self.table_name = "active_vault_blobs"
class ActiveStorage::Blob < ActiveRecord::Base
self.table_name = "active_storage_blobs"

has_secure_token :key
store :metadata, coder: JSON
Expand Down Expand Up @@ -33,7 +33,7 @@ def key
end

def filename
ActiveVault::Filename.new(self[:filename])
ActiveStorage::Filename.new(self[:filename])
end

def url(expires_in: 5.minutes, disposition: :inline)
Expand Down Expand Up @@ -63,6 +63,6 @@ def purge
end

def purge_later
ActiveVault::PurgeJob.perform_later(self)
ActiveStorage::PurgeJob.perform_later(self)
end
end
@@ -1,13 +1,13 @@
# Configuration should be something like this:
#
# config/environments/development.rb
# config.active_vault.site = :local
# config.active_storage.site = :local
#
# config/environments/production.rb
# config.active_vault.site = :amazon
# config.active_storage.site = :amazon
local:
site: Disk
root: <%%= File.join(Dir.tmpdir, "active_vault") %>
root: <%%= File.join(Dir.tmpdir, "active_storage") %>

amazon:
site: S3
Expand Down
@@ -1,13 +1,13 @@
require "action_controller"
require "active_vault/blob"
require "active_vault/verified_key_with_expiration"
require "active_storage/blob"
require "active_storage/verified_key_with_expiration"

require "active_support/core_ext/object/inclusion"

class ActiveVault::DiskController < ActionController::Base
class ActiveStorage::DiskController < ActionController::Base
def show
if key = decode_verified_key
blob = ActiveVault::Blob.find_by!(key: key)
blob = ActiveStorage::Blob.find_by!(key: key)

if stale?(etag: blob.checksum)
send_data blob.download, filename: blob.filename, type: blob.content_type, disposition: disposition_param
Expand All @@ -19,7 +19,7 @@ def show

private
def decode_verified_key
ActiveVault::VerifiedKeyWithExpiration.decode(params[:encoded_key])
ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_key])
end

def disposition_param
Expand Down
@@ -1,4 +1,4 @@
class ActiveVault::Download
class ActiveStorage::Download
# Sending .ai files as application/postscript to Safari opens them in a blank, grey screen.
# Downloading .ai as application/postscript files in Safari appends .ps to the extension.
# Sending HTML, SVG, XML and SWF files as binary closes XSS vulnerabilities.
Expand Down
@@ -1,4 +1,4 @@
class ActiveVault::Filename
class ActiveStorage::Filename
include Comparable

def initialize(filename)
Expand Down
@@ -1,6 +1,6 @@
class ActiveVault::CreateTables < ActiveRecord::Migration[5.1]
class ActiveStorage::CreateTables < ActiveRecord::Migration[5.1]
def change
create_table :active_vault_blobs do |t|
create_table :active_storage_blobs do |t|
t.string :key
t.string :filename
t.string :content_type
Expand All @@ -12,7 +12,7 @@ def change
t.index [ :key ], unique: true
end

create_table :active_vault_attachments do |t|
create_table :active_storage_attachments do |t|
t.string :name
t.string :record_gid
t.integer :blob_id
Expand Down
@@ -1,7 +1,7 @@
require "active_job"

class ActiveVault::PurgeJob < ActiveJob::Base
# FIXME: Limit this to a custom ActiveVault error
class ActiveStorage::PurgeJob < ActiveJob::Base
# FIXME: Limit this to a custom ActiveStorage error
retry_on StandardError

def perform(attachment_or_blob)
Expand Down
27 changes: 27 additions & 0 deletions lib/active_storage/railtie.rb
@@ -0,0 +1,27 @@
require "rails/railtie"

module ActiveStorage
class Railtie < Rails::Railtie # :nodoc:
config.active_storage = ActiveSupport::OrderedOptions.new

config.eager_load_namespaces << ActiveStorage

initializer "active_storage.routes" do
require "active_storage/disk_controller"

config.after_initialize do |app|
app.routes.prepend do
get "/rails/blobs/:encoded_key" => "active_storage/disk#show", as: :rails_disk_blob
end
end
end

initializer "active_storage.attached" do
require "active_storage/attached"

ActiveSupport.on_load(:active_record) do
extend ActiveStorage::Attached::Macros
end
end
end
end
6 changes: 3 additions & 3 deletions lib/active_vault/site.rb → lib/active_storage/site.rb
@@ -1,9 +1,9 @@
# Abstract class serving as an interface for concrete sites.
class ActiveVault::Site
class ActiveStorage::Site
def self.configure(site, **options)
begin
require "active_vault/site/#{site.to_s.downcase}_site"
ActiveVault::Site.const_get(:"#{site}Site").new(**options)
require "active_storage/site/#{site.to_s.downcase}_site"
ActiveStorage::Site.const_get(:"#{site}Site").new(**options)
rescue LoadError => e
puts "Couldn't configure site: #{site} (#{e.message})"
end
Expand Down
@@ -1,7 +1,7 @@
require "fileutils"
require "pathname"

class ActiveVault::Site::DiskSite < ActiveVault::Site
class ActiveStorage::Site::DiskSite < ActiveStorage::Site
attr_reader :root

def initialize(root:)
Expand Down Expand Up @@ -38,7 +38,7 @@ def exist?(key)


def url(key, expires_in:, disposition:, filename:)
verified_key_with_expiration = ActiveVault::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in)
verified_key_with_expiration = ActiveStorage::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in)

if defined?(Rails) && defined?(Rails.application)
Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition)
Expand Down
@@ -1,7 +1,7 @@
require "google/cloud/storage"
require "active_support/core_ext/object/to_query"

class ActiveVault::Site::GCSSite < ActiveVault::Site
class ActiveStorage::Site::GCSSite < ActiveStorage::Site
attr_reader :client, :bucket

def initialize(project:, keyfile:, bucket:)
Expand Down
@@ -1,4 +1,4 @@
class ActiveVault::Site::MirrorSite < ActiveVault::Site
class ActiveStorage::Site::MirrorSite < ActiveStorage::Site
attr_reader :sites

def initialize(sites:)
Expand Down
@@ -1,6 +1,6 @@
require "aws-sdk"

class ActiveVault::Site::S3Site < ActiveVault::Site
class ActiveStorage::Site::S3Site < ActiveStorage::Site
attr_reader :client, :bucket

def initialize(access_key_id:, secret_access_key:, region:, bucket:)
Expand Down
@@ -1,5 +1,5 @@
class ActiveVault::VerifiedKeyWithExpiration
class_attribute :verifier, default: defined?(Rails) ? Rails.application.message_verifier('ActiveVault') : nil
class ActiveStorage::VerifiedKeyWithExpiration
class_attribute :verifier, default: defined?(Rails) ? Rails.application.message_verifier('ActiveStorage') : nil

class << self
def encode(key, expires_in: nil)
Expand Down

0 comments on commit c624df3

Please sign in to comment.