require 'rails/generators' require 'rails/generators/migration' require 'rails/generators/active_model' module Cable module Generators class MenuGenerator < Rails::Generators::NamedBase include Rails::Generators::Migration # include Rails::Generators::ActiveModel source_root File.expand_path("../templates", __FILE__) desc "Generates a Cable Menu with the given NAME (if one does not exist) plus a migration file" # argument :model_name, :type => :string, :default => "menu" argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" class_option :controller, :type => :boolean, :default => true class_option :model, :type => :boolean, :default => true class_option :migration, :type => :boolean, :default => true class_option :views, :type => :boolean, :default => true class_option :routes, :type => :boolean, :default => true def create_migration_file if options.migration? migration_template 'migration.rb', "db/migrate/create_#{model_name.pluralize}.rb" if yes?("Would you like to generate a migration?") end end def create_model_file if options.model? template 'model.rb' , "app/models/#{model_name}.rb" if yes?("Would you like to generate a model?") end end def create_controller_file if options.controller? template 'controller.rb', "app/controllers/admin/#{model_name.pluralize}_controller.rb" if yes?("Would you like to generate a controller?") end end def create_views if options.views? if yes?( "Would you like Cable to generate menu views?") Dir.glob(File.expand_path("../templates", __FILE__) + '/erb/menus/*.erb') do |rb_file| template rb_file, "app/views/admin/#{plural_table_name}/#{File.basename(rb_file)}" end copy_file 'erb/partials/_menu_fields.html.erb', 'app/views/admin/partials/_menu_fields.html.erb' end end end def create_routes route_string = < :migration # hook_for :orm, :as => :model end end end