Skip to content

Commit

Permalink
Merge pull request #211 from platanus/unify-storage-options
Browse files Browse the repository at this point in the history
Unify storage options
  • Loading branch information
difernandez authored Aug 2, 2019
2 parents faf4622 + d3a3f17 commit 8e2a9db
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 136 deletions.
2 changes: 1 addition & 1 deletion lib/potassium/assets/README.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ readme:
paperclip:
title: "Uploads"
body: "For managing uploads, this project uses [Paperclip](https://github.com/thoughtbot/paperclip), a gem made by the awesome [Thoughbot](https://thoughtbot.com/) team."
storage:
active_storage:
title: "Active Storage"
body: "For managing uploads, this project uses [Active Storage](https://github.com/rails/rails/tree/master/activestorage)."
pundit:
Expand Down
19 changes: 5 additions & 14 deletions lib/potassium/cli_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,11 @@ module Potassium::CliOptions # rubocop:disable Metrics/ModuleLength
default_test_value: false
},
{
type: :switch,
name: "paperclip",
desc: "Whether to include Paperclip as dependency",
negatable: true,
default_value: "none",
default_test_value: false
},
{
type: :switch,
name: "active_storage",
desc: "Whether to include Active Storage as dependency",
negatable: true,
default_value: "none",
default_test_value: false
type: :flag,
name: "storage",
desc: "Decides which file storage to use. Available: active_storage, paperclip, none",
default_value: "active_storage",
default_test_value: "active_storage"
},
{
type: :switch,
Expand Down
40 changes: 0 additions & 40 deletions lib/potassium/recipes/active_storage.rb

This file was deleted.

82 changes: 82 additions & 0 deletions lib/potassium/recipes/file_storage.rb
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
47 changes: 0 additions & 47 deletions lib/potassium/recipes/paperclip.rb

This file was deleted.

6 changes: 2 additions & 4 deletions lib/potassium/templates/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
ask :i18n
ask :api
ask :draper
ask :active_storage
ask :paperclip
ask :file_storage
ask :heroku
ask :github
end
Expand Down Expand Up @@ -69,8 +68,7 @@
create :draper
create :power_types
create :rack_cors
create :active_storage
create :paperclip
create :file_storage
create :tzinfo
create :script
create :github
Expand Down
30 changes: 0 additions & 30 deletions spec/features/active_storage_spec.rb

This file was deleted.

64 changes: 64 additions & 0 deletions spec/features/file_storage_spec.rb
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

0 comments on commit 8e2a9db

Please sign in to comment.