I'm writing a Rails generator that is going to copy files/folders from my gem's template directory into the app's directory. It works as expected when I run rails generate mygem:install however when I try to reverse it using rails destroy mygem:install it doesn't remove the newly created sub folders.
templates folder
├── templates
│ ├── views
│ │ ├── about
│ │ │ ├── index.html.erb
│ │ ├── contact
│ │ │ ├── index.html.erb
app folder (after generate)
├── app
│ ├── views
│ │ ├── about
│ │ │ ├── index.html.erb
│ │ ├── contact
│ │ │ ├── index.html.erb
app folder (after destroy)
├── app
│ ├── views
│ │ ├── about
│ │ ├── contact
desired outcome
├── app
│ ├── views
my gem's install generator
module Mygem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def copy_templates
templates = Dir.glob("#{source_paths[0]}/*")
directory(templates[0], "app/views/")
end
end
end
end
I encountered the same problem - fixed by adding the following to my generator.rb file
You also have the
:invokeoption to specify actions which only happen when generating:so