This is my first time making a custom rails generator and I want to be able to create a dynamic class in the template based off the argument passed to the generator but I can’t figure out how and to have it formatted properly.
class Achievements::__FILE__ < Achievement
end
This is the generated class I want to create and below is the generator. Also on a side note, am I creating the directory ‘achievement’ right in my generator?
module Achiever
module Generators
class AchievementGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :award, :type => :string
def generate_achievement
copy_file "achievement.rb", "app/models/achievement/#{file_name}.rb"
end
private
def file_name
award.underscore
end
end
end
end
I figured out the problem with this issue. Instead of the copy_file method I should be using the template method. This then allows me to use erb tags inside the templated view and I can call the file_name.classify inside the views and it will dynamically create the model.
What ever is set here above as the argument will define the class below in the generated model.