Project

code_spike

0.0
No commit activity in last 3 years
No release in over 3 years
Down and dirty ActiveRecord code spiking outside of Rails
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

CodeSpike

CodeSpike is a simple Gem for spiking ActiveRecord models. It uses an in memory sqlite3 database and requires all the necessary libs for unit testing. In addition it provides a DSL for creating the database schema as well calling test methods.

I created this Gem because when working on complex ActiveRecord associations, I like to hop outside of Rails to isolate the problem. Setting up ActiveRecord outside of Rails is easy enough, but the setup is a little verbose and I can never quite remember all the libraries or the exact syntax. So I spend time googling or looking back through old code, which I don't like doing if I've already solved the problem.

This Gem just wraps the setup and adds a simple DSL that I can remember, so I can move right into the problem I'm working on!

Installation

gem install code_spike

Usage

require 'code_spike'

schema do
  create_table(:people ){ |t| t.string :name }
end

class Person < ActiveRecord::Base; end

units do
  test "person create" do
    assert Person.create!
  end
end

For comparison, this is how you would do the same thing without this Gem.

require 'test/unit'
require 'active_support/core_ext/class/subclasses'
require 'active_support/test_case'
require 'active_record'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Migration.verbose = false

ActiveRecord::Schema.define do
  create_table(:people ){ |t| t.string :name }
end

class Person < ActiveRecord::Base; end

class CodeSpikeTest < ActiveSupport::TestCase
  teardown { ActiveRecord::Base.subclasses.each { |klass| klass.destroy_all} }

  test "person create" do
    assert Person.create!
  end
end

Using Fixtures

You can also use fixtures to load up seed data for your tests. You have to provide the directory to the YAML files. Here is an example using fixtures.

require 'code_spike'

# In ./companies.yml
#
# one:
#   name: Gristmill
fixtures_directory '.'

schema do
  create_table :companies { |t| t.string :name }
end

class Company < ActiveRecord::Base; end

units do
  test "company" do
    assert_equal "Gristmill", Company.first.name
  end
end