Class: Cable::Generators::ResourceGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/cable/resource/resource_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/generators/cable/resource/resource_generator.rb', line 61

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



33
34
35
36
37
# File 'lib/generators/cable/resource/resource_generator.rb', line 33

def create_controller_file
  if options.controller?
    template 'controller.rb' , "app/controllers/admin/#{file_name.pluralize}_controller.rb" if yes?("Would you like to generate a controller?".color(:yellow))
  end
end

#create_migration_fileObject



25
26
27
# File 'lib/generators/cable/resource/resource_generator.rb', line 25

def create_migration_file
   migration_template 'migration.rb', "db/migrate/create_#{table_name}.rb" if options.migration? and yes?("Would you like to generate a migration?".color(:yellow))
end

#create_model_fileObject



29
30
31
# File 'lib/generators/cable/resource/resource_generator.rb', line 29

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

#create_scaffoldObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/cable/resource/resource_generator.rb', line 39

def create_scaffold
  if options.views?
    if yes?("Would you like Cable to generate views for #{model_name.capitalize}?".color(:yellow))
      if attributes.empty? and yes?("\tWould you like to use existing attributes?".color(:green))
        self.attributes = model_name.classify.constantize.columns_hash.delete_if{|x| ["id", "created_at", "updated_at"].include? x }.collect{|c| [c.first,c.second.type]}.collect do |attri|
          Rails::Generators::GeneratedAttribute.new( attri.first, attri.second )
        end
      end
      template 'erb/scaffold/_form.html.erb',       "app/views/admin/#{plural_table_name}/_#{singular_table_name}.html.erb"
      template 'erb/scaffold/index.html.erb',       "app/views/admin/#{plural_table_name}/index.html.erb"
      template 'erb/scaffold/edit.html.erb',        "app/views/admin/#{plural_table_name}/edit.html.erb"
      template 'erb/scaffold/new.html.erb',         "app/views/admin/#{plural_table_name}/new.html.erb"
      template 'erb/scaffold/show.html.erb',        "app/views/admin/#{plural_table_name}/show.html.erb"
      template 'erb/scaffold/_model_row.html.erb',  "app/views/admin/#{plural_table_name}/_#{singular_table_name}_row.html.erb"
    end
  end
end

#install_routeObject



57
58
59
# File 'lib/generators/cable/resource/resource_generator.rb', line 57

def install_route
  route("cable_to :#{plural_table_name}") if options.routes? and yes?("Would you like to generate routes for #{model_name.capitalize}?".color(:yellow))
end