seedie 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +91 -9
  3. data/CHANGELOG.md +123 -0
  4. data/Gemfile +4 -1
  5. data/README.md +77 -3
  6. data/Rakefile +1 -1
  7. data/lib/generators/seedie/install_generator.rb +84 -37
  8. data/lib/generators/seedie/templates/blank_seedie.yml +22 -0
  9. data/lib/generators/seedie/templates/seedie_initializer.rb +8 -0
  10. data/lib/seedie/associations/base_association.rb +35 -31
  11. data/lib/seedie/associations/belongs_to.rb +12 -13
  12. data/lib/seedie/associations/has_and_belongs_to_many.rb +26 -0
  13. data/lib/seedie/associations/has_many.rb +6 -4
  14. data/lib/seedie/associations/has_one.rb +13 -13
  15. data/lib/seedie/configuration.rb +12 -0
  16. data/lib/seedie/field_values/custom_value.rb +14 -83
  17. data/lib/seedie/field_values/fake_value.rb +85 -17
  18. data/lib/seedie/field_values/faker_builder.rb +29 -35
  19. data/lib/seedie/field_values/value_template_validator.rb +91 -0
  20. data/lib/seedie/field_values_set.rb +21 -4
  21. data/lib/seedie/model/creator.rb +7 -5
  22. data/lib/seedie/model/id_generator.rb +10 -8
  23. data/lib/seedie/model/model_sorter.rb +14 -19
  24. data/lib/seedie/model_fields.rb +5 -3
  25. data/lib/seedie/model_seeder.rb +11 -22
  26. data/lib/seedie/polymorphic_association_helper.rb +20 -16
  27. data/lib/seedie/railtie.rb +3 -2
  28. data/lib/seedie/reporters/base_reporter.rb +75 -68
  29. data/lib/seedie/reporters/console_reporter.rb +16 -12
  30. data/lib/seedie/reporters/reportable.rb +14 -10
  31. data/lib/seedie/seeder.rb +5 -3
  32. data/lib/seedie/version.rb +1 -1
  33. data/lib/seedie.rb +21 -27
  34. data/lib/tasks/seedie.rake +4 -2
  35. metadata +32 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dba945f1f8d911d59194de8191ef857885bc79fb3986fe4d159bbf21842758d9
4
- data.tar.gz: 330174e781fa44b766d1fdcc4fbca9b4cc4f549db447913a60e2c1a41a4f0f51
3
+ metadata.gz: 7b22e6527a57385dbb0b5fc616cf632b84550a3ce8237f286a64b931670bd34a
4
+ data.tar.gz: edeb838fb02090e02a9434260e08f6cb376b4b9d1e16a75e7dc3bd97c6d0a14a
5
5
  SHA512:
6
- metadata.gz: 4717460bd7dc315de646b3e839f5d8be92881245638711c790b5f8a8b3dac216d8ab0eccf8c6d3bc974f7c9ecf1f00b8520bbf44f6b112af18e2620a44faee71
7
- data.tar.gz: cc648a856d662822565da6d648f16d5f17da1f9b5f1eca45f0e2748fc8633e98d7018efda18d699af4771315b424fa83d13b397a632716a06d79910ee4ec4ae9
6
+ metadata.gz: 85ae796fa177098ac721addf4e4da3a10f12e26613318fb8f3fe8d126d008ec3de310115b877848fa607a91ad69e51c7c10a531322100050e76e45e89a2f2b72
7
+ data.tar.gz: cb9fe8d4333e9a80f32fc33bab4e8fbe5066832121ab5c49bb3701691e8a07147115819f13ee41e58eb91d90d03a247e6bfe5f5280cc600b4c307c251b0e98fc
data/.rubocop.yml CHANGED
@@ -1,16 +1,98 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
3
- DisabledByDefault: true
4
-
2
+ TargetRubyVersion: 2.7
3
+ NewCops: disable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - "spec/dummy/bin/*"
7
+
8
+ Style/HashSyntax:
9
+ EnforcedStyle: ruby19_no_mixed_keys
10
+ EnforcedShorthandSyntax: never
11
+
12
+ Style/GuardClause:
13
+ Enabled: false
14
+
15
+ # Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
16
+ # SupportedStyles: assign_to_condition, assign_inside_condition
17
+ Style/ConditionalAssignment:
18
+ Enabled: false
19
+
20
+ # Configuration parameters: AllowedConstants.
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ Style/IfUnlessModifier:
25
+ Enabled: false
26
+
27
+ # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
28
+ # SupportedStyles: single_quotes, double_quotes
5
29
  Style/StringLiterals:
