ruby Rails应用程序生成器调整了我的共同需求
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Rails应用程序生成器调整了我的共同需求相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env ruby
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'
require "active_support/core_ext/string/strip"
class CleverAppGenerator < Rails::Generators::AppGenerator
class_option :skip_bundle, default: true
class_option :database, default: "postgresql"
class_option :skip_test_unit, default: true, hide: true
def self.source_root(path=nil)
Rails::Generators::AppGenerator.source_root(path)
end
protected
def self.banner
"./clever-rails #{self.arguments.map(&:usage).join(' ')} [options]"
end
def app_secret
"APP_SECRET"
end
end
class AppBuilder < Rails::AppBuilder
def readme
copy_file "README", "README.md"
end
def gitignore
super
@generator.append_file '.gitignore', <<-DOC.strip_heredoc
/.env
/config/database.yml
DOC
end
def database_yml
template "config/databases/#{options[:database]}.yml", "config/database.example.yml"
template "config/databases/#{options[:database]}.yml", "config/database.yml"
end
def lib
empty_directory "lib"
empty_directory_with_gitkeep "lib/tasks"
empty_directory_with_gitkeep "lib/assets"
end
def public_directory
super
remove_file "public/index.html"
remove_file 'app/assets/images/rails.png'
empty_directory_with_gitkeep "app/assets/images"
end
def gemfile
super
@generator.append_file 'Gemfile', <<-DOC.strip_heredoc
gem "bootstrap-sass"
gem "pjax_rails"
gem "thin"
gem "foreman"
group :development, :test do
gem "rspec-rails"
end
DOC
end
def rspec
say("Installing rspec from generator", :yellow)
system('rails generate rspec:install')
end
def rake
say("Creating and migrating initial database", :yellow)
system "rake db:create db:migrate"
end
def leftovers
foreman_setup
app_secret_from_env
swap_pg_username
update_application_config
update_boot_rb
rspec
bootstrap_sass
rake
end
def foreman_setup
create_file 'Procfile', <<-DOC.strip_heredoc
web: bundle exec rails server thin -p $PORT
DOC
create_file '.env', <<-DOC.strip_heredoc
PORT=3000
RAILS_SECRET_TOKEN=#{SecureRandom.hex(64)}
DOC
end
def app_secret_from_env
gsub_file("config/initializers/secret_token.rb", "'APP_SECRET'", "ENV['RAILS_SECRET_TOKEN']")
end
def swap_pg_username
%w[config/database.yml config/database.example.yml].each do |file|
gsub_file(file, /^ username: .*$/, ' username: postgres')
end
end
def update_application_config
inject_into_class("config/application.rb", "Application") do
<<-DOC
config.assets.initialize_on_precompile = false
config.autoload_paths += %W(\#{config.root}/lib)
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.generators do |g|
g.view_specs false
g.routing_specs false
g.stylesheets false
g.javascripts false
g.helper false
end
DOC
end
end
def update_boot_rb
prepend_to_file("config/boot.rb") do
<<-DOC.strip_heredoc
# Don't buffer stdout
$stdout.sync = true
DOC
end
end
def bootstrap_sass
gsub_file("app/assets/javascripts/application.js", "//= require_tree .") do
<<-DOC.strip_heredoc
//= require bootstrap
//= require_tree .
DOC
end
remove_file("app/assets/stylesheets/application.css")
create_file("app/assets/stylesheets/application.sass") do
'@import "bootstrap";'
end
end
end
CleverAppGenerator.start
以上是关于ruby Rails应用程序生成器调整了我的共同需求的主要内容,如果未能解决你的问题,请参考以下文章
无法在 gem Devise 中调整控制器,ruby on rails 4
Ruby/Rails-我的复习参考,因为我忘了。。。创建项目生成scaf