Creating a rails 3 generator - ruby-on-rails

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

Related

Rails Gem: Running All Generators for given Namespace

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.

Rails: how to extend a generator?

I'm trying to extend the model generator in Rails ( rails g model ). Basically my generator should do the same thing as the model one, but copy 2 additional files. Simple as that.
I reviewed Railscast #218 ( http://railscasts.com/episodes/218-making-generators-in-rails-3 ) which was very informative but i couldn't find any info about extending generators.
Checking the source code of rails, it looks like the model generator is in lib/rails/generators/rails/model/model_generator.rb defined as Rails::Generators::ModelGenerator.
I tried to make my generator extend this class but it results in:
Error: uninitialized constant Rails::Generators::ModelGenerator.
And my attempts to require this file were not successful.
So I decided to stop and ask here. What is the proper way of extending a generator?
Take a look at hooks and invoke.
class MyGenerator < Rails::Generators::Base
def create_my_file
# Do your generators stuff
create_file "config/initializers/my.rb", "# Add content here"
# Create model
invoke("model", ["model_name", "arg1", "arg2"])
end
end
Hope this help.
Generate your custom generator:
rails generate generator my_model
Open lib/generators/my_model/my_model_generator.rb and change it to:
require 'rails/generators/active_record/model/model_generator'
class MyModelGenerator < ActiveRecord::Generators::ModelGenerator
source_root File.expand_path('../templates', __FILE__)
end
This works for rails engines. Don't forget to add required templates.

How to include Draper Decorators in Rails Console?

I'd like to see the output of some of my Draper Decorators in Rails console (https://github.com/drapergem/draper and http://railscasts.com/episodes/286-draper). To do so, I was looking for a way to include a decorator and its methods similar to as we would an application helper:
include ActionView::ApplicationHelper
A draper decorator inherits from an ApplicationDecorator, which inherits from Draper::Base
class ApplicationDecorator < Draper::Base
end
class MaterialDecorator < ApplicationDecorator
decorates :material
#methods to decorate your material..
end
I've tried include Draper::Base::MaterialDecorator, include ApplicationDecorator::MaterialDecorator, and some other similar variations with no luck.
How can I include a decorator such as the Material Decorator above in rails console?
So, it turns out (unless someone can show otherwise) that this isn't the way you test draper decorator output in rails console..
Instead, you just do:
material = Material.first
material = MaterialDecorator.decorate(material)
material.some_decorator_method
Add app/decorators to the Rails autoload path:
in config/application.rb:
config.autoload_paths << Rails.root.join('app/decorators')
then, the decorator classes under that path should automatically be loaded, even in the rails console. Example:
Material.first.decorate.some_decorator_method # => "good stuff"

Include Railtie initialization in seeds.rb in Rails 3.1

I have created a simple railtie, adding a bunch of stuff to ActiveRecord:
0 module Searchable
1 class Railtie < Rails::Railtie
2 initializer 'searchable.model_additions' do
3 ActiveSupport.on_load :active_record do
4 extend ModelAdditions
5 end
6 end
7 end
8 end
I require this file (in /lib) by adding the following line to config/environment.rb before the application is called:
require 'searchable'
This works great with my application and there are no major problems.
I have however encountered a problem with rake db:seed.
In my seeds.rb file, I read data in from a csv and populate the database. The problem I am having is that the additions I made to ActiveRecord don't get loaded, and seeds fails with a method_missing error. I am not calling these methods, but I assume that since seeds.rb loads the models, it tries to call some of the methods and that's why it fails.
Can anyone tell me a better place to put the require so that it will be included every time ActiveRecord is loaded (not just when the full application is loaded)? I would prefer to keep the code outside of my models, as it is code shared between most of my models and I want to keep them clean and DRY.
Putting the extend there just adds it to ActiveRecord::Base.
When a model class is referenced, via Rails 3.1 autoloading/constant lookup, it will load the class. At that point, it is pure Ruby (nothing magic) as to what happens, basically. So I think you have at least a few options. The "bad" option that kind of does what you want it to hook into dependency loading. Maybe something like:
module ActiveSupport
module Dependencies
alias_method(:load_missing_constant_renamed_my_app_name_here, :load_missing_constant)
undef_method(:load_missing_constant)
def load_missing_constant(from_mod, const_name)
# your include here if const_name = 'ModelName'
# perhaps you could list the app/models directory, put that in an Array, and do some_array.include?(const_name)
load_missing_constant_renamed_my_app_name_here(from_mod, const_name)
end
end
end
Another way to do it would be to use a Railtie like you were doing and add a class method to ActiveRecord::Base that then includes stuff, like:
module MyModule
class Railtie < Rails::Railtie
initializer "my_name.active_record" do
ActiveSupport.on_load(:active_record) do
# ActiveRecord::Base gets new behavior
include ::MyModule::Something # where you add behavior. consider using an ActiveSupport::Concern
end
end
end
end
If using an ActiveSupport::Concern:
module MyModule
module Something
extend ActiveSupport::Concern
included do
# this area is basically for anything other than class and instance methods
# add class_attribute's, etc.
end
module ClassMethods
# class method definitions go here
def include_some_goodness_in_the_model
# include or extend a module
end
end
# instance method definitions go here
end
end
Then in each model:
class MyModel < ActiveRecord::Base
include_some_goodness_in_the_model
#...
end
However, that isn't much better than just doing an include in each model, which is what I'd recommend.

generators and migrations in plugins (rails 3)

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

Resources