From c92c172f2db75d3747a36b3a7f143860aa54f1ef Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 30 Apr 2020 15:39:25 -0500 Subject: [PATCH] Initial generator for beginning migration from carrierwave to activestorage --- lib/generators/cw_to_activestorage/USAGE | 8 +++ .../cw_to_activestorage_generator.rb | 39 +++++++++++ .../templates/initializers/carrierwave.rb | 17 +++++ .../migrate/rename_uploader_columns.rb | 19 ++++++ .../templates/models/campaign.rb | 5 ++ .../templates/models/event.rb | 4 ++ .../templates/models/image_attachment.rb | 3 + .../templates/models/nonprofit.rb | 7 ++ .../templates/models/profile.rb | 3 + .../uploaders/article_background_uploader.rb | 54 ++++++++++++++++ .../templates/uploaders/article_uploader.rb | 54 ++++++++++++++++ .../campaign_background_image_uploader.rb | 25 ++++++++ .../campaign_banner_image_uploader.rb | 18 ++++++ .../uploaders/campaign_main_image_uploader.rb | 56 ++++++++++++++++ .../event_background_image_uploader.rb | 52 +++++++++++++++ .../uploaders/event_main_image_uploader.rb | 56 ++++++++++++++++ .../uploaders/image_attachment_uploader.rb | 64 +++++++++++++++++++ .../nonprofit_background_uploader.rb | 55 ++++++++++++++++ .../uploaders/nonprofit_logo_uploader.rb | 51 +++++++++++++++ .../templates/uploaders/nonprofit_uploader.rb | 61 ++++++++++++++++++ .../templates/uploaders/profile_uploader.rb | 60 +++++++++++++++++ 21 files changed, 711 insertions(+) create mode 100644 lib/generators/cw_to_activestorage/USAGE create mode 100644 lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb create mode 100644 lib/generators/cw_to_activestorage/templates/initializers/carrierwave.rb create mode 100644 lib/generators/cw_to_activestorage/templates/migrate/rename_uploader_columns.rb create mode 100644 lib/generators/cw_to_activestorage/templates/models/campaign.rb create mode 100644 lib/generators/cw_to_activestorage/templates/models/event.rb create mode 100644 lib/generators/cw_to_activestorage/templates/models/image_attachment.rb create mode 100644 lib/generators/cw_to_activestorage/templates/models/nonprofit.rb create mode 100644 lib/generators/cw_to_activestorage/templates/models/profile.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/article_background_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/article_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/campaign_background_image_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/campaign_banner_image_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/campaign_main_image_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/event_background_image_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/event_main_image_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/image_attachment_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_background_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_logo_uploader.rb create mode 100755 lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_uploader.rb create mode 100644 lib/generators/cw_to_activestorage/templates/uploaders/profile_uploader.rb diff --git a/lib/generators/cw_to_activestorage/USAGE b/lib/generators/cw_to_activestorage/USAGE new file mode 100644 index 00000000..d93ba875 --- /dev/null +++ b/lib/generators/cw_to_activestorage/USAGE @@ -0,0 +1,8 @@ +Description: + Explain the generator + +Example: + rails generate cw_to_activestorage + + This will create: + what/will/it/create diff --git a/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb b/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb new file mode 100644 index 00000000..64b0084d --- /dev/null +++ b/lib/generators/cw_to_activestorage/cw_to_activestorage_generator.rb @@ -0,0 +1,39 @@ +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class CwToActivestorageGenerator < Rails::Generators::Base + source_root File.expand_path('templates', __dir__) + + def copy_uploaders + if (!File.exists?('app/uploaders')) + directory 'uploaders', 'app/uploaders' + end + end + + def include_uploaders + file_and_search = [ + ["campaign.rb", "class Campaign < ApplicationRecord\n"], + ["profile.rb", "class Profile < ApplicationRecord\n"], + ['nonprofit.rb', "class Nonprofit < ApplicationRecord\n"], + ['image_attachment.rb', "class ImageAttachment < ApplicationRecord\n"], + ['event.rb', "class Event < ApplicationRecord\n"] + ] + file_and_search + .select{|filename, _| !File.read("app/models/#{filename}").include?('###MIGRATION_FIELDS_BEGIN')}\ + .each do |filename, find_string| + gsub_file("app/models/#{filename}",find_string ) do |match| + match << File.read(Pathname(File.expand_path('templates', __dir__, )) + 'models' + "#{filename}") + end + end + end + + def create_column_migration_file + if (Dir.glob("db/migrate/*_rename_uploader_columns.rb").none?) + copy_file "migrate/rename_uploader_columns.rb", + "db/migrate/#{DateTime.now.strftime('%Y%m%d%H%M%S')}_rename_uploader_columns.rb" + end + end + + def add_carrierwave_gems + gem "carrierwave", "~> 1" + gem "carrierwave-aws" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/initializers/carrierwave.rb b/lib/generators/cw_to_activestorage/templates/initializers/carrierwave.rb new file mode 100644 index 00000000..9014df8a --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/initializers/carrierwave.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later + +CarrierWave.configure do |config| + config.storage = :aws + config.aws_bucket = ENV['CARRIERWAVE_AWS_BUCKET'] + config.aws_acl = :public_read + config.asset_host = ENV['CARRIERWAVE_IMAGE_HOST'] || "https://#{config.aws_bucket}.s3.amazonaws.com" + # config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365 + # config.aws_credentials = { + # access_key_id: ENV['CARRIERWAVE_AWS_ACCESS_KEY_ID'], + # secret_access_key: ENV['CARRIERWAVE_SECRET_ACCESS_KEY'], + # config: AWS.config(cache_dir: "#{Rails.root}/tmp/uploads", region: ENV['CARRIERWAVE_SECRET_ACCESS_KEY']) + # } +end + \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/migrate/rename_uploader_columns.rb b/lib/generators/cw_to_activestorage/templates/migrate/rename_uploader_columns.rb new file mode 100644 index 00000000..dfd67d93 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/migrate/rename_uploader_columns.rb @@ -0,0 +1,19 @@ +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class RenameUploaderColumns < ActiveRecord::Migration[5.2] + rename_column :campaigns, :main_image, :main_image_temp + rename_column :campaigns, :background_image, :background_image_temp + rename_column :campaigns, :banner_image, :banner_image_temp + + rename_column :events, :main_image, :main_image_temp + rename_column :events, :background_image, :background_image_temp + + rename_column :image_attachments, :file, :file_temp + + rename_column :nonprofits, :main_image, :main_image_temp + rename_column :nonprofits, :second_image, :second_image_temp + rename_column :nonprofits, :third_image, :third_image_temp + rename_column :nonprofits, :background_image, :background_image_temp + rename_column :nonprofits, :logo_image, :logo_image_temp + + rename_column :profiles, :picture, :picture_temp +end \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/models/campaign.rb b/lib/generators/cw_to_activestorage/templates/models/campaign.rb new file mode 100644 index 00000000..ce91277f --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/models/campaign.rb @@ -0,0 +1,5 @@ +###MIGRATION_FIELDS_BEGIN +mount_uploader :main_image_temp, CampaignMainImageUploader +mount_uploader :background_image_temp, CampaignBackgroundImageUploader +mount_uploader :banner_image_temp, CampaignBannerImageUploader +###MIGRATION_FIELDS_END \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/models/event.rb b/lib/generators/cw_to_activestorage/templates/models/event.rb new file mode 100644 index 00000000..5d39c728 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/models/event.rb @@ -0,0 +1,4 @@ +###MIGRATION_FIELDS_BEGIN +mount_uploader :main_image_temp, EventMainImageUploader +mount_uploader :background_image_temp, EventBackgroundImageUploader +###MIGRATION_FIELDS_END \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/models/image_attachment.rb b/lib/generators/cw_to_activestorage/templates/models/image_attachment.rb new file mode 100644 index 00000000..561ab5f3 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/models/image_attachment.rb @@ -0,0 +1,3 @@ +###MIGRATION_FIELDS_BEGIN +mount_uploader :file_temp, ImageAttachmentUploader +###MIGRATION_FIELDS_END \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/models/nonprofit.rb b/lib/generators/cw_to_activestorage/templates/models/nonprofit.rb new file mode 100644 index 00000000..e23110c9 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/models/nonprofit.rb @@ -0,0 +1,7 @@ +###MIGRATION_FIELDS_BEGIN +mount_uploader :main_image_temp, NonprofitUploader +mount_uploader :second_image_temp, NonprofitUploader +mount_uploader :third_image_temp, NonprofitUploader +mount_uploader :background_image_temp, NonprofitBackgroundUploader +mount_uploader :logo_temp, NonprofitLogoUploader +###MIGRATION_FIELDS_END diff --git a/lib/generators/cw_to_activestorage/templates/models/profile.rb b/lib/generators/cw_to_activestorage/templates/models/profile.rb new file mode 100644 index 00000000..31bee3d2 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/models/profile.rb @@ -0,0 +1,3 @@ +###MIGRATION_FIELDS_BEGIN +mount_uploader :picture_temp, ProfileUploader +###MIGRATION_FIELDS_END \ No newline at end of file diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/article_background_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/article_background_uploader.rb new file mode 100644 index 00000000..9ac0229f --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/article_background_uploader.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class ArticleBackgroundUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultNonprofitUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :large do + process resize_to_fill: [600, 400] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/article_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/article_uploader.rb new file mode 100644 index 00000000..906c96ad --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/article_uploader.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class ArticleUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultNonprofitUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :thumb do + process resize_to_fill: [200, 200] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/campaign_background_image_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_background_image_uploader.rb new file mode 100644 index 00000000..da31b91c --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_background_image_uploader.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class CampaignBackgroundImageUploader < CarrierWave::Uploader::Base + include CarrierWave::MiniMagick + + def store_dir + "uploads/campaigns/#{mounted_as}/#{model.id}" + end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [1000, 600] + end + + def extension_white_list + %w[jpg jpeg png] + end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/campaign_banner_image_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_banner_image_uploader.rb new file mode 100644 index 00000000..09fe0e46 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_banner_image_uploader.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class CampaignBannerImageUploader < CarrierWave::Uploader::Base + include CarrierWave::MiniMagick + + def store_dir + "uploads/campaigns/#{mounted_as}/#{model.id}" + end + + def extension_white_list + %w[jpg jpeg png] + end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/campaign_main_image_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_main_image_uploader.rb new file mode 100644 index 00000000..a8def8e4 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/campaign_main_image_uploader.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class CampaignMainImageUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/campaigns/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [524, 360] + end + + version :thumb do + process resize_to_fill: [180, 150] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/event_background_image_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/event_background_image_uploader.rb new file mode 100644 index 00000000..123517b2 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/event_background_image_uploader.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class EventBackgroundImageUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/events/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultCampaignUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [1000, 600] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/event_main_image_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/event_main_image_uploader.rb new file mode 100644 index 00000000..e8b8adb1 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/event_main_image_uploader.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class EventMainImageUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/events/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [400, 400] + end + + version :thumb do + process resize_to_fill: [100, 100] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/image_attachment_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/image_attachment_uploader.rb new file mode 100644 index 00000000..a5e6d304 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/image_attachment_uploader.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class ImageAttachmentUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/image_attachments/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :large do + process resize_to_fill: [600, 400] + end + version :medium do + process resize_to_fill: [400, 266] + end + version :small do + process resize_to_fill: [400, 266] + end + # slightly smaller than the normal thumb + version :thumb_explore do + process resize_to_fill: [200, 133] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_background_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_background_uploader.rb new file mode 100644 index 00000000..e6134f68 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_background_uploader.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later + +# encoding: utf-8 + +class NonprofitBackgroundUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/npo/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultNonprofitUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [1000, 600] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_logo_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_logo_uploader.rb new file mode 100644 index 00000000..55dd242e --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_logo_uploader.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class NonprofitLogoUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/npo/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Create different versions of your uploaded files: + version :large do + process resize_to_fit: [180, 180] + end + version :normal do + process resize_to_fit: [100, 100] + end + version :small do + process resize_to_fit: [30, 30] + end + + def extension_white_list + %w[jpg jpeg png gif] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_uploader.rb new file mode 100755 index 00000000..334d3df6 --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/nonprofit_uploader.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class NonprofitUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/npo/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :nonprofit_carousel do + process resize_to_fill: [590, 338] + end + version :thumb do + process resize_to_fill: [188, 120] + end + # slightly smaller than the normal thumb + version :thumb_explore do + process resize_to_fill: [100, 100] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end diff --git a/lib/generators/cw_to_activestorage/templates/uploaders/profile_uploader.rb b/lib/generators/cw_to_activestorage/templates/uploaders/profile_uploader.rb new file mode 100644 index 00000000..1abd9e4c --- /dev/null +++ b/lib/generators/cw_to_activestorage/templates/uploaders/profile_uploader.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +# encoding: utf-8 + +class ProfileUploader < CarrierWave::Uploader::Base + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + include CarrierWave::MiniMagick + + # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: + # include Sprockets::Helpers::RailsHelper + # include Sprockets::Helpers::IsolatedHelper + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + def default_url + # For Rails 3.1+ asset pipeline compatibility: + Image::DefaultProfileUrl + end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + version :normal do + process resize_to_fill: [150, 150] + end + version :medium do + process resize_to_fill: [100, 100] + end + version :tiny do + process resize_to_fill: [50, 50] + end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w[jpg jpeg png] + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + + def cache_dir + "#{Rails.root}/tmp/uploads" + end +end