Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying model class inline in seed file. #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
appraise "active_record-3" do
gem "sqlite3", "~> 1.3.8"
gem "test-unit" # Required for Travis-CI.
end

appraise "active_record-4" do
Expand All @@ -9,6 +10,7 @@ end

appraise "mongoid-3" do
gem "mongoid", "~> 3.1.6"
gem "test-unit" # Required for Travis-CI.
end

appraise "mongoid-4" do
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sprig (0.1.6)
sprig (0.1.7)

GEM
remote: https://rubygems.org/
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

Seed Rails application by convention, not configuration.

Tested against the following adapters:
- ActiveRecord 3 (SQLite)
- ActiveRecord 4 (SQLite)
- Mongoid 3
- Mongoid 4

Provides support for common files types: *csv*, *yaml*, and *json*. Extensible for the rest!

Learn more about Sprig and view documentation at [http://vigetlabs.github.io/sprig/](http://vigetlabs.github.io/sprig/).
Expand Down Expand Up @@ -129,6 +135,12 @@ options:
find_existing_by: ['title', 'user_id']
```

#### reserved_class_name_attribute:

The reserved attribute name for specifying a different class for that seed (see Setting The Record Class, below).

The default value is 'class_name'. This can be set to any string (e.g. 'klass' or 'type'), or to `null` to disable this feature for all seeds in the current document.

### Computed Values

It's common to want seed values that are dynamic. Sprig supports an ERB style syntax for computing seed attributes.
Expand All @@ -142,6 +154,26 @@ records:
published_at: "<%= 1.week.ago %>"
```

### Setting The Record Class

Using Single-Table Inheritance? Want to keep your posts and comments in the same file? Sprig supports specifying the class used to seed each record by specifying the 'class_name' key.

```yaml
# posts.yml

records:
- sprig_id: 1
body: "This is a sample Post"
- class_name: 'Comment'
post_id: <%= sprig_record(Post, 1).id %>
body: "This is a Comment on the sample Post"
- class_name: GuestPost
body: "This is a Guest Post, which has special behavior"
author_id: <%= sprig_record(User, 2).id %>
```

If 'class_name' doesn't fit your source data, you can change the reserved attribute key by setting the 'reserved_class_name_attribute' option (see Special Options, above).

##Custom Sources and Parsers

If all your data is in `.wat` files, fear not. You can tell Sprig where to look for your data, and point it toward a custom parser class for turning your data into records. The example below tells Sprig to read `User` seed data from a Google Spreadsheet, and parse it accordingly.
Expand Down
1 change: 1 addition & 0 deletions gemfiles/active_record_3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
source "https://rubygems.org"

gem "sqlite3", "~> 1.3.8"
gem "test-unit"

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/mongoid_3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
source "https://rubygems.org"

gem "mongoid", "~> 3.1.6"
gem "test-unit"

gemspec :path => "../"
11 changes: 10 additions & 1 deletion lib/sprig/seed/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ def initialize(klass, datasource, options)
end

def add_seeds_to_hopper(hopper)
reserved_class_name_attribute = datasource.options.fetch('reserved_class_name_attribute', 'class_name')

datasource.records.each do |record_data|
hopper << Entry.new(klass, record_data, options)
record_attrs, record_klass = record_data, klass

if reserved_class_name_attribute && record_data.key?(reserved_class_name_attribute)
record_attrs = record_attrs.dup
record_klass = record_attrs.delete(reserved_class_name_attribute).to_s.constantize
end

hopper << Entry.new(record_klass, record_attrs, options)
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/seeds/test/posts_and_comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
records:
- sprig_id: 1
title: 'Yaml title'
content: 'Yaml content'
- class_name: 'Comment'
post_id: <%= sprig_record(Post, 1).id %>
body: 'Comment body'
10 changes: 10 additions & 0 deletions spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
options:
reserved_class_name_attribute: 'klass'

records:
- sprig_id: 1
title: 'Yaml title'
content: 'Yaml content'
- klass: 'Comment'
post_id: <%= sprig_record(Post, 1).id %>
body: 'Comment body'
52 changes: 52 additions & 0 deletions spec/sprig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,58 @@
end
end

context "with the class specified in the entry" do
describe "with a yaml file" do
around do |example|
load_seeds('posts_and_comments.yml', &example)
end

it "seeds the db" do
sprig [
{
:class => Post,
:source => open('spec/fixtures/seeds/test/posts_and_comments.yml')
}
]

post, comment = Post.first, Comment.first

expect(Post.count).to be == 1
expect(Comment.count).to be == 1

expect(post.title).to be == 'Yaml title'
expect(comment.body).to be == 'Comment body'
expect(comment.post_id).to be == post.id
end
end

describe "with a yaml file with custom class_name attribute" do
describe "with a yaml file" do
around do |example|
load_seeds('posts_and_comments_with_custom_class.yml', &example)
end

it "seeds the db" do
sprig [
{
:class => Post,
:source => open('spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml')
}
]

post, comment = Post.first, Comment.first

expect(Post.count).to be == 1
expect(Comment.count).to be == 1

expect(post.title).to be == 'Yaml title'
expect(comment.body).to be == 'Comment body'
expect(comment.post_id).to be == post.id
end
end
end
end

context "from a specific environment" do
around do |example|
stub_rails_env 'staging'
Expand Down