6
- Enabled: true
7
30
  EnforcedStyle: double_quotes
31
+ ConsistentQuotesInMultiline: true
8
32
 
9
- Style/StringLiteralsInInterpolation:
10
- Enabled: true
11
- EnforcedStyle: double_quotes
33
+ # Configuration parameters: EnforcedStyle.
34
+ # SupportedStyles: both, prefix, postfix
35
+ Style/NegatedIf:
36
+ Enabled: false
37
+
38
+ Style/RedundantBegin:
39
+ Enabled: false
40
+
41
+ # Configuration parameters: MinDigits, Strict, AllowedNumbers, AllowedPatterns.
42
+ Style/NumericLiterals:
43
+ Exclude:
44
+ - 'spec/dummy/db/*.rb'
12
45
 
46
+ Style/RedundantInterpolation:
47
+ Exclude:
48
+ - 'lib/seedie/reporters/console_reporter.rb'
49
+
50
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
51
+ # URISchemes: http, https
13
52
  Layout/LineLength:
14
- Max: 120
53
+ Max: 150
54
+
55
+ # Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
56
+ # NamePrefix: is_, has_, have_
57
+ # ForbiddenPrefixes: is_, has_, have_
58
+ # AllowedMethods: is_a?
59
+ # MethodDefinitionMacros: define_method, define_singleton_method
60
+ Naming/PredicateName:
15
61
  Exclude:
16
- - "spec/dummy/**/*"
62
+ - 'spec/**/*'
63
+ - 'lib/seedie/field_values/faker_builder.rb'
64
+ - 'lib/seedie/polymorphic_association_helper.rb'
65
+
66
+ Metrics/AbcSize:
67
+ Max: 30
68
+ Exclude:
69
+ - "lib/generators/seedie/install_generator.rb"
70
+
71
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
72
+ # AllowedMethods: refine
73
+ Metrics/BlockLength:
74
+ CountComments: false
75
+ Max: 30
76
+ Exclude:
77
+ - "spec/integration/*.rb"
78
+ - "spec/seedie/**/*.rb"
79
+ - "spec/generators/*.rb"
80
+ - "spec/dummy/**/*.rb"
81
+
82
+ # Configuration parameters: CountComments, CountAsOne.
83
+ Metrics/ClassLength:
84
+ Exclude:
85
+ - "lib/generators/seedie/install_generator.rb"
86
+ - "lib/seedie/field_values/faker_builder.rb"
87
+
88
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
89
+ Metrics/CyclomaticComplexity:
90
+ Max: 17
91
+
92
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
93
+ Metrics/MethodLength:
94
+ Max: 52
95
+
96
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
97
+ Metrics/PerceivedComplexity:
98
+ Max: 15
data/CHANGELOG.md CHANGED
@@ -1,7 +1,130 @@
1
+ ## Version 0.4.0
2
+
3
+ ### New Features
4
+
5
+ #### Added HasAndBelongsToMany Association support
6
+
7
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/43)
8
+
9
+ You can now add `has_and_belongs_to_many` associations to your seedie.yml file:
10
+
11
+ ```yaml
12
+ has_and_belongs_to_many:
13
+ posts:
14
+ count: 5
15
+ attributes:
16
+ title: "{{Faker::Lorem.sentence}}"
17
+ body: "{{Faker::Lorem.paragraph}}"
18
+ ```
19
+
20
+ #### Added the ability to generate nested BelongsTo Associations
21
+
22
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/34)
23
+
24
+ Before, Models had no ability to add assocations to the child_model.
25
+
26
+ ```yaml
27
+ parent_model:
28
+ attributes:
29
+ some_attribute: 'hello'
30
+ associations:
31
+ has_many:
32
+ child_model: 2
33
+ ```
34
+
35
+ Now you can add associations to the child_model:
36
+
37
+ ```yaml
38
+ parent_model:
39
+ attributes:
40
+ some_attribute: 'hello'
41
+ associations:
42
+ has_many:
43
+ child_model:
44
+ attributes:
45
+ title: 'some title'
46
+ associations:
47
+ belongs_to:
48
+ another_parent_model: 'random'
49
+ ```
50
+
51
+ ### Enhancements
52
+
53
+ #### Complete Rubocop Integration with CI
54
+
55
+ * [Rubocop Integration](https://github.com/keshavbiswa/seedie/pull/33) by [@giovannism20](https://github.com/giovannism20)
56
+ * Rubocop cleanups:
57
+ - https://github.com/keshavbiswa/seedie/pull/37
58
+ - https://github.com/keshavbiswa/seedie/pull/38
59
+ - https://github.com/keshavbiswa/seedie/pull/39
60
+ - https://github.com/keshavbiswa/seedie/pull/40
61
+ - https://github.com/keshavbiswa/seedie/pull/41
62
+ - https://github.com/keshavbiswa/seedie/pull/42
63
+
64
+ #### Added Zeitwerk Support
65
+
66
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/35)
67
+
68
+ #### Added Rails 7.1 Support
69
+
70
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/36)
71
+
1
72
  ## Version 0.3.0
