Class: Cable::Generators::MenuGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/cable/menu/menu_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

Implement the required interface for Rails::Generators::Migration. taken from github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb



66
67
68
69
70
71
72
# File 'lib/generators/cable/menu/menu_generator.rb', line 66

def self.next_migration_number(dirname)
 if ActiveRecord::Base.timestamped_migrations
   Time.now.utc.strftime("%Y%m%d%H%M%S")
 else
   "%.3d" % (current_migration_number(dirname) + 1)
 end
end

Instance Method Details

#create_controller_fileObject



34
35
36
37
38
# File 'lib/generators/cable/menu/menu_generator.rb', line 34

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?".color(:yellow))
  end
end

#create_migration_fileObject



22
23
24
25
26
# File 'lib/generators/cable/menu/menu_generator.rb', line 22

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?".color(:yellow))
 end
end

#create_model_fileObject



28
29
30
31
32
# File 'lib/generators/cable/menu/menu_generator.rb', line 28

def create_model_file
  if options.model?
   template 'model.rb' , "app/models/#{model_name}.rb" if yes?("Would you like to generate a model?".color(:yellow))
 end
end

#create_routesObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/cable/menu/menu_generator.rb', line 51

def create_routes
  
  route_string = <<EOF
cable_to :#{plural_table_name} do |menu|
  collection do
    post :sort
  end
end
EOF
  route( route_string ) if yes?("Would you like to generate routes?".color(:yellow))

end

#create_viewsObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/cable/menu/menu_generator.rb', line 40

def create_views
  if options.views?
    if yes?( "Would you like Cable to generate menu views?".color(:yellow))
      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