planter 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48ca5902ca8e033fca09dedeed1c7f62efbffc3a347149787b7282030045c740
4
- data.tar.gz: b79c752f9db2e5ab8b7067a0a7c41066e4ad731d7090f861eb38cfd5d73ef8cc
3
+ metadata.gz: 65e5880dfef8ccce0bf8fec627a74ab7ee76c9d0f84379079056d934b9210e58
4
+ data.tar.gz: 2bc7f2d1e88618db03c882291e8dc48b3aa453199f96e235eeab49ec554d0273
5
5
  SHA512:
6
- metadata.gz: 9db7c6dc7a76a834aa21a69423f4e3bf97d637eee7169c32ec75dc773dfda69a622510ba947c969691ab894d4ed9d098222653273caae06e0e00424d9a166df3
7
- data.tar.gz: 6923ec3963d9b6af5c48bc2deb418ab2d9885814e97243501269eb0b6379376457e2dddbc23bd907d00ed75362441bd40563de5349bf71957259ca87244beb34
6
+ metadata.gz: 011cec287611791d0585a80c83e51bd602eafb9bba60755c5079b1eff6f5218ca3a05e21f1651098acda55055590ae4d9ca649f50e3f4559ca7cd971acf647c4
7
+ data.tar.gz: 5a11daf5df589cda533596d9039820c0da140f2c2f44eafb40d33a110cbd3aa7fef0a18c78c8c2f7f8d7a4a6dd8ab1f2e3a4d1a0a7a9f3dee3665fece5ff4fd6
data/README.md CHANGED
@@ -23,7 +23,7 @@ currently a pre-release version, it's recommended to lock it to a specific
23
23
  version, as breaking changes may occur, even at the minor level.
24
24
 
25
25
  ```ruby
26
- gem 'planter', '0.4.0'
26
+ gem 'planter', '0.4.2'
27
27
  ```
28
28
 
29
29
  And then execute:
@@ -67,11 +67,15 @@ Planter.configure do |config|
67
67
 
68
68
  ##
69
69
  # The directory where the seeders are kept.
70
- # config.seeders_directory = 'db/seeds'
70
+ config.seeders_directory = 'db/seeds'
71
71
 
72
72
  ##
73
73
  # The directory where CSVs are kept.
74
- # config.csv_files_directory = 'db/seed_files'
74
+ config.csv_files_directory = 'db/seed_files'
75
+
76
+ ##
77
+ # When true, don't print output when seeding.
78
+ config.quiet = false
75
79
 
76
80
  ##
77
81
  # The default trim mode for ERB. Valid modes are:
@@ -80,7 +84,7 @@ Planter.configure do |config|
80
84
  # '>' omit newline for lines ending in %>
81
85
  # '-' omit blank lines ending in -%>
82
86
  # I recommend reading the help documentation for ERB::new()
83
- # config.erb_trim_mode = nil
87
+ config.erb_trim_mode = nil
84
88
  end