2
73
 
3
74
  ### New Features
4
75
 
76
+ #### Generate a Blank Seedie.yml file with `--blank` option
77
+
78
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/20)
79
+
80
+ You can now generate a blank seedie.yml file with the `--blank` option:
81
+
82
+ ```bash
83
+ rails g seedie:install --blank
84
+ ```
85
+
86
+ #### Exclude models from seedie.yml generation with `--excluded_models` option
87
+
88
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/21)
89
+
90
+ You can now exclude models from seedie.yml generation with the `--excluded_models` option:
91
+
92
+ ```bash
93
+ rails g seedie:install --excluded_models Post Comment
94
+ ```
95
+
96
+ #### Generate a Seedie.yml file with only specific models with `--include_only_models` option
97
+
98
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/22)
99
+
100
+ You can now generate a seedie.yml file with only specific models with the `--include_only_models` option:
101
+
102
+ ```bash
103
+ rails g seedie:install --include_only_models Post Comment
104
+ ```
105
+
106
+ #### Bugfix: Now unique indexes will be generated with `unique` instead of `random` pick_strategy
107
+
108
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/23)
109
+
110
+ #### Introducing new Seedie.rb initializer
111
+
112
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/28)
113
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/29)
114
+
115
+ ```ruby
116
+ Seedie.configure do |config|
117
+ # config.default_count = 10
118
+
119
+ config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
120
+ # Add more custom attributes here
121
+ end
122
+ ```
123
+
124
+ ## Version 0.2.0
125
+
126
+ ### New Features
127
+
5
128
  #### Polymorphic Association
6
129
  * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/12)
7
130
 
data/Gemfile CHANGED
@@ -6,10 +6,13 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
- gem "rubocop", "~> 1.21"
10
9
 
11
10
  gem "pry", "~> 0.14.2"
12
11
 
13
12
  group :test do
14
13
  gem "simplecov", "~> 0.22.0", require: false
15
14
  end
15
+
16
+ group :development, :test do
17
+ gem "rubocop", require: false
18
+ end
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Seedie
2
2
 
3
- Seedie is a Ruby gem designed to streamline the seeding of your database.
3
+ Seedie is a Ruby gem designed to make it easy to seed your database with realistic data.
4
4
  Utilizing the Faker library, Seedie generates realistic data for ActiveRecord models.
5
5
  Currently supports only PostrgreSQL and SQLite3 databases.
6
6
  The gem includes a Rake task for seeding models and a Rails generator for easy setup.
