How to implement generators for a plugin located in the `lib / <plugin_name>` directory?
I am using Ruby on Rails 3.2.2. I implemented a plugin Something(this is almost a gem, but not a gem), and all related files are in the directory lib/something. Since I would like to automate the code generation associated with this plugin, I came up with Ruby on Rails Generators . So, for the plugin, SomethingI am looking for an implementation of my own generators in the directory lib/something.
How do I do this and what recipes? That is, for example, what command line rails generateshould be called to correctly create all the necessary files in the directory lib/something? Will generators still work with plugins (not gems)? What are the tips on this?
I would make it a gem. I made gem generators, but I don’t know if the generators will work with plugins.
If you are having difficulty with the command line, I assume that you do not need any arguments. (If you need an argument, I could copy the provided templates, and if I needed another argument, I would be lost, so my advice is not limited to the argument.)
, , . , ( ) db/migrate .
. , .
class ItrcClientFilesGenerator < Rails::Generators::Base
source_root(File.dirname(__FILE__) + "/../src")
desc "Generator to create migrations for needed db tables"
def create_itrc_client_files
prefix = DateTime.now.strftime("%Y%m%d%H%M")
existing_migrations =
Dir.glob("db/migrate/*itrc*").map do |path|
File.basename(path).gsub(/^\d*_/, '')
end
Dir.glob(File.dirname(__FILE__) + "/../src/*").sort.each_with_index do |src_filepath, index|
src_filename = File.basename(src_filepath)
unless existing_migrations.include?(src_filename.gsub(/^\d*_/, '')) then
this_prefix = "#{prefix}#{'%02i' % index}_"
dst_filename = src_filename.gsub(/^\d*_/, this_prefix)
copy_file(src_filename, "db/migrate/" + dst_filename)
end
end
end
end