Git Product home page Git Product logo

temping's Introduction

Temping

Description

Temping allows you to create arbitrary ActiveRecord models backed by a temporary SQL table for use in tests. You may need to do something like this if you're testing a module that is meant to be mixed into ActiveReord models without relaying on a concrete class.

Temping will use your existing database connection. If one does not exist, it will create and connect to an in-memory SQLite3 instance.

As we're using temporary tables all data will be dropped when the database connection is terminated.

Examples

The basic setup of a model involves calling create with a symbol that represents the class name of the model you wish to create. By default, this will create a temporary table with an id column.

Temping.create :dog

Dog.create => #<Dog id: 1>
Dog.table_name => "dogs"
Dog => Dog(id: integer)

Additional database columns can be specified via the with_columns method which uses Rails migration syntax:

Temping.create :dog do
  with_columns do |t|
    t.string :name
    t.integer :age, :weight
  end
end

Dog.create => #<Dog id: 1, name: nil, age: nil, weight: nil>

When a block is passed to create, it is evaluated in the context of the class. This means anything you do in an ActiveRecord model class body can be accomplished in the block including method definitions, validations, module includes, etc.

Temping.create :dog do
  validates :name, presence: true

  include Duckish

  with_columns do |t|
    t.string :name
    t.integer :age, :weight
  end

  def quack
    "arf!"
  end
end

Dog.create! => ActiveRecord::RecordInvalid: Validation failed: Name can"t be blank

codey = Dog.create! name: "Codey"
codey.quack => "arf!"

Installation

In your Gemfile:

gem "temping"

Bugs, Features, Feedback

Tickets can be submitted by via GitHub issues.

temping's People

Contributors

jpignata avatar awebneck avatar

Watchers

James Cloos avatar Wayne Conrad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.