@@ -34,8 +34,77 @@ $ rails generate seedie:install
34
34
  ```
35
35
  This will create a seedie.yml file in your config directory, which will include configurations for your models.
36
36
 
37
+ Alternatively, you can also create a blank seedie.yml file by running:
38
+
39
+ ```bash
40
+ $ rails generate seedie:install --blank
41
+ ```
42
+
43
+ This will generate a blank seedie.yml config file for you that you can now customize according to your needs.
44
+
37
45
  ## Usage
38
46
 
47
+ ### Generating blank seedie.yml
48
+ If you want to create a blank seedie.yml file, use the `--blank` option:
49
+
50
+ ```bash
51
+ $ rails generate seedie:install --blank
52
+ ```
53
+
54
+ This will generate a blank seedie.yml config file for you that you can now customize according to your needs.
55
+
56
+ ### Excluding Models
57
+ If you want to exclude certain models while generating the `seedie.yml`, use the `--exclude_models` option:
58
+
59
+ ```bash
60
+ $ rails generate seedie:install --exclude_models User Admin Post
61
+ ```
62
+
63
+ NOTE: Some models may not be excluded because of their dependencies. For example, if you have a model `Post` that belongs to a model `User`, then the `User` model will not be excluded even if you specify it in the `--exclude_models` option.
64
+
65
+ You'll get a warning in your console if any models are not excluded:
66
+
67
+ ```bash
68
+ WARNING: User has dependencies with other models and cannot be excluded.
69
+ ```
70
+
71
+ ### Including only few Models
72
+ If you want to include only few particular models while generating the `seedie.yml`, use the `--include_only_models` option:
73
+
74
+ ```bash
75
+ $ rails generate seedie:install --include_only_models Post Comment
76
+ ```
77
+
78
+ NOTE: Some models may be a dependency for the required models and will need to be included for successful seeding. For example, if you have a model `Post` that belongs to a model `User`, then the `User` model will need to be included even if you didn't specify it in the `--include_only_models` option.
79
+
80
+ You'll get a warning in your console if any other models are included:
81
+
82
+ ```bash
83
+ WARNING: User is a dependency of included models and needs to be included.
84
+ ```
85
+
86
+ ### Add Custom Attributes inside seedie.rb initializer
87
+
88
+ While generating the seedie.yml file, there are default values.
89
+ For example, for every `string` field, the default value is `{{Faker::Lorem.word}}`.
90
+ This works fine for most attributes, but for some there are validations which breaks the seeding.
91
+ Take `email` as an example, the default value `{{Faker::Lorem.word}}` will not be a valid email.
92
+ By default, when we generate the seedie.yml file, we add a `seedie.rb` initializer file in the `config/initializers` directory.
93
+
94
+ ```ruby
95
+ Seedie.configure do |config|
96
+ # config.default_count = 10
97
+
98
+ config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
99
+ # Add more custom attributes here
100
+ end
101
+ ```
102
+
103
+ This ensures that the `email` field is seeded with a valid email address.
104
+ You can add more custom attributes in the `seedie.rb` initializer file.
105
+
106
+ ### Seeding Models
107
+
39
108
  To seed your models, run the following Rake task:
40
109
 
41
110
  ```bash
@@ -75,6 +144,11 @@ models:
75
144
  post_metadatum:
76
145
  attributes:
77
146
  seo_text: "{{Faker::Lorem.paragraph}}"
147
+ has_and_belongs_to_many:
148
+ tags:
149
+ count: 3
150
+ attributes:
151
+ name: "{{Faker::Lorem.word}}"
78
152
  disabled_fields: []
79
153
  comment:
80
154
  attributes:
@@ -96,9 +170,9 @@ In this file:
96
170
  - Additionally, we can use placeholders like `{{Faker::Internet.email}}` to generate dynamic and unique data for each record using Faker.
97
171
  - We can also specify an array of values that can be picked from randomly or sequentially using the `values` and `pick_strategy` options.
98
172
  - `disabled_fields` is an array of fields that should not be automatically filled by Seedie.
99
- - `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, and `has_one` are supported.
173
+ - `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, `has_one` and `has_and_belongs_to_many` are supported.
100
174
  - The specified number for `has_many` represents the number of associated records to create.
101
- - For `belongs_to`, the value `random` means that a random existing record will be associated.
175
+ - For `belongs_to`, the value `random` means that a random existing record will be associated. If there is a unique index associated, then `unique` will be set or else `random` is the default.
102
176
  - If attributes are specified under an association, those attributes will be used when creating the associated record(s)
103
177
  - When using associations, it's important to define the models in the correct order in the `seedie.yml` file. Associated models should be defined before the models that reference them.
104
178
 
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "bundler/gem_tasks"
3
4
  require "rspec/core/rake_task"
4
5
 
5
-
6
6
  desc "Run all examples"
7
7
  RSpec::Core::RakeTask.new(:spec) do |t|
8
8
  t.ruby_opts = %w[-w]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/generators/base"
2
4
  require "seedie"
3
5
 
@@ -5,7 +7,7 @@ module Seedie
5
7
  module Generators
6
8
  class InstallGenerator < Rails::Generators::Base
7
9
  include PolymorphicAssociationHelper
8
-
10
+
9
11
  EXCLUDED_MODELS = %w[
10
12
  ActiveRecord::SchemaMigration
11
13
  ActiveRecord::InternalMetadata
@@ -15,29 +17,49 @@ module Seedie
15
17
  ActionText::RichText
16
18
  ActionMailbox::InboundEmail
17
19
  ActionText::EncryptedRichText
18
- ]
20
+ ].freeze
19
21
 
20
22
  source_root File.expand_path("templates", __dir__)
