class WagnGenerator::Interactive

Guides through the wagn deck installation with an interactive menu Offers the possibilitiy to

- edit database config
- edit application.rb
- seed database
- run server

Public Class Methods

new(options, destination_root) click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 9
def initialize options, destination_root
  @dev = options["core-dev"] || options["mod-dev"]
  @destination_root = destination_root
end

Public Instance Methods

run() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 14
def run
  require config_path("application") # need this for Rails.env
  @menu = ActiveSupport::OrderedHash.new
  add_config_options
  add_seed_options
  add_exit_option
  while (answer = ask(build_menu)) != "x"
    if @menu.key? answer
      @menu[answer][:code].call
    else
      puts "invalid choice"
    end
  end
end

Private Instance Methods

add_after_seed_options() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 105
def add_after_seed_options
  @menu["x"][:desc] = "exit"
  @menu["r"] = {
    desc:    "run wagn server",
    command: "wagn server",
    code:    proc { bundle_exec "wagn server" }
  }
end
add_common_seed_option() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 74
def add_common_seed_option
  @menu["s"] = {
    desc: "seed #{Rails.env}#{' and test' if dev_options?} database",
    command: "wagn seed",
    code: proc do
      bundle_exec "rake wagn:seed"
      bundle_exec "rake wagn:seed", rails_env: "test" if dev_options?
      add_after_seed_options
    end
  }
end
add_config_options() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 56
def add_config_options
  @menu["d"] = {
    desc: "edit database configuration file",
    command: "nano config/database.yml",
    code: proc { system "nano #{config_path 'database.yml'}" }
  }
  @menu["c"] = {
    desc: "configure Wagn (e.g. email settings)",
    command: "nano config/application.rb",
    code: proc { system "nano #{config_path 'application.rb'}" }
  }
end
add_exit_option() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 99
def add_exit_option
  @menu["x"] = {
    desc: "exit (run 'wagn seed' to complete the installation later)"
  }
end
add_seed_all_option() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 86
def add_seed_all_option
  @menu["a"] = {
    desc: "seed all databases (production, development, and test)",
    command: "wagn seed --all",
    code: proc do
      %w(production development test).each do |env|
        bundle_exec "rake wagn:seed", rails_env: env
      end
      add_after_seed_options
    end
  }
end
add_seed_options() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 69
def add_seed_options
  add_common_seed_option
  add_seed_all_option
end
build_menu() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 44
def build_menu
  lines = ["What would you like to do next?"]
  lines += @menu.map { |key, v|  build_option key, v[:desc], v[:command] }
  lines << "[#{@menu.keys.join}]"
  "\n#{lines.join("\n")}\n"
end
build_option(key, desc, command) click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 51
def build_option key, desc, command
  command &&= " " * (65 - desc.size) + "[" + command + "]"
  "  #{key} - #{desc}#{command if command}"
end
bundle_exec(command, opts={}) click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 39
def bundle_exec command, opts={}
  rails_env = "RAILS_ENV=#{opts[:rails_env]}" if opts[:rails_env]
  system "cd #{destination_root} && #{rails_env} bundle exec #{command}"
end
config_path(file) click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 35
def config_path file
  File.join destination_root, "config", file
end
dev_options?() click to toggle source
# File lib/wagn/generators/wagn/wagn_generator/interactive.rb, line 31
def dev_options?
  @dev
end