Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 155 lines (121 sloc) 3.85 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
require 'rubygems'

require 'rake'
require 'yaml'
require 'pg'
require 'active_record'
require 'benchmark'

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/spec')

task :default do
  Rake::Task["db:setup"].invoke
  Rake::Task["test"].invoke
end

desc "Fire up an interactive terminal to play with"
task :console do
  require 'pry'
  require File.expand_path(File.dirname(__FILE__) + '/lib/texticle')

  config = YAML.load_file File.expand_path(File.dirname(__FILE__) + '/spec/config.yml')
  ActiveRecord::Base.establish_connection config.merge(:adapter => :postgresql)

  class Character < ActiveRecord::Base
    belongs_to :web_comic
  end

  class WebComic < ActiveRecord::Base
    has_many :characters
  end

  class Game < ActiveRecord::Base
  end

  # add ability to reload console
  def reload
    reload_msg = '# Reloading the console...'
    puts CodeRay.scan(reload_msg, :ruby).term
    Pry.save_history
    exec('rake console')
  end

  # start the console! :-)
  welcome = <<-EOS
Welcome to the Texticle devloper console. You have some classes you can play with:

class Character < ActiveRecord::Base
# string :name
# string :description
# integer :web_comic_id

belongs_to :web_comic
end

class WebComic < ActiveRecord::Base
# string :name
# string :author
# integer :id

has_many :characters
end

class Game < ActiveRecord::Base
# string :system
# string :title
# text :description
end
EOS

  puts CodeRay.scan(welcome, :ruby).term
  Pry.start
end

task :test do
  require 'texticle_spec'
  require 'texticle/searchable_spec'
  require 'texticle/full_text_indexer_spec'
end

namespace :db do
  desc 'Create and configure the test database'
  task :setup do
    spec_directory = "#{File.expand_path(File.dirname(__FILE__))}/spec"

    STDOUT.puts "Detecting database configuration..."

    if File.exists?("#{spec_directory}/config.yml")
      STDOUT.puts "Configuration detected. Skipping confguration."
    else
      STDOUT.puts "Would you like to create and configure the test database? y/N"
      continue = STDIN.gets.chomp

      unless continue =~ /^[y]$/i
        STDOUT.puts "Done."
        exit 0
      end

      STDOUT.puts "Creating database..."
      `createdb texticle`

      STDOUT.puts "Writing configuration file..."

      config_example = File.read("#{spec_directory}/config.yml.example")

      File.open("#{spec_directory}/config.yml", "w") do |config|
        config << config_example.sub(/<username>/, `whoami`.chomp)
      end

      STDOUT.puts "Running migrations..."
      Rake::Task["db:migrate"].invoke

      STDOUT.puts 'Done.'
    end
  end

  desc 'Run migrations for test database'
  task :migrate do
    config = YAML.load_file File.expand_path(File.dirname(__FILE__) + '/spec/config.yml')
    ActiveRecord::Base.establish_connection config.merge(:adapter => :postgresql)

    ActiveRecord::Migration.instance_eval do
      create_table :games do |table|
        table.string :system
        table.string :title
        table.text :description
      end
      create_table :web_comics do |table|

        table.string :name
        table.string :author
        table.text :review
        table.integer :id
      end

      create_table :characters do |table|
        table.string :name
        table.string :description
        table.integer :web_comic_id
      end
    end
  end

  desc 'Drop tables from test database'
  task :drop do
    config = YAML.load_file File.expand_path(File.dirname(__FILE__) + '/spec/config.yml')
    ActiveRecord::Base.establish_connection config.merge(:adapter => :postgresql)

    ActiveRecord::Migration.instance_eval do
      drop_table :games
      drop_table :web_comics
      drop_table :characters
    end
  end
end
Something went wrong with that request. Please try again.