|
1 | 1 | # Insertion |
2 | 2 |
|
3 | | -TODO: Delete this and the text below, and describe your gem |
4 | | - |
5 | | -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/insertion`. To experiment with that code, run `bin/console` for an interactive prompt. |
| 3 | +PORO fixtures for Rails. A simple, fast way to create test data using Plain Old Ruby Objects. |
6 | 4 |
|
7 | 5 | ## Installation |
8 | 6 |
|
9 | | -TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. |
| 7 | +Add to your application's Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +gem 'insertion' |
| 11 | +``` |
10 | 12 |
|
11 | | -Install the gem and add to the application's Gemfile by executing: |
| 13 | +Then run: |
12 | 14 |
|
13 | 15 | ```bash |
14 | | -bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG |
| 16 | +bundle install |
15 | 17 | ``` |
16 | 18 |
|
17 | | -If bundler is not being used to manage dependencies, install the gem by executing: |
| 19 | +For convenience, include the Insertion methods in your test setup. For RSpec, add this to your `spec_helper.rb` or `test_helper.rb`: |
18 | 20 |
|
19 | | -```bash |
20 | | -gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG |
| 21 | +```ruby |
| 22 | +module ActiveSupport |
| 23 | + class TestCase |
| 24 | + include Insertion |
| 25 | + # ... |
| 26 | + end |
| 27 | +end |
21 | 28 | ``` |
22 | 29 |
|
| 30 | +Then you can simply call `insert` and `build` in your tests. But of course, you can also call them directly via `Insertion.insert` and `Insertion.build`. |
| 31 | + |
23 | 32 | ## Usage |
24 | 33 |
|
25 | | -TODO: Write usage instructions here |
| 34 | +### Basic Usage |
| 35 | + |
| 36 | +Create records directly in your tests: |
| 37 | + |
| 38 | +```ruby |
| 39 | +user = insert(:user, name: 'Joel', email: 'joel@example.com') |
| 40 | +``` |
| 41 | + |
| 42 | +Build a record without persisting (useful for getting default values): |
| 43 | + |
| 44 | +```ruby |
| 45 | +user = build(:user, name: 'Joel') |
| 46 | +``` |
| 47 | + |
| 48 | +### Custom Insert Classes |
| 49 | + |
| 50 | +For more control, create custom Insert classes in `app/inserts/`: |
| 51 | + |
| 52 | +```ruby |
| 53 | +# app/inserts/user_insert.rb |
| 54 | +class UserInsert < Insertion::Insert |
| 55 | + def attributes |
| 56 | + { |
| 57 | + name: 'Default Name', |
| 58 | + email: "user#{SecureRandom.hex(4)}@example.com", |
| 59 | + **super |
| 60 | + } |
| 61 | + end |
| 62 | + |
| 63 | + def after_insert(record) |
| 64 | + # Run code after the record is inserted |
| 65 | + end |
| 66 | + |
| 67 | + def after_build(record) |
| 68 | + # Run code after the record is built |
| 69 | + end |
| 70 | +end |
| 71 | +``` |
| 72 | + |
| 73 | +Then use it: |
| 74 | + |
| 75 | +```ruby |
| 76 | +# Uses defaults from UserInsert |
| 77 | +user = insert(:user) |
| 78 | + |
| 79 | +# Override specific attributes |
| 80 | +user = insert(:user, name: 'Custom Name') |
| 81 | +``` |
| 82 | + |
| 83 | +### Nested Inserts |
| 84 | + |
| 85 | +Create associated records within your Insert classes: |
| 86 | + |
| 87 | +```ruby |
| 88 | +class PostInsert < Insertion::Insert |
| 89 | + def attributes |
| 90 | + { |
| 91 | + title: 'Default Title', |
| 92 | + user_id: insert(:user).id, |
| 93 | + **super |
| 94 | + } |
| 95 | + end |
| 96 | +end |
| 97 | +``` |
| 98 | + |
| 99 | +## Why Insertion? |
26 | 100 |
|
27 | | -## Development |
| 101 | +- **Fast**: Uses `insert!` to write directly to the database, bypassing model callbacks and validations |
| 102 | +- **Simple**: Plain Ruby classes with no DSL to learn |
| 103 | +- **Flexible**: Override any attribute at call time |
28 | 104 |
|
29 | | -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 105 | +## Requirements |
30 | 106 |
|
31 | | -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 107 | +- Ruby >= 3.3.0 |
| 108 | +- Rails >= 7.2.0 |
32 | 109 |
|
33 | 110 | ## Contributing |
34 | 111 |
|
|
0 commit comments