adds zip_export utility first run

This commit is contained in:
zmagod 2017-03-21 16:15:11 +01:00
parent 38299d9f67
commit 4385c68f5f
9 changed files with 110 additions and 1 deletions

View file

@ -0,0 +1,21 @@
class ZipExportsController < ApplicationController
before_action :load_var
before_action :check_edit_permissions
def download
send_data @zip_export.zip_file,
filename: @zip_export.file_file_name + '.zip',
type: @zip_export.file_content_type
end
private
def load_var
@zip_export = ZipExport.find_by_id(params[:id])
render_404 unless @zip_export
end
def check_edit_permissions
render_403 unless @zip_export.user == current_user
end
end

6
app/models/zip_export.rb Normal file
View file

@ -0,0 +1,6 @@
class ZipExport < ActiveRecord::Base
belongs_to :user
has_attached_file :zip_file
validates_attachment :zip_file,
content_type: { content_type: 'application/zip' }
end

View file

@ -0,0 +1,36 @@
require 'zip'
require 'tmpdir'
class DelayedExporter
include Rails.application.routes.url_helpers
def initialize(options = {})
@user = options.fetch(:user) { :user_must_be_present }
@zip_export = ZipExport.new(user: @user)
end
def run(&collection)
Delayed::Job.enqueue(setup(&collection))
end
def setup
temp_dir = Dir.mktempdir
zip_path = File.join(temp_dir, 'export.zip')
Zip::ZipFile.open(zip_path, true) do |zipfile|
yield(zipfile) if block_given?
end
@zip_export.zip_file = File.open(zip_path)
generate_notification if @zip_export.save
end
private
def generate_notification
notification = Notification.create(
type_of: :system_message,
title: t('zip_export.notification_title'),
message: zip_exports_download_url(@zip_export)
)
UserNotification.create(notification: notification, user: @user.id)
end
end

View file

@ -1542,6 +1542,8 @@ en:
deleted: "(deleted)"
popover_html: "<span class='silver'>Team:</span>&nbsp;%{team} <br> <span class='silver'>Role:</span>&nbsp;%{role} <br> <span class='silver'>Joined:</span>&nbsp;%{time}"
zip_export:
notification_title: 'Your package is ready to be exported!'
# This section contains general words that can be used in any parts of
# application.

View file

@ -113,6 +113,11 @@ Rails.application.routes.draw do
to: 'user_notifications#index',
as: 'notifications'
# Get Zip Export
get 'zip_exports/download/:id',
to: 'zip_exports#download',
as: 'zip_exports_download'
resources :teams do
resources :samples, only: [:new, :create]
resources :sample_types, except: [:show, :new] do

View file

@ -0,0 +1,10 @@
class CreateZipExports < ActiveRecord::Migration
def change
create_table :zip_exports do |t|
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
add_attachment :zip_exports, :zip_file
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170306121855) do
ActiveRecord::Schema.define(version: 20170321131116) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -700,6 +700,18 @@ ActiveRecord::Schema.define(version: 20170306121855) do
t.string "proof_key_old_exp", null: false
end
create_table "zip_exports", force: :cascade do |t|
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "zip_file_file_name"
t.string "zip_file_content_type"
t.integer "zip_file_file_size"
t.datetime "zip_file_updated_at"
end
add_index "zip_exports", ["user_id"], name: "index_zip_exports_on_user_id", using: :btree
add_foreign_key "activities", "my_modules"
add_foreign_key "activities", "projects"
add_foreign_key "activities", "users"
@ -815,4 +827,5 @@ ActiveRecord::Schema.define(version: 20170306121855) do
add_foreign_key "users", "teams", column: "current_team_id"
add_foreign_key "wopi_actions", "wopi_apps"
add_foreign_key "wopi_apps", "wopi_discoveries"
add_foreign_key "zip_exports", "users"
end

9
test/fixtures/zip_exports.yml vendored Normal file
View file

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id:
zip_file:
two:
user_id:
zip_file:

View file

@ -0,0 +1,7 @@
require 'test_helper'
class ZipExportTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end