Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Example rails app template script
- # I included some functions and other things in this file
- # that I would usually keep seperate and just require.
- require 'yaml'
- # I like to use the irb replacement pry to debug scripts like
- # this if you have the pry gem you can uncomment the following line
- #require 'pry'
- # Then place the next (uncommented) line near where you are
- # having issues and when the script is run it will break into
- # a pry session.
- #binding.pry
- ######
- # SETUP
- ######
- # A quick descriptive name for the template
- def template_name
- "Postgres ActiveAdmin setupper"
- end
- #Describe quickly what is going to happen when this template is run
- def desc
- <<-END_DESC
- Assumes that you are using postgres for development, test and production
- and that you have the psql command available.
- Will setup config/database.yml, create postgres databases if necessary.
- Will install ActiveAdmin, and track the AdminUser as an ActiveAdmin resource.
- NOTE: Due to the way templates work you may need to have the required gems installed
- on your system or current rvm gemset before you procede.
- (TODO: add a check to see if necessary gems are installed)
- END_DESC
- end
- ######
- # Some Functions
- # Included here for example purposes(I ususally source from a seperate file)
- ######
- #Ask a question if fallback is true otherwise just return the given default
- def ask_with_default(statement, default, fallback=false)
- if (fallback) then
- default
- else
- say("#{statement} [default: #{default} ]",:BLUE)
- input = $stdin.gets.strip
- input.empty? ? default : input
- end
- end
- #use to report things that have been done or applied (just saves setting the color each time)
- def verbate(statement)
- say(" " + statement,:GREEN)
- end
- #if called with true defaults will used and no questions will be asked.
- def config_database_yml(just_defaults = false)
- default_db_adapter = 'postgresql'
- default_db_username = ENV['USER'] # <= EDIT you may need to change the db username. default is to use shell login name.
- default_db_password = 'PASSWORD' # <= EDIT
- environment_names = %w(development test production)
- create_database_sql = '-------------'
- dbconfig = {}
- environment_names.each do |e|
- say("Configuring Database settings for #{e} environment.",:GREEN)
- say("Generated settings will be placed in db/database.yml")
- dbconfig[e] = {}
- dbconfig[e]['adapter'] = default_db_adapter
- dbconfig[e]['encoding'] = 'unicode'
- dbconfig[e]['pool'] = 5
- dbconfig[e]['database'] = ask_with_default("What is the name for the #{e} database","#{app_name}_#{e}",just_defaults)
- verbate("Using database #{dbconfig[e]['database']} for #{e} environment.")
- dbconfig[e]['username'] = ask_with_default("What is the username for the #{e} environment database (#{dbconfig[e]['database']})?", default_db_username,just_defaults)
- verbate("Using username '#{dbconfig[e]['username']}' for #{e} environment.")
- dbconfig[e]['password'] = ask_with_default("What is the password for user `#{dbconfig[e]['username']}` on database (#{dbconfig[e]['database']})?",default_db_password, just_defaults)
- verbate("Using password `#{dbconfig[e]['password']}` for '#{dbconfig[e]['username']}' for #{e} environment.")
- create_database_sql << <<-SQL_END
- --
- -- {e} Database
- --
- CREATE DATABASE #{dbconfig[e]['database']}
- WITH ENCODING='UTF8'
- OWNER=#{dbconfig[e]['username']}
- CONNECTION LIMIT=-1;
- --Uncomment next line if you want public connect access to DB then re-run this sql.
- --GRANT CONNECT ON DATABASE #{dbconfig[e]['database']} TO public;
- -------------
- SQL_END
- end
- file('db/create_dbs.sql',create_database_sql)
- file 'config/database.yml', dbconfig.to_yaml
- if yes?("Create the postgres databases?") then
- say("Provide your password for psql to create your databases",:magenta)
- inside('db') do
- #Due to the way psql works (on my system at least) an existing database is required to login.
- #The default is a database of the users login name, so I have a database that
- #has the same name as my shell login name, and my postgres username is also
- #my shell login name. You may need to change the -d flag to point to a db that
- #exists on your system, and perhaps add a -U flag followed by your postgresql username
- run "psql -d #{default_db_username} -f create_dbs.sql" # <= EDIT see note above
- end
- end
- end
- #
- # Begin of actual template script
- #
- say("ABOUT TO RUN TEMPLATE: #{template_name} ", :MAGENTA)
- puts desc
- exit unless yes?("Shall I continue?")
- if yes?("Setup PostgresSql Database with defaults?") then
- config_database_yml(true)
- else
- config_database_yml(false)
- end
- say("Adding Gems to Gemfile")
- gem 'pg'
- gem 'thin'
- #also write a simple procfile for thin
- file 'Procfile', <<-THIN_PROC_END
- web: bundle exec rails server thin -p $PORT
- THIN_PROC_END
- gem 'activeadmin'
- gem "meta_search", '>= 1.1.0.pre'
- gem 'sass-rails', " ~> 3.1.0"
- # gem_group :development do
- # gem 'foreman'
- # end
- gem 'foreman'
- git :init
- git :add => "."
- git :commit => "-a -m 'Initial Commit'"
- generate 'active_admin:install'
- git :add => "."
- git :commit => "-a -m 'Install Active Admin'"
- rake 'db:migrate'
- generate 'active_admin:resource', "AdminUser"
- git :add => "."
- git :commit => "-a -m 'Track AdminUser as an ActiveAdmin resource'"
- say("You still need to set up mail delevery system for ActiveAdmin!
- Look at the source of this script
- (#{__FILE__})
- for a quick example provided in the comments", :RED)
- puts
- ask("Done running template. Just wanted you to see the note above ;-). About to let rails finish creating your project.[hit return]")
- # For small production and pre-deploy on larger apps I use sendgrid via the heroku addon to send mail
- # if you have enabled sendgrid for your heroku app you can add the following to
- # config/environments/production.rb
- ################################
- #
- # #This Address will be the host where the link in the email will point
- # # config.action_mailer.default_url_options = { :host => 'MYDOMAIN.EXT' } # <= EDIT
- #
- # #On heroku these ENV variables are setup for us (since we have enabled sendgrid)
- # ActionMailer::Base.smtp_settings = {
- # :user_name => ENV['SENDGRID_USERNAME'],
- # :password => ENV['SENDGRID_PASSWORD'],
- # :domain => ENV['SENDGRID_DOMAIN'],
- # :address => "smtp.sendgrid.net",
- # :port => 587,
- # :authentication => :plain,
- # :enable_starttls_auto => true
- # }
- ##############################
- # To use sendgrid locally (once you have it setup on heroku). I use `heroku config`
- # to see my generated sendgrid variables then place them in a file called local_pass_store (export VAR=VALUE) at the root of my project
- # and then `source local_pass_store` before I start the server or console.
- # config/environments/development.rb
- ################################
- #
- # #This Address will be the host where the link in the email will point
- # config.action_mailer.default_url_options = { :host => 'localhost:5000' }
- #
- # #Note that these variable need to be set in the shell before starting the
- # #local server or console (source local_pass_store)
- # ActionMailer::Base.smtp_settings = {
- # :user_name => ENV['SENDGRID_USERNAME'],
- # :password => ENV['SENDGRID_PASSWORD'],
- # :domain => ENV['SENDGRID_DOMAIN'],
- # :address => "smtp.sendgrid.net",
- # :port => 587,
- # :authentication => :plain,
- # :enable_starttls_auto => true
- # }
- #You also need to edit the config/initializers/devise.rb file and change the line
- # config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
- #to the account you want the mail to look like it was sent from
Add Comment
Please, Sign In to add comment