# encoding : utf-8 class BeautifulStorageGenerator < Rails::Generators::Base require_relative 'beautiful_scaffold_common_methods' include BeautifulScaffoldCommonMethods source_root File.expand_path('../templates', __FILE__) # TODO voir pour engine argument :model, :type => :string, :desc => "Name of model (ex: user)" argument :storage_name, :type => :string, :desc => "Storage's name (ex: picture_file)" class_option :mountable_engine, default: nil def install_storage #if !File.read('Gemfile').include?("image_processing") gem("image_processing", '~> 1.2') #end Bundler.with_unbundled_env do run "bundle install" end # Install activestorage run "bin/rails active_storage:install" #run "rake db:migrate" raise "Model must be specified" if model.blank? raise "Attachment must be specified" if storage_name.blank? # ===== Model inject_into_file("app/models/#{engine_name}#{model}.rb", "\n has_one_attached :#{storage_name} \n", after: "< ApplicationRecord") inject_into_file("app/models/#{engine_name}#{model}.rb", ":#{storage_name},", :after => "def self.permitted_attributes\n return ") # ====== Views inject_into_file("app/views/#{engine_name}#{model_pluralize}/_form.html.erb", "
<%= t('app.models.#{model}.bs_attributes.#{storage_name}', :default => '#{storage_name}') %>:
<%= image_tag @#{model}.#{storage_name}.variant(resize_to_limit: [100, 100]) %>