class Decko::Generators::Deck::DeckGenerator::Interactive

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

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

Public Class Methods

new(destination_root, dev=false) click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 12
def initialize destination_root, dev=false
  @dev = dev
  @destination_root = destination_root
end

Public Instance Methods

run() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 17
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/generators/deck/deck_generator/interactive.rb, line 108
def add_after_seed_options
  @menu["x"][:desc] = "exit"
  @menu["r"] = {
    desc: "run decko server",
    command: "decko server",
    code: proc { bundle_exec "decko server" }
  }
end
add_common_seed_option() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 77
def add_common_seed_option
  @menu["s"] = {
    desc: "seed #{Rails.env}#{' and test' if dev_options?} database",
    command: "decko setup",
    code: proc do
      bundle_exec "rake decko:seed"
      bundle_exec "rake decko:seed", rails_env: "test" if dev_options?
      add_after_seed_options
    end
  }
end
add_config_options() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 59
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 Decko (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/generators/deck/deck_generator/interactive.rb, line 102
def add_exit_option
  @menu["x"] = {
    desc: "exit (run 'decko setup' to complete the installation later)"
  }
end
add_seed_all_option() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 89
def add_seed_all_option
  @menu["a"] = {
    desc: "seed all databases (production, development, and test)",
    command: "decko setup --all",
    code: proc do
      %w[production development test].each do |env|
        bundle_exec "rake decko:seed", rails_env: env
      end
      add_after_seed_options
    end
  }
end
add_seed_options() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 72
def add_seed_options
  add_common_seed_option
  add_seed_all_option
end
build_menu() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 47
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/generators/deck/deck_generator/interactive.rb, line 54
def build_option key, desc, command
  command &&= "#{' ' * (65 - desc.size)}[#{command}]"
  "  #{key} - #{desc}#{command}"
end
bundle_exec(command, opts={}) click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 42
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/generators/deck/deck_generator/interactive.rb, line 38
def config_path file
  File.join destination_root, "config", file
end
dev_options?() click to toggle source
# File lib/generators/deck/deck_generator/interactive.rb, line 34
def dev_options?
  @dev
end