module JsonVoorhees class AppEnvironmentGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def sprint createDev createTest createPro addToApplication end private def addToApplication inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<-'RUBY' #So url_for works in the mailer config.action_mailer.default_url_options = { host: 'localhost:3000' } #config.middleware.insert_before "ActionDispatch::Static", "Rack::Cors" do config.middleware.use Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options] end end RUBY end end def createDev gsub_file "config/environments/development.rb", "config.action_mailer.raise_delivery_errors = false\n", "#config.action_mailer.raise_delivery_errors = false\n" inject_into_file 'config/environments/development.rb', after: "configure do\n" do <<-'RUBY' config.action_mailer.perform_deliveries = false config.action_mailer.raise_delivery_errors = false config.action_mailer.default_options = {from: ENV['GMAIL_USERNAME']} config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'localhost:3000', user_name: ENV['GMAIL_USERNAME'], password: ENV['GMAIL_PASSWORD'], authentication: 'plain', enable_starttls_auto: true } RUBY end end def createTest gsub_file "config/environments/test.rb", "config.action_mailer.delivery_method = :test\n", "#config.action_mailer.delivery_method = :test\n" inject_into_file 'config/environments/test.rb', after: "configure do\n" do <<-'RUBY' #config.action_mailer.perform_deliveries = false config.action_mailer.raise_delivery_errors = true config.action_mailer.default_options = {from: 'testing123@example.com'} # Emails get sent here ::ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'localhost:3000', user_name: 'testing123', password: 'testing123', authentication: 'plain', enable_starttls_auto: true } RUBY end end def createPro inject_into_file 'config/environments/production.rb', after: "configure do\n" do <<-'RUBY' config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.default_options = {from: ENV['GMAIL_USERNAME']} config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'example.com', user_name: ENV['GMAIL_USERNAME'], password: ENV['GMAIL_PASSWORD'], authentication: 'plain', enable_starttls_auto: true } RUBY end end end end