21
23
 
24
+ class_option :blank, type: :boolean, default: false, desc: "Generate a blank seedie.yml with examples"
25
+ class_option :excluded_models, type: :array, default: [], desc: "Models to exclude from seedie.yml"
26
+ class_option :include_only_models, type: :array, default: [],
27
+ desc: "Models to be specifically included in seedie.yml. This will ignore all other models."
28
+
22
29
  desc "Creates a seedie.yml for your application."
23
- def generate_seedie_file(output = STDOUT)
24
- Rails.application.eager_load! # Load all models. This is required!!
30
+ def generate_seedie_file(output = $stdout)
31
+ if options[:include_only_models].present? && options[:excluded_models].present?
32
+ raise ArgumentError, "Cannot use both --include_only_models and --excluded_models together."
33
+ end
34
+
35
+ # This needs to be generated before anything else.
36
+ template "seedie_initializer.rb", "config/initializers/seedie.rb"
37
+
38
+ @excluded_models = options[:excluded_models] + EXCLUDED_MODELS
39
+ @output = output
40
+
41
+ if options[:blank]
42
+ template "blank_seedie.yml", "config/seedie.yml"
43
+ else
44
+ Rails.application.eager_load! # Load all models. This is required!!
25
45
 
26
- @models = get_models
27
- @models_config = build_models_config
28
- template "seedie.yml", "config/seedie.yml"
46
+ @models = collect_models
47
+ @models_config = build_models_config
48
+ template "seedie.yml", "config/seedie.yml"
49
+ end
29
50
 
30
- output_seedie_warning(output)
51
+ output_seedie_warning
31
52
  end
32
53
 
33
54
  private
34
55
 
35
-
36
56
  def build_models_config
37
57
  models = Model::ModelSorter.new(@models).sort_by_dependency
38
- models.reduce({}) do |config, model|
58
+
59
+ output_warning_for_extra_models(models)
60
+
61
+ models.each_with_object({}) do |model, config|
39
62
  config[model.name.underscore] = model_configuration(model)
40
- config
41
63
  end
42
64
  end
43
65
 
@@ -68,12 +90,12 @@ module Seedie
68
90
  if polymorphic_types.include?(column.name) || foreign_keys.include?(column.name)
69
91
  next
70
92
  end
71
-
93
+
72
94
  # Adding default columns to default_columns
73
95
  if column.default.present? || column.default_function.present?
74
96
  default_columns << column
75
- elsif column.null == false || has_presence_validator?(model, column.name)
76
- # Only add to active if its required or has presence validator
97
+ elsif column.null == false || presence_validator?(model, column.name)
98
+ # Only add to active if its required or has presence validator
77
99
  active_columns << column
78
100
  else
79
101
  disabled_columns << column
@@ -82,7 +104,7 @@ module Seedie
82
104
 
83
105
  # Add atleast one column to active columns
84
106
  active_columns << disabled_columns.pop if active_columns.empty? && disabled_columns.present?
85
-
107
+
86
108
  # Disable all default columns
87
109
  disabled_columns += default_columns
88
110
 
@@ -93,14 +115,13 @@ module Seedie
93
115
  end
94
116
 
95
117
  def active_columns_configuration(model, columns)
96
- columns.reduce({}) do |config, column|
118
+ columns.each_with_object({}) do |column, config|
97
119
  validations = model.validators_on(column.name)
98
120
  config[column.name] = if validations.present?
99
121
  FieldValues::FakerBuilder.new(column.name, column, validations).build_faker_constant
100
122
  else
101
123
  FieldValues::FakeValue.new(column.name, column).generate_fake_value
102
124
  end
103
- config
104
125
  end
105
126
  end
106
127
 
@@ -113,7 +134,7 @@ module Seedie
113
134
  "associations" => {
114
135
  "belongs_to" => belongs_to_associations_configuration(model),
115
136
  "has_one" => {}, # TODO: Add has_one associations
116
- "has_many" => {}, # TODO: Add has_many associations
137
+ "has_many" => {} # TODO: Add has_many associations
117
138
  }
118
139
  }
119
140
  end
@@ -123,16 +144,18 @@ module Seedie
123
144
  association.options[:optional] == true # Excluded Optional Associations
124
145
  end
125
146
 
126
- belongs_to_associations.reduce({}) do |config, association|
147
+ unique_indexes = model.connection.indexes(model.table_name).select(&:unique).flat_map(&:columns)
148
+
149
+ belongs_to_associations.each_with_object({}) do |association, config|
127
150
  if association.polymorphic?
