From c729bb930a542720d62eca40c44cb9b5c0e869b1 Mon Sep 17 00:00:00 2001 From: sleepingkingstudios Date: Sun, 22 Mar 2015 18:34:21 -0400 Subject: [PATCH 1/3] Add support for specifying model class inline in seed file. --- Gemfile.lock | 2 +- README.md | 22 ++++++++ lib/sprig/seed/factory.rb | 11 +++- .../seeds/test/posts_and_comments.yml | 7 +++ .../posts_and_comments_with_custom_class.yml | 10 ++++ spec/sprig_spec.rb | 52 +++++++++++++++++++ 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/seeds/test/posts_and_comments.yml create mode 100644 spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml diff --git a/Gemfile.lock b/Gemfile.lock index b6de813..9973f5e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - sprig (0.1.6) + sprig (0.1.7) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index d95580f..7c05e9e 100644 --- a/README.md +++ b/README.md @@ -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/). @@ -142,6 +148,22 @@ records: published_at: "<%= 1.week.ago %>" ``` +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 %> +``` + ##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. diff --git a/lib/sprig/seed/factory.rb b/lib/sprig/seed/factory.rb index 25ed43d..1d66857 100644 --- a/lib/sprig/seed/factory.rb +++ b/lib/sprig/seed/factory.rb @@ -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 diff --git a/spec/fixtures/seeds/test/posts_and_comments.yml b/spec/fixtures/seeds/test/posts_and_comments.yml new file mode 100644 index 0000000..d3ee56d --- /dev/null +++ b/spec/fixtures/seeds/test/posts_and_comments.yml @@ -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' diff --git a/spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml b/spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml new file mode 100644 index 0000000..e98e963 --- /dev/null +++ b/spec/fixtures/seeds/test/posts_and_comments_with_custom_class.yml @@ -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' diff --git a/spec/sprig_spec.rb b/spec/sprig_spec.rb index f76c4cc..f804f43 100644 --- a/spec/sprig_spec.rb +++ b/spec/sprig_spec.rb @@ -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' From ad26ba5d133cce55db6b5591354c674b05b30b1c Mon Sep 17 00:00:00 2001 From: sleepingkingstudios Date: Sun, 22 Mar 2015 18:50:07 -0400 Subject: [PATCH 2/3] Add test-unit to Rails 3-era gemfiles for Travis-CI. --- Appraisals | 2 ++ gemfiles/active_record_3.gemfile | 1 + gemfiles/mongoid_3.gemfile | 1 + 3 files changed, 4 insertions(+) diff --git a/Appraisals b/Appraisals index d41f6fa..42f8843 100644 --- a/Appraisals +++ b/Appraisals @@ -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 @@ -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 diff --git a/gemfiles/active_record_3.gemfile b/gemfiles/active_record_3.gemfile index 120971e..2eff6e6 100644 --- a/gemfiles/active_record_3.gemfile +++ b/gemfiles/active_record_3.gemfile @@ -3,5 +3,6 @@ source "https://rubygems.org" gem "sqlite3", "~> 1.3.8" +gem "test-unit" gemspec :path => "../" diff --git a/gemfiles/mongoid_3.gemfile b/gemfiles/mongoid_3.gemfile index b0f6e9e..e95c98a 100644 --- a/gemfiles/mongoid_3.gemfile +++ b/gemfiles/mongoid_3.gemfile @@ -3,5 +3,6 @@ source "https://rubygems.org" gem "mongoid", "~> 3.1.6" +gem "test-unit" gemspec :path => "../" From cdfd692c2f48705dc698d543ed54766c009c129b Mon Sep 17 00:00:00 2001 From: sleepingkingstudios Date: Sun, 22 Mar 2015 19:16:33 -0400 Subject: [PATCH 3/3] Update README. --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c05e9e..620c44a 100644 --- a/README.md +++ b/README.md @@ -135,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. @@ -148,7 +154,9 @@ records: published_at: "<%= 1.week.ago %>" ``` -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. +### 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 @@ -164,6 +172,8 @@ records: 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.