croatech / smart_seeds

Gem for smart auto generating of new records with random values for your ruby app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SmartSeeds

Gem for smart auto generating of new records with random values for your ruby app.

It determines types of fields(attributes) of an ActiveRecord model and creates records with random values for each of these attributes.

The gem has simple integration with Faker and supports foreign_keys, enums, etc.

Usage

For example Entity model has name, age, is_human attributes.

All you need to do is run this command: SmartSeeds.plant(Entity)

 > SmartSeeds.plant(Entity)
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "entities" ("name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "Numquam"], ["age", 60246], ["is_human", true], ["created_at", 2017-01-30 15:10:00 UTC], ["updated_at", 2017-01-30 15:10:00 UTC]]
   (0.7ms)  commit transaction
 => #<Entity id: 1, name: "Numquam", age: 1488, is_human: false, created_at: "2017-01-30 15:10:00", updated_at: "2017-01-30 15:10:00"> 

Custom values

If you want to override some default random values, you can send them in a hash:

 > SmartSeeds.plant(Entity, {name: 'Aleah'})
   (0.7ms)  begin transaction
  SQL (1.9ms)  INSERT INTO "entities" ("name", "age", "is_human", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "Aleah"], ["age", 15166], ["is_human", false], ["created_at", 2017-01-30 15:11:16 UTC], ["updated_at", 2017-01-30 15:11:16 UTC]]
   (0.7ms)  commit transaction
 => #<Entity id: 2, name: "Aleah", age: 39, is_human: true, created_at: "2017-01-30 15:11:16", updated_at: "2017-01-30 15:11:16">

By the way, all attributes which has default values on AR side will be skipped by default.

Faker

The gem has a simple integration with Faker.

That means if you have an AR model which called like a some Faker class and an attribute called like a some faker method in that class, a value will be generated by Faker gem.

For example: Model Superhero has name and power attributes, so the value will be generated by Faker::Superhero.name and Faker::Superhero.power. Superhero doc.

> SmartSeeds.plant(Superhero)
   (0.7ms)  begin transaction
  SQL (2.8ms)  INSERT INTO "superheros" ("name", "power", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "Cheetah Skull"], ["power", "Magic"], ["created_at", 2017-01-22 23:33:20 UTC], ["updated_at", 2017-01-22 23:33:20 UTC]]
   (0.9ms)  commit transaction
 => #<Superhero id: 8, name: "Ultra Firebird", power: "Magic", created_at: "2017-01-22 23:33:20", updated_at: "2017-01-22 23:33:20"> 

Installation

Add this line to your application's Gemfile:

gem 'smart_seeds', '~> 0.1.7'

And then execute:

$ bundle

Or install it yourself as:

$ gem install smart_seeds

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.

About

Gem for smart auto generating of new records with random values for your ruby app

License:MIT License


Languages

Language:Ruby 86.4%Language:HTML 10.0%Language:JavaScript 2.2%Language:CSS 1.4%