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



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

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



31
32
33
34
35
# File 'lib/generators/cable/menu/menu_generator.rb', line 31

def create_controller_file
  if options.controller?
    template 'controller.rb', "app/controllers/admin/#{model_name.pluralize}_controller.rb"
  end
end

#create_migration_fileObject



19
20
21
22
23
# File 'lib/generators/cable/menu/menu_generator.rb', line 19

def create_migration_file
  if options.migration?
   migration_template 'migration.rb', "db/migrate/create_#{model_name.pluralize}.rb"
 end
end

#create_model_fileObject



25
26
27
28
29
# File 'lib/generators/cable/menu/menu_generator.rb', line 25

def create_model_file
  if options.model?
   template 'model.rb' , "app/models/#{model_name}.rb"
 end
end

#create_routesObject



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

def create_routes
  
  route_string = "cable_to :\#{plural_table_name} do |menu|\n  collection do\n    get :rebuild\n    get :table\n    post :sort\n    post :move\n    post :remove_resource\n  end\nend\n"
  route( route_string )

end

#create_viewsObject



37
38
39
40
41
42
43
44
# File 'lib/generators/cable/menu/menu_generator.rb', line 37

def create_views
  if options.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