85
89
  ```
86
90
 
@@ -348,6 +352,10 @@ class MyAdapter
348
352
  # Return the attribute used to attach a parent id to the seeded record.
349
353
  end
350
354
 
355
+ def table_columns(model_name:)
356
+ # Return native columns or fields for model_name.
357
+ end
358
+
351
359
  def table_names
352
360
  # Return table or collection names used by `rails generate planter:seeder ALL`.
353
361
  end
@@ -53,6 +53,16 @@ module Planter
53
53
  raise NotImplementedError, "\#{self.class} must implement #foreign_key"
54
54
  end
55
55
 
56
+ ##
57
+ # Return native columns or fields for the model being seeded.
58
+ #
59
+ # @param [String] model_name the model being seeded
60
+ #
61
+ # @return [Array<String>]
62
+ def table_columns(model_name:)
63
+ raise NotImplementedError, "\#{self.class} must implement #table_columns"
64
+ end
65
+
56
66
  ##
57
67
  # Return table or collection names that can have seeders generated.
58
68
  #
@@ -27,11 +27,15 @@ module Planter
27
27
 
28
28
  ##
29
29
  # The directory where the seeders are kept.
30
- # config.seeders_directory = 'db/seeds'
30
+ config.seeders_directory = 'db/seeds'
31
31
 
32
32
  ##
33
33
  # The directory where CSVs are kept.
34
- # config.csv_files_directory = 'db/seed_files'
34
+ config.csv_files_directory = 'db/seed_files'
35
+
36
+ ##
37
+ # When true, don't print output when seeding.
38
+ config.quiet = false
35
39
 
36
40
  ##
37
41
  # The default trim mode for ERB. Valid modes are:
@@ -40,7 +44,7 @@ module Planter
40
44
  # '>' omit newline for lines ending in %>
41
45
  # '-' omit blank lines ending in -%>
42
46
  # I recommend reading the help documentation for ERB::new()
43
- # config.erb_trim_mode = nil
47
+ config.erb_trim_mode = nil
44
48
  end
45
49
  EOF
46
50
  end
@@ -9,6 +9,7 @@ module Planter
9
9
  # - +create_record(model_name:, lookup_attributes:, create_attributes:)+
10
10
  # - +parent_ids(model_name:, parent:)+
11
11
  # - +foreign_key(model_name:, parent:)+
12
+ # - +table_columns(model_name:)+
12
13
  # - +table_names+
13
14
  #
14
15
  # +model_name+ is the configured seeder model name. +parent+ is the
@@ -56,6 +57,16 @@ module Planter
56
57
  association_options(model_name, parent).fetch(:foreign_key, "#{parent}_id")
57
58
  end
58
59
 
60
+ ##
61
+ # Return native table columns for the model being seeded.
62
+ #
63
+ # @param [String] model_name the model being seeded
64
+ #
65
+ # @return [Array<String>]
66
+ def table_columns(model_name:)
67
+ model_name.constantize.column_names
68
+ end
69
+
59
70
  ##
60
71
  # Return application table names that can have seeders generated.
61
72
  #
@@ -268,6 +268,7 @@ module Planter
268
268
  def create_record(record, parent_id: nil)
269
269
  unique, attrs = split_record(apply_transformations(record))
270
270
  unique = unique.merge(foreign_key => parent_id) if parent_id
271
+ unique, attrs = filter_lookup_attributes(unique, attrs)
271
272
  adapter.create_record(
272
273
  model_name: model,
273
274
  lookup_attributes: unique,
@@ -334,6 +335,42 @@ module Planter
334
335
  [u, rec.except(*unique_columns)]
335
336
  end
336
337
 
338
+ def filter_lookup_attributes(lookup_attributes, create_attributes)
339
+ return [lookup_attributes, create_attributes] unless adapter.respond_to?(:table_columns)
340
+
341
+ table_columns = adapter.table_columns(model_name: model).map(&:to_s)
342
+ native_lookup_attributes = lookup_attributes.select do |field, _value|
343
+ table_columns.include?(field.to_s)
344
+ end
345
+ non_column_lookup_attributes = lookup_attributes.except(*native_lookup_attributes.keys)
346
+
347
+ if non_column_lookup_attributes.any?
348
+ warn_non_column_lookup_attributes(non_column_lookup_attributes.keys)
349
+ end
350
+
351
+ if native_lookup_attributes.empty?
352
+ raise "No native lookup columns found for #{model}. " \
353
+ "Add a native table column to the seed data or unique_columns."
354
+ end
355
+
356
+ [
357
+ native_lookup_attributes,
358
+ non_column_lookup_attributes.merge(create_attributes)
359
+ ]
360
+ end
361
+
362
+ def warn_non_column_lookup_attributes(fields)
363
+ warning_key = [model, fields.map(&:to_s).sort]
364
+ @warned_non_column_lookup_attributes ||= []
365
+ return if @warned_non_column_lookup_attributes.include?(warning_key)
366
+
367
+ @warned_non_column_lookup_attributes << warning_key
368
+ warn(
369
+ "WARNING: Planter moved non-column lookup attributes for #{model} " \
370
+ "into create attributes: #{warning_key.last.join(", ")}"
371
+ )
372
+ end
373
+
337
374
  def foreign_key
338
375
  adapter.foreign_key(model_name: model, parent: parent)
339
376
  end
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 0
24
+ PATCH = 2
25
25
 
26
26
  module_function
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray