Skip to content
This repository was archived by the owner on May 28, 2020. It is now read-only.

Commit 8ac0e59

Browse files
author
Mikhail Varabyou
committed
allow separate configuration for open-invoice uploaders.
1 parent 4c76565 commit 8ac0e59

File tree

7 files changed

+67
-52
lines changed

7 files changed

+67
-52
lines changed

Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
open_invoice (0.1.1)
4+
open_invoice (0.1.2)
55
carrierwave-aws
66
jbuilder (~> 2.9)
77
orm_adapter (~> 0.5.0)
@@ -79,13 +79,13 @@ GEM
7979
public_suffix (>= 2.0.2, < 5.0)
8080
ast (2.4.0)
8181
aws-eventstream (1.0.3)
82-
aws-partitions (1.263.0)
82+
aws-partitions (1.267.0)
8383
aws-sdk-core (3.89.1)
8484
aws-eventstream (~> 1.0, >= 1.0.2)
8585
aws-partitions (~> 1, >= 1.239.0)
8686
aws-sigv4 (~> 1.1)
8787
jmespath (~> 1.0)
88-
aws-sdk-kms (1.27.0)
88+
aws-sdk-kms (1.28.0)
8989
aws-sdk-core (~> 3, >= 3.71.0)
9090
aws-sigv4 (~> 1.1)
9191
aws-sdk-s3 (1.60.1)
@@ -125,7 +125,7 @@ GEM
125125
factory_bot_rails (5.1.1)
126126
factory_bot (~> 5.1.0)
127127
railties (>= 4.2.0)
128-
ffi (1.11.3)
128+
ffi (1.12.1)
129129
globalid (0.4.2)
130130
activesupport (>= 4.2.0)
131131
haml (5.1.2)
@@ -308,7 +308,7 @@ GEM
308308
unf (0.1.4)
309309
unf_ext
310310
unf_ext (0.0.7.6)
311-
unicode-display_width (1.6.0)
311+
unicode-display_width (1.6.1)
312312
warden (1.2.8)
313313
rack (>= 2.0.6)
314314
websocket-driver (0.7.1)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# Author: varaby_m@modulotech.fr
4+
module OpenInvoice
5+
6+
class BaseUploader < CarrierWave::Uploader::Base
7+
8+
# Choose what kind of storage to use for this uploader:
9+
storage OpenInvoice.config.storage
10+
cache_storage :file
11+
12+
def initialize(*)
13+
super
14+
15+
self.aws_credentials = { access_key_id: OpenInvoice.config.aws_key_id,
16+
secret_access_key: OpenInvoice.config.aws_secret,
17+
region: OpenInvoice.config.aws_region,
18+
stub_responses: Rails.env.test? }
19+
self.aws_bucket = OpenInvoice.config.aws_bucket
20+
self.aws_acl = 'private'
21+
self.aws_attributes = {}
22+
self.aws_authenticated_url_expiration = 5.minutes.to_i
23+
end
24+
25+
# Override the directory where uploaded files will be stored.
26+
def store_dir
27+
File.join(OpenInvoice.config.dir_prefix,
28+
model.class.to_s.underscore,
29+
mounted_as.to_s,
30+
model&.id.to_s)
31+
end
32+
33+
end
34+
35+
end

app/uploaders/open_invoice/original_file_uploader.rb

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,7 @@ module OpenInvoice
44

55
# Author: varaby_m@modulotech.fr
66
# uploader for invoice's original file
7-
class OriginalFileUploader < CarrierWave::Uploader::Base
8-
9-
# Choose what kind of storage to use for this uploader:
10-
storage OpenInvoice.config.storage
11-
12-
cache_storage :file
13-
14-
# Override the directory where uploaded files will be stored.
15-
def store_dir
16-
File.join(OpenInvoice.config.dir_prefix,
17-
model.class.to_s.underscore,
18-
mounted_as.to_s,
19-
model&.id.to_s)
20-
end
21-
22-
# Provide a default URL as a default if there hasn't been a file uploaded:
23-
# def default_url(*args)
24-
# # For Rails 3.1+ asset pipeline compatibility:
25-
# # ActionController::Base.helpers.asset_path("fallback/" +
26-
# [version_name, "default.png"].compact.join('_'))
27-
#
28-
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
29-
# end
7+
class OriginalFileUploader < BaseUploader
308

319
# Add a white list of extensions which are allowed to be uploaded.
3210
def extension_whitelist

lib/open_invoice/carrier_wave_configure.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/open_invoice/configuration.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ def configure
3636
require "open_invoice/orm/#{config.orm}"
3737
# load application record for engine's models
3838
require_relative 'application_record'
39-
# load carrierwave configuration
40-
require_relative 'carrier_wave_configure'
4139
# load application mailer for engine's mailers
4240
require_relative 'application_mailer'
41+
# load carrierwave uploaders
42+
require 'open_invoice/base_uploader'
43+
require 'open_invoice/original_file_uploader'
4344
end
4445

4546
# shorthand for app_name

lib/open_invoice/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module OpenInvoice
44

5-
VERSION = '0.1.1'
5+
VERSION = '0.1.2'
66

77
end

spec/rails_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@
3333
RSpec.configure do |config|
3434
config.before(:all) do
3535
DatabaseCleaner.clean_with :truncation
36+
CarrierWave.configure do |cw|
37+
cw.storage = :file
38+
cw.enable_processing = false
39+
end
40+
OpenInvoice::BaseUploader.prepend(Module.new do
41+
extend ActiveSupport::Concern
42+
43+
included do
44+
storage :file
45+
cache_storage :file
46+
end
47+
48+
def store_dir
49+
Rails.root.join('tmp/uploads')
50+
end
51+
52+
alias_method :cache_dir, :store_dir
53+
end)
54+
end
55+
56+
config.after(:all) do
57+
FileUtils.rm_rf(Rails.root.join('tmp/uploads'))
3658
end
3759

3860
config.around(:each) do |example|

0 commit comments

Comments
 (0)