Socket
Book a DemoInstallSign in
Socket

auto-seeding

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

auto-seeding

0.1.5
bundlerRubygems
Version published
Maintainers
1
Created
Source

Auto-seeding Gem Version Build Status

A component to auto generate seed data with ActiveRecord using a set of predefined or custom rules respecting models validations.

Install

  • Add to Gemfile: gem 'auto-seeding' (better in development group)
  • Edit the seed task:
auto_seeding = AutoSeeding::Seeder.new
3.times.each do
  auto_seeding.update( Author.new ).save!
end

Options

  • conf/seeder: seeding source [nil | :faker | :ffaker] (:faker requires Faker gem, :ffaker requires FFaker gem)
  • conf/file: load seed configuration from a local file
  • auto_create: array of nested associations to create while seeding (useful for has_one associations), ex. [:profile]
  • ignore_attrs: ignore some attributes, ex. [:id, updated_at]
  • skip_associations: array of nested associations to skip while seeding, ex. [:posts]
  • sources: configure sources rules for autoseed data

Conf file: see data folder

Global options (shared between instances):

AutoSeeding::Seeder.config({
  skip_associations: [:versions],
  conf: {
    seeder: :ffaker,
  },
})

Instance options:

autoseeding = AutoSeeding::Seeder.new({
  auto_create: [:profile],       # array of symbols
  conf: {
    file: 'test/conf.yml',       # string
    seeder: :faker,              # symbol - :faker or :ffaker
  },
  ignore_attrs: [:id],           # array of symbols - ignored attributes
  skip_associations: [:author],  # array of symbols - ignored nested associations
  sources: {                     # hash - keys: types, fields
    types: {                     # hash - override basic types rules
      integer: {
        source_model: 'Random',
        source_method: 'rand',
        source_args: '0..100',
      }
    },
    fields: [                    # array of hashes - override fields rules
      {
        in: ['name'],
        source_model: 'Faker::StarWars',
        source_method: 'character',
        type: 'string'
      },
      {
        regexp: '^(.+_|)title(|_.+)$',
        source_model: 'Faker::Book',
        source_method: 'title',
        post_process: '->( val ) { val + " (seeding)" }',
        type: 'string'
      }
    ]
  }
})

Notes

Generated data can be manipulated easily before saving:

obj = auto_seeding.update( Author.new )
obj.name = 'John Doe'
obj.save!

Field names can be changed using append and prepend options - example using Carrierwave remote url property:

AutoSeeding::Seeder.new({
  sources: {
    fields: [
      {
        regexp: '^(.+_|)photo(|_.+)$|^(.+_|)image(|_.+)$',
        source_model: 'Faker::Avatar',
        source_method: 'image',
        prepend: 'remote_',
        append: '_url',
        type: 'string'
      }
    ]
  }
}

To avoid problems with PaperTrail use:

AutoSeeding::Seeder.config({ skip_associations: [:versions] })

Do you like it? Star it!

If you use this component just star it. A developer is more motivated to improve a project when there is some interest.

Contributors

License

MIT

FAQs

Package last updated on 30 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.