It seems that I am stuck figuring out so that my generator doesn't need an argument. So for instance my generator code is this:
class MyGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
def generate_stylesheet
copy_file "my.css", "public/stylesheets/my.css"
end
end
But when I do rails g my rails always asks for an extra argument. Can you show me how so it doesn't need an extra argument?
Thanks.
You have to use class MyGenerator < Rails::Generators::Base instead of class MyGenerator < Rails::Generators::NamedBase
Related
this is my generator class in lib/genrators/form
class FormGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __FILE__)
source_root File.expand_path('templates', __dir__)
argument :model, type: :string
def create_template
template "models/form.template", "app/models/#{model}.rb"
template "controllers/forms_controller.template", "app/controllers/#{model}s_controller.rb"
template "javascript/api/form.template", "app/javascript/api/#{model}.js"
template "javascript/pages/component/forms.template", "app/javascript/component/#{model}s.vue"
template "javascript/pages/form/index.template", "app/javascript/pages/#{model}/index.vue"
template "javascript/pages/form/layout.template", "app/javascript/pages/#{model}/layout.vue"
template "javascript/store/actions/form.template", "app/javascript/store/actions/#{model}.js"
template "javascript/store/getters/form.template", "app/javascript/store/getters/#{model}.js"
template "javascript/store/modules/form.template", "app/javascript/store/modules/#{model}.js"
template "javascript/store/mutations/form.template", "app/javascript/store/mutations/#{model}.js"
end
end
and this is output :
rails g form test name:string
create app/models/test.rb
create app/controllers/tests_controller.rb
create app/javascript/api/test.js
create app/javascript/component/tests.vue
create app/javascript/pages/test/index.vue
create app/javascript/pages/test/layout.vue
create app/javascript/store/actions/test.js
create app/javascript/store/getters/test.js
create app/javascript/store/modules/test.js
create app/javascript/store/mutations/test.js
i wanna this generator , generates a migration file in db/migrate
i have tried these things and it didnt work out :
class FormGenerator < Rails::Generators::Base
include Rails::Generators::Migration
include ActiveRecord::Generators::Base
migration_template
I'm developing a gem core with multiple sub modules, each it's own gem. As a developer, you'll be able to install the core and any other of the gems. How can I create a rake task or generator to run the generators for ALL of the installed gems with generators under the main gem namespace.
Example, if I my gem is called admin:
module Admin
module Generators
class InstallGenerator < Rails::Generators::Base
end
end
end
And I have another generator for one of the sub-gems:
module Admin
module Generators
class PostsGenerator < Rails::Generators::Base
end
end
end
And another one:
module Admin
module Generators
class TagslGenerator < Rails::Generators::Base
end
end
end
And there might be up to 10 more gems that can be installed. Rather than rail g admin:... installing each one, I would like to create a rake task or generator that runs all of the tasks.
Thanks in advance!
Keep an "AllGenerator" class under Admin module. The generator will have to do the following :
For each class under the namespace that is a generator class,
get the namespace from classname.
Call the invoke method with the namespace.
Something like this :
module Admin
module Generators
class AllGenerator < Rails::Generators::Base
def generator
Rails::Generators.lookup!
Admin::Generators.constants.each do |const|
generator_class = Admin::Generators.const_get(const)
next if self.class == generator_class
if generator_class < Rails::Generators::Base
namespace = generator_klass_to_namespace(generator_class)
invoke(namespace)
end
end
end
private
def generator_klass_to_namespace(klass)
namespace = Thor::Util.namespace_from_thor_class(klass)
return namespace.sub(/_generator$/, '').sub(/:generators:/, ':')
end
end
end
end
Here's the link to the gist with complete tested code
This way, running rails g admin:all would run every other generator directly under Admin::Generators .
First check out the following question and answer.
Find classes available in a Module
So all you have to do is access
Admin::Generators.constants.each do |c|
c = Admin::Generators.const_get(c)
if c < Rails::Generators::Base
c.new.run(your_args)
end
end
Only thing is I have never invoked a generator like this so it might be a little bit more then c.new.run, but I think that should do it.
I'm building a Rails engine in a ruby gem. It includes some migrations right now that are called when you run:
rails g myengine:install
The code in the generator is as follows:
module MyEngine
module Generators
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
# ...
def copy_migrations
migration_template "migrations/migration1.rb", "db/migrate/migration1.rb"
migration_template "migrations/migration2.rb", "db/migrate/migration2.rb"
end
# ...
end
end
end
However, if I run rails g myengine:install again, it fails with this error:
Another migration is already named migration1: /Users/jh/Code/Web/demoapp/db/migrate/20130327222221_migration1.rb
I want it to just silently ignore the fact that there's already a migration and continue on to the next migration. What would be the best way to do this?
EDIT:
Per Dmitry's answer, this was my solution:
def copy_migrations
copy_migration "migration1"
copy_migration "migration2"
end
protected
def copy_migration(filename)
if self.class.migration_exists?("db/migrate", "#{filename}")
say_status("skipped", "Migration #{filename}.rb already exists")
else
migration_template "migrations/#{filename}.rb", "db/migrate/#{filename}.rb"
end
end
Using migration_template in Rails as example, you could perhaps check for destination = self.class.migration_exists?(migration_dir, #migration_file_name) and if migration already exists, skip over making migration_template call.
I'm creating a rails generator:
class TaggableGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
hook_for :orm, :as => "model"
end
Everything works fine, but I would like to set the fields created in the models and create multiple models, I can't find anything about how to do it (I got the above code from looking at the devise generators) preferably I'd like it to me orm generic (but its not that important).
Here is some links that may help you:
http://railscasts.com/episodes/218-making-generators-in-rails-3
http://guides.rubyonrails.org/generators.html
Basically you just need to add methods in your class to do things you want (all the public methods will get called when the generator is called), here is an example from rails sources:
class AssetsGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)
def copy_stylesheet
copy_file "stylesheet.css", File.join('app/assets/stylesheets', class_path, "#{file_name}.css")
end
end
the copy_file comes from Thor, you wan see a list of availables methods in the Thor reference: http://textmate.rubyforge.org/thor/Thor/Actions.html
I am simply trying to create a plugin migration generator without any parameters, like : $rails generate yaffle and this should copy the migration file (lib/generators/yaffle/template/create_yaffle.rb) to db/migrate/[timestamp]_create_yaffle.rb.
The problem I am facing here is, its copying, but without timestamp.
Also, when I run $rails generate yaffle it gives me a message that arguments are not provided, it expects to be in this format rails generate yaffle NAME [options]. I dont want to have any options/arguments, it should just be rails generate yaffle.
What should I do?
I followed the generator used in acts_as_commentable , it looks pretty simple, but I don't know where to modify these settings... can anybody help?
Generator Code:
require 'rails/generators'
require 'rails/generators/migration'
class ThumbitGenerator Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
def self.next_migration_number(path)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def create_model_file
template "like.rb", "app/models/like.rb"
template "liking.rb", "app/models/liking.rb"
template "create_likes.rb", "db/migrate/create_likes.rb"
template "create_likings.rb", "db/migrate/create_likings.rb"
end
end
Ok, I found the answer...
I was using Rails::Generators::NamedBase instead of Rails::Generators::Base in my generator file! When you use NamedBase, it always expects an argument to be passed (which is the name of initializer) Explanation : guides.rubyonrails.org/generators
And I was using template method instead of migration_template because of which migration files din't produce any migration number Explanation: Rails::Generators::Migration.migration_template
So finally, this worked!
require 'rails/generators'
require 'rails/generators/migration'
class ThumbitGenerator < Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
def self.next_migration_number(path)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def create_model_file
template "like.rb", "app/models/like.rb"
template "liking.rb", "app/models/liking.rb"
migration_template "create_likes.rb", "db/migrate/create_likes.rb"
migration_template "create_likings.rb", "db/migrate/create_likings.rb"
end
end
A small polish on the solution - to save yourself the hassle of defining the timestamp for the migration and future proof your generator in case Rails core team decides to use another way of stamping (e.g. SHA hashes truncated to 10 characters), you can require 'rails/generators/active_record' and extend ActiveRecord::Generators::Migration like this:
require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'
class ThumbitGenerator < Rails::Generators::Base
include Rails::Generators::Migration
extend ActiveRecord::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
def create_model_file
template "like.rb", "app/models/like.rb"
template "liking.rb", "app/models/liking.rb"
migration_template "create_likes.rb", "db/migrate/create_likes.rb"
migration_template "create_likings.rb", "db/migrate/create_likings.rb"
end
end
UPDATE In Rails 4 ActiveRecord::Generators::Migration is no longer a module, so use instead:
require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'
class ThumbitGenerator < Rails::Generators::Base
include Rails::Generators::Migration
# Implement the required interface for Rails::Generators::Migration
def self.next_migration_number(dirname)
ActiveRecord::Generators::Base.next_migration_number(dirname)
end
source_root File.expand_path('../templates', __FILE__)
def create_model_file
template "like.rb", "app/models/like.rb"
template "liking.rb", "app/models/liking.rb"
migration_template "create_likes.rb", "db/migrate/create_likes.rb"
migration_template "create_likings.rb", "db/migrate/create_likings.rb"
end
end
you can simply inherit from ActiveRecord::Generators::Base and everything will work