-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from platanus/unify-storage-options
Unify storage options
- Loading branch information
Showing
8 changed files
with
154 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class Recipes::FileStorage < Rails::AppBuilder | ||
def ask | ||
storages = { | ||
active_storage: 'ActiveStorage', | ||
paperclip: '[DEPRECATED] Paperclip', | ||
none: 'None, thanks' | ||
} | ||
|
||
storage = answer(:storage) do | ||
storages.keys[Ask.list('Which storage are you going to use?', storages.values)] | ||
end | ||
|
||
set(:storage, storage.to_sym) | ||
end | ||
|
||
def create | ||
add_chosen_storage(check_rspec: false) | ||
end | ||
|
||
def install | ||
ask | ||
add_chosen_storage(check_rspec: true) | ||
end | ||
|
||
def installed? | ||
gem_exists?(/paperclip/) || file_exist?('config/storage.yml') | ||
end | ||
|
||
private | ||
|
||
def paperclip_config | ||
@paperclip_config ||= | ||
<<~RUBY | ||
config.paperclip_defaults = { | ||
storage: :s3, | ||
s3_protocol: 'https', | ||
s3_region: ENV.fetch('AWS_REGION', 'us-east-1'), | ||
s3_credentials: { | ||
bucket: ENV['S3_BUCKET'] | ||
} | ||
} | ||
RUBY | ||
end | ||
|
||
def config_rspec_for_paperclip | ||
copy_file '../assets/testing/platanus.png', 'spec/assets/platanus.png' | ||
copy_file '../assets/testing/paperclip.rb', 'spec/support/paperclip.rb' | ||
end | ||
|
||
def add_paperclip | ||
gather_gem 'paperclip', '~> 5.0' | ||
application paperclip_config, env: 'production' | ||
append_to_file '.gitignore', "/public/system/*\n" | ||
end | ||
|
||
def add_active_storage | ||
after(:gem_install) { run('bundle exec rails active_storage:install') } | ||
copy_file('../assets/config/storage.yml', 'config/storage.yml', force: true) | ||
active_storage_service_regexp = /config.active_storage.service = :local\n/ | ||
gsub_file 'config/environments/production.rb', active_storage_service_regexp do | ||
'config.active_storage.service = :amazon' | ||
end | ||
end | ||
|
||
def common_setup | ||
add_readme_section :internal_dependencies, get(:storage) | ||
append_to_file '.env.development', "S3_BUCKET=\n" | ||
end | ||
|
||
def add_chosen_storage(check_rspec:) | ||
return if get(:storage) == :none | ||
|
||
common_setup | ||
case get(:storage) | ||
when :paperclip | ||
add_paperclip | ||
config_rspec_for_paperclip if !check_rspec || gem_exists?(/rspec-rails/) | ||
when :active_storage | ||
add_active_storage | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe "File Storage" do | ||
context "when selecting active_storage" do | ||
before :all do | ||
drop_dummy_database | ||
remove_project_directory | ||
create_dummy_project(storage: :active_storage) | ||
end | ||
|
||
it "customizes config file" do | ||
content = IO.read("#{project_path}/config/storage.yml") | ||
expect(content).to include("bucket: <%= ENV['S3_BUCKET'] %>") | ||
end | ||
|
||
it "adds brief to README file" do | ||
content = IO.read("#{project_path}/README.md") | ||
expect(content).to include("Active Storage") | ||
end | ||
|
||
it "uses amazon on production env" do | ||
content = IO.read("#{project_path}/config/environments/production.rb") | ||
expect(content).to include("config.active_storage.service = :amazon") | ||
end | ||
|
||
it "adds S3 bucket ENV vars" do | ||
content = IO.read("#{project_path}/.env.development") | ||
expect(content).to include("S3_BUCKET=") | ||
end | ||
end | ||
|
||
context "when selecting paperclip" do | ||
before :all do | ||
drop_dummy_database | ||
remove_project_directory | ||
create_dummy_project(storage: :paperclip) | ||
end | ||
|
||
it "adds the Paperclip gem to Gemfile" do | ||
gemfile_content = IO.read("#{project_path}/Gemfile") | ||
expect(gemfile_content).to include("gem 'paperclip'") | ||
end | ||
|
||
it "adds brief to README file" do | ||
content = IO.read("#{project_path}/README.md") | ||
expect(content).to include("Paperclip") | ||
end | ||
|
||
it "adds local file storage path to gitignore" do | ||
content = IO.read("#{project_path}/.gitignore") | ||
expect(content).to include("/public/system/*") | ||
end | ||
|
||
it "adds paperclip_defaults config to production" do | ||
content = IO.read("#{project_path}/config/environments/production.rb") | ||
expect(content).to include("config.paperclip_defaults") | ||
end | ||
|
||
it "adds S3 bucket ENV var" do | ||
content = IO.read("#{project_path}/.env.development") | ||
expect(content).to include("S3_BUCKET=") | ||
end | ||
end | ||
end |