128
151
  config[association.name.to_s] = set_polymorphic_association_config(model, association)
129
152
  else
130
- config[association.name.to_s] = "random"
153
+ association_has_unique_index = unique_indexes.include?(association.foreign_key.to_s)
154
+ config[association.name.to_s] = association_has_unique_index ? "unique" : "random"
131
155
  end
132
- config
133
156
  end
134
- end
135
-
157
+ end
158
+
136
159
  def set_polymorphic_association_config(model, association)
137
160
  {
138
161
  "polymorphic" => find_polymorphic_types(model, association.name),
@@ -140,26 +163,50 @@ module Seedie
140
163
  }
141
164
  end
142
165
 
143
- def has_presence_validator?(model, column_name)
166
+ def presence_validator?(model, column_name)
144
167
  model.validators_on(column_name).any? { |v| v.kind == :presence }
145
168
  end
146
169
 
147
- def get_models
148
- @get_models ||= ActiveRecord::Base.descendants.reject do |model|
149
- EXCLUDED_MODELS.include?(model.name) || # Excluded Reserved Models
150
- model.abstract_class? || # Excluded Abstract Models
151
- model.table_exists? == false || # Excluded Models without tables
152
- model.name.blank? || # Excluded Anonymous Models
153
- model.name.start_with?("HABTM_") # Excluded HABTM Models
170
+ def collect_models
171
+ @collect_models ||= begin
172
+ all_models = ActiveRecord::Base.descendants
173
+
174
+ if options[:include_only_models].present?
175
+ all_models.select! { |model| options[:include_only_models].include?(model.name) }
176
+ end
177
+
178
+ all_models.reject do |model|
179
+ @excluded_models.include?(model.name) || # Excluded Reserved Models
180
+ model.abstract_class? || # Excluded Abstract Models
181
+ model.table_exists? == false || # Excluded Models without tables
182
+ model.name.blank? || # Excluded Anonymous Models
183
+ model.name.start_with?("HABTM_") # Excluded HABTM Models
184
+ end
154
185
  end
155
186
  end
156
187
 
157
- def output_seedie_warning(output)
158
- output.puts "Seedie config file generated at config/seedie.yml"
159
- output.puts "##################################################"
160
- output.puts "WARNING: Please review the generated config file before running the seeds."
161
- output.puts "There might be some things that you might need to change to ensure that the generated seeds run successfully."
162
- output.puts "##################################################"
188
+ def output_seedie_warning
189
+ @output.puts "Seedie config file generated at config/seedie.yml"
190
+ @output.puts "##################################################"
191
+ @output.puts "WARNING: Please review the generated config file before running the seeds."
192
+ @output.puts "There might be some things that you might need to change to ensure that the generated seeds run successfully."
193
+ @output.puts "##################################################"
194
+ end
195
+
196
+ def output_warning_for_extra_models(models)
197
+ if options[:excluded_models].present?
198
+ required_excluded_models = models.map(&:name) & @excluded_models
199
+
200
+ required_excluded_models.each do |model_name|
201
+ @output.puts "WARNING: #{model_name} has dependencies with other models and cannot be excluded."
202
+ end
203
+ elsif options[:include_only_models].present?
204
+ dependent_models = models.map(&:name) - @models.map(&:name)
205
+
206
+ dependent_models.each do |model_name|
207
+ @output.puts "WARNING: #{model_name} is a dependency of included models and needs to be included."
208
+ end
209
+ end
163
210
  end
164
211
  end
165
212
  end
@@ -0,0 +1,22 @@
1
+ ### This is a blank seedie.yml file. ###
2
+
3
+ # default_count: 10
4
+ #
5
+ # models:
6
+ # model_name:
7
+ # attributes:
8
+ # string_field: '{{Faker::Lorem.word}}'
9
+ # integer_field:
10
+ # values: [1, 2, 3]
11
+ # options:
12
+ # pick_strategy: random
13
+ # jsonb_field:
14
+ # value: '{"key": "value"}'
15
+ # disabled_fields: ["unwanted_field"]
16
+ # associations:
17
+ # belongs_to:
18
+ # parent_model: random
19
+ # has_one:
20
+ # associated_model: random
21
+ # has_many:
22
+ # other_models: random
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ Seedie.configure do |config|
4
+ # config.default_count = 10
5
+
6
+ config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
7
+ # Add more custom attributes here
8
+ end