From 03bb008b3c6dc9045a0b0bc454a41ffc41032c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:28:36 -0400 Subject: [PATCH 1/8] feat(recipes): unify paperclip and active_storage questions --- lib/potassium/recipes/file_storage.rb | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 lib/potassium/recipes/file_storage.rb diff --git a/lib/potassium/recipes/file_storage.rb b/lib/potassium/recipes/file_storage.rb new file mode 100644 index 00000000..b9753fa9 --- /dev/null +++ b/lib/potassium/recipes/file_storage.rb @@ -0,0 +1,82 @@ +class Recipes::FileStorage < Rails::AppBuilder + def ask + storages = { + active_storage: 'ActiveStorage', + paperclip: '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 From 55338f3cd1763358bf9157d6dfd5bea911658d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:30:10 -0400 Subject: [PATCH 2/8] test(file-storage): move existing active_storage tests and add papaerclips --- spec/features/file_storage_spec.rb | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 spec/features/file_storage_spec.rb diff --git a/spec/features/file_storage_spec.rb b/spec/features/file_storage_spec.rb new file mode 100644 index 00000000..700acdaa --- /dev/null +++ b/spec/features/file_storage_spec.rb @@ -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 From 7f6590b013a64215269597c6832aa136dffe50ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:31:42 -0400 Subject: [PATCH 3/8] feat(application): change ask/create of active_storage/paperclip --- lib/potassium/templates/application.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/potassium/templates/application.rb b/lib/potassium/templates/application.rb index 46800634..cd4b7175 100644 --- a/lib/potassium/templates/application.rb +++ b/lib/potassium/templates/application.rb @@ -29,8 +29,7 @@ ask :i18n ask :api ask :draper - ask :active_storage - ask :paperclip + ask :file_storage ask :heroku ask :github end @@ -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 From 8380c958b7deac63d5d4edde50d6ae5c00593a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:33:08 -0400 Subject: [PATCH 4/8] feat(cli-options): update options to show storage as flag --- lib/potassium/cli_options.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/potassium/cli_options.rb b/lib/potassium/cli_options.rb index 22db700a..1a90a3f4 100644 --- a/lib/potassium/cli_options.rb +++ b/lib/potassium/cli_options.rb @@ -67,20 +67,10 @@ 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_test_value: "active_storage" }, { type: :switch, From 3c620c1cd5c5d6f38f56e7a32cc3f505facc4da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:34:53 -0400 Subject: [PATCH 5/8] refactor(README): rename storage section to active_storage --- lib/potassium/assets/README.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/potassium/assets/README.yml b/lib/potassium/assets/README.yml index aad0a2d5..ec45fbff 100644 --- a/lib/potassium/assets/README.yml +++ b/lib/potassium/assets/README.yml @@ -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: From db71b6e54c6f32df08b5c1d7356c37432b9a5e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 12 Jul 2019 17:36:10 -0400 Subject: [PATCH 6/8] refactor(): erase unused paperclip/active_storage files --- lib/potassium/recipes/active_storage.rb | 40 --------------------- lib/potassium/recipes/paperclip.rb | 47 ------------------------- spec/features/active_storage_spec.rb | 30 ---------------- 3 files changed, 117 deletions(-) delete mode 100644 lib/potassium/recipes/active_storage.rb delete mode 100644 lib/potassium/recipes/paperclip.rb delete mode 100644 spec/features/active_storage_spec.rb diff --git a/lib/potassium/recipes/active_storage.rb b/lib/potassium/recipes/active_storage.rb deleted file mode 100644 index e54535a7..00000000 --- a/lib/potassium/recipes/active_storage.rb +++ /dev/null @@ -1,40 +0,0 @@ -class Recipes::ActiveStorage < Rails::AppBuilder - def ask - active_storage = answer(:active_storage) do - Ask.confirm("Do you want to use ActiveStorage for uploads?") - end - - set(:active_storage, active_storage) - end - - def create - return unless selected?(:active_storage) - add_active_storage - end - - def install - add_active_storage - end - - def installed? - file_exist?('config/storage.yml') - end - - private - - def add_active_storage - after(:gem_install) { run("bundle exec rails active_storage:install") } - - add_readme_section :internal_dependencies, :storage - - copy_file("../assets/config/storage.yml", "config/storage.yml", force: true) - - append_to_file '.env.development', "AWS_REGION=\n" - append_to_file '.env.development', "S3_BUCKET=\n" - - raise_delivery_errors_regexp = /config.active_storage.service = :local\n/ - gsub_file 'config/environments/production.rb', raise_delivery_errors_regexp do - "config.active_storage.service = :amazon" - end - end -end diff --git a/lib/potassium/recipes/paperclip.rb b/lib/potassium/recipes/paperclip.rb deleted file mode 100644 index 559a7937..00000000 --- a/lib/potassium/recipes/paperclip.rb +++ /dev/null @@ -1,47 +0,0 @@ -class Recipes::Paperclip < Rails::AppBuilder - def ask - paperclip = answer(:paperclip) { Ask.confirm("Do you want to use Paperclip for uploads?") } - set(:paperclip, paperclip) - end - - def create - return unless selected?(:paperclip) - add_paperclip - config_rspec - end - - def install - add_paperclip - config_rspec if gem_exists?(/rspec-rails/) - end - - def installed? - gem_exists?(/paperclip/) - end - - private - - def add_paperclip - gather_gem 'paperclip', '~> 5.0' - 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 - application paperclip_config, env: 'production' - append_to_file '.env.development', "S3_BUCKET=\n" - append_to_file '.gitignore', "/public/system/*\n" - add_readme_section :internal_dependencies, :paperclip - end - - def config_rspec - copy_file '../assets/testing/platanus.png', 'spec/assets/platanus.png' - copy_file '../assets/testing/paperclip.rb', 'spec/support/paperclip.rb' - end -end diff --git a/spec/features/active_storage_spec.rb b/spec/features/active_storage_spec.rb deleted file mode 100644 index 944bfcb6..00000000 --- a/spec/features/active_storage_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "spec_helper" - -RSpec.describe "Active Storage" do - before :all do - drop_dummy_database - remove_project_directory - create_dummy_project("active_storage" => true) - 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 amazon ENV vars" do - content = IO.read("#{project_path}/.env.development") - expect(content).to include("AWS_REGION=") - expect(content).to include("S3_BUCKET=") - end -end From 0bbb1ff4c17a5a7e9d8cb9d5daca4adc08b0dcc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 26 Jul 2019 10:05:47 -0400 Subject: [PATCH 7/8] feat(file-storage): add deprecation message to paperclip option --- lib/potassium/recipes/file_storage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/potassium/recipes/file_storage.rb b/lib/potassium/recipes/file_storage.rb index b9753fa9..d6da1dd1 100644 --- a/lib/potassium/recipes/file_storage.rb +++ b/lib/potassium/recipes/file_storage.rb @@ -2,7 +2,7 @@ class Recipes::FileStorage < Rails::AppBuilder def ask storages = { active_storage: 'ActiveStorage', - paperclip: 'Paperclip', + paperclip: '[DEPRECATED] Paperclip', none: 'None, thanks' } From d3a3f17aca5db205b8d995204625dd7ed1acadd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fern=C3=A1ndez?= Date: Fri, 26 Jul 2019 10:07:38 -0400 Subject: [PATCH 8/8] feat(cli-options): add active_storage as default value --- lib/potassium/cli_options.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/potassium/cli_options.rb b/lib/potassium/cli_options.rb index 1a90a3f4..f93e3bfe 100644 --- a/lib/potassium/cli_options.rb +++ b/lib/potassium/cli_options.rb @@ -70,6 +70,7 @@ module Potassium::CliOptions # rubocop:disable Metrics/ModuleLength 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" }, {