class Starter::ResourceGenerator
Public Instance Methods
copy_view_files()
click to toggle source
def create_root_view_folder
empty_directory File.join("app/views", controller_file_path)
end
# File lib/generators/starter/resource/resource_generator.rb, line 43 def copy_view_files available_views.each do |view| filename = view_filename_with_extensions(view) template filename, File.join("app/views", controller_file_path, File.basename(filename)) end end
generate_controller()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 20 def generate_controller return if options[:skip_controller] if dry? template 'dried/controller.rb', "app/controllers/#{plural_name.underscore}_controller.rb" else template 'controller.rb', "app/controllers/#{plural_name.underscore}_controller.rb" end end
generate_migration()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 34 def generate_migration return if options[:skip_model] migration_template "migration.rb", "db/migrate/create_#{table_name}.rb" end
generate_model()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 29 def generate_model return if options[:skip_model] template 'model.rb', "app/models/#{singular_name.underscore}.rb" end
generate_routes()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 51 def generate_routes return if options[:skip_controller] if dry? route "resources :#{plural_name}", "Named RESTful routes" elsif named_routes? route golden_7_named, "Named RESTful routes" else route golden_7, "RESTful routes" end end
Protected Instance Methods
attributes_with_index()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 126 def attributes_with_index attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } end
available_views()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 130 def available_views dry? ? %w(index new edit show _form) : %w(index new edit show) end
dry?()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 104 def dry? options[:dry] end
golden_7()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 64 def golden_7 ["# Routes for the #{singular_name.capitalize} resource:", " # CREATE", " get '/#{plural_name}/new', controller: '#{plural_name}', action: 'new'", " post '/#{plural_name}', controller: '#{plural_name}', action: 'create'", "", " # READ", " get '/#{plural_name}', controller: '#{plural_name}', action: 'index'", " get '/#{plural_name}/:id', controller: '#{plural_name}', action: 'show'", "", " # UPDATE", " get '/#{plural_name}/:id/edit', controller: '#{plural_name}', action: 'edit'", " patch '/#{plural_name}/:id', controller: '#{plural_name}', action: 'update'", "", " # DELETE", " delete '/#{plural_name}/:id', controller: '#{plural_name}', action: 'destroy'", " ##{'-' * 30}" ].join("\n") end
golden_7_named()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 84 def golden_7_named ["# Routes for the #{singular_name.capitalize} resource:", " # CREATE", " get '/#{plural_name}/new', controller: '#{plural_name}', action: 'new', as: 'new_#{singular_name}'", " post '/#{plural_name}', controller: '#{plural_name}', action: 'create', as: '#{plural_name}'", "", " # READ", " get '/#{plural_name}', controller: '#{plural_name}', action: 'index'", " get '/#{plural_name}/:id', controller: '#{plural_name}', action: 'show', as: '#{singular_name}'", "", " # UPDATE", " get '/#{plural_name}/:id/edit', controller: '#{plural_name}', action: 'edit', as: 'edit_#{singular_name}'", " patch '/#{plural_name}/:id', controller: '#{plural_name}', action: 'update'", "", " # DELETE", " delete '/#{plural_name}/:id', controller: '#{plural_name}', action: 'destroy'", " ##{'-' * 30}" ].join("\n") end
named_routes?()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 108 def named_routes? options[:named_routes] end
route(routing_code, title)
click to toggle source
Override of Rails::Generators::Actions
# File lib/generators/starter/resource/resource_generator.rb, line 117 def route(routing_code, title) log :route, title sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/ in_root do inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel, :verbose => false } end end
styled?()
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 112 def styled? options[:styled] end
view_filename_with_extensions(name)
click to toggle source
# File lib/generators/starter/resource/resource_generator.rb, line 134 def view_filename_with_extensions(name) filename = [name, :html, :erb].compact.join(".") folders = [] folders << 'dried' if dry? folders << 'bootstrapped' if styled? filename = File.join(folders, filename) if folders.any? return filename end