seedie 0.2.0 → 0.3.0

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: dba945f1f8d911d59194de8191ef857885bc79fb3986fe4d159bbf21842758d9
4
- data.tar.gz: 330174e781fa44b766d1fdcc4fbca9b4cc4f549db447913a60e2c1a41a4f0f51
3
+ metadata.gz: fa80b46ccb1727a4451598e8f94657bc15b43dbb0b2b0e58e8bd744db0636af6
4
+ data.tar.gz: b8e49e8fecdbc3c63f62b55384e3860428c9eb579114552cc2da7212c1e4b92c
5
5
  SHA512:
6
- metadata.gz: 4717460bd7dc315de646b3e839f5d8be92881245638711c790b5f8a8b3dac216d8ab0eccf8c6d3bc974f7c9ecf1f00b8520bbf44f6b112af18e2620a44faee71
7
- data.tar.gz: cc648a856d662822565da6d648f16d5f17da1f9b5f1eca45f0e2748fc8633e98d7018efda18d699af4771315b424fa83d13b397a632716a06d79910ee4ec4ae9
6
+ metadata.gz: fd0263009e5cd4b9d2287d56bde4ee2eb93337019218e018960efb9e5c9206b9f0323ccec95eaa986ddb9257d4acc9ccf684906f3f56319e1536fb6018201c4a
7
+ data.tar.gz: '0808e17b7a47e12367491571ade529974202d48eb0d93a0885143dba3e4b2982db13cac4a2196e478e39770d120cf4cd632b4436934d649f23e9f2f651263684'
data/CHANGELOG.md CHANGED
@@ -2,6 +2,58 @@
2
2
 
3
3
  ### New Features
4
4
 
5
+ #### Generate a Blank Seedie.yml file with `--blank` option
6
+
7
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/20)
8
+
9
+ You can now generate a blank seedie.yml file with the `--blank` option:
10
+
11
+ ```bash
12
+ rails g seedie:install --blank
13
+ ```
14
+
15
+ #### Exclude models from seedie.yml generation with `--excluded_models` option
16
+
17
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/21)
18
+
19
+ You can now exclude models from seedie.yml generation with the `--excluded_models` option:
20
+
21
+ ```bash
22
+ rails g seedie:install --excluded_models Post Comment
23
+ ```
24
+
25
+ #### Generate a Seedie.yml file with only specific models with `--include_only_models` option
26
+
27
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/22)
28
+
29
+ You can now generate a seedie.yml file with only specific models with the `--include_only_models` option:
30
+
31
+ ```bash
32
+ rails g seedie:install --include_only_models Post Comment
33
+ ```
34
+
35
+ #### Bugfix: Now unique indexes will be generated with `unique` instead of `random` pick_strategy
36
+
37
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/23)
38
+
39
+ #### Introducing new Seedie.rb initializer
40
+
41
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/28)
42
+ * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/29)
43
+
44
+ ```ruby
45
+ Seedie.configure do |config|
46
+ # config.default_count = 10
47
+
48
+ config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
49
+ # Add more custom attributes here
50
+ end
51
+ ```
52
+
53
+ ## Version 0.2.0
54
+
55
+ ### New Features
56
+
5
57
  #### Polymorphic Association
6
58
  * [GitHub PR](https://github.com/keshavbiswa/seedie/pull/12)
7
59
 
data/README.md CHANGED
@@ -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
@@ -98,7 +167,7 @@ In this file:
98
167
  - `disabled_fields` is an array of fields that should not be automatically filled by Seedie.
99
168
  - `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, and `has_one` are supported.
100
169
  - 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.
170
+ - 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
171
  - If attributes are specified under an association, those attributes will be used when creating the associated record(s)
103
172
  - 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
173
 
@@ -19,22 +19,44 @@ module Seedie
19
19
 
20
20
  source_root File.expand_path("templates", __dir__)
21
21
 
22
+ class_option :blank, type: :boolean, default: false, desc: "Generate a blank seedie.yml with examples"
23
+ class_option :excluded_models, type: :array, default: [], desc: "Models to exclude from seedie.yml"
24
+ class_option :include_only_models, type: :array, default: [],
25
+ desc: "Models to be specifically included in seedie.yml. This will ignore all other models."
26
+
27
+
22
28
  desc "Creates a seedie.yml for your application."
23
29
  def generate_seedie_file(output = STDOUT)
24
- Rails.application.eager_load! # Load all models. This is required!!
30
+ if options[:include_only_models].present? && options[:excluded_models].present?
31
+ raise ArgumentError, "Cannot use both --include_only_models and --excluded_models together."
32
+ end
33
+
34
+ # This needs to be generated before anything else.
35
+ template "seedie_initializer.rb", "config/initializers/seedie.rb"
36
+
37
+ @excluded_models = options[:excluded_models] + EXCLUDED_MODELS
38
+ @output = output
25
39
 
26
- @models = get_models
27
- @models_config = build_models_config
28
- template "seedie.yml", "config/seedie.yml"
40
+ if options[:blank]
41
+ template "blank_seedie.yml", "config/seedie.yml"
42
+ else
43
+ Rails.application.eager_load! # Load all models. This is required!!
29
44
 
30
- output_seedie_warning(output)
45
+ @models = get_models
46
+ @models_config = build_models_config
47
+ template "seedie.yml", "config/seedie.yml"
48
+ end
49
+
50
+ output_seedie_warning
31
51
  end
32
52
 
33
53
  private
34
54
 
35
-
36
55
  def build_models_config
37
56
  models = Model::ModelSorter.new(@models).sort_by_dependency
57
+
58
+ output_warning_for_extra_models(models)
59
+
38
60
  models.reduce({}) do |config, model|
39
61
  config[model.name.underscore] = model_configuration(model)
40
62
  config
@@ -123,11 +145,14 @@ module Seedie
123
145
  association.options[:optional] == true # Excluded Optional Associations
124
146
  end
125
147
 
148
+ unique_indexes = model.connection.indexes(model.table_name).select(&:unique).flat_map(&:columns)
149
+
126
150
  belongs_to_associations.reduce({}) do |config, association|
127
151
  if association.polymorphic?
128
152
  config[association.name.to_s] = set_polymorphic_association_config(model, association)
129
153
  else
130
- config[association.name.to_s] = "random"
154
+ association_has_unique_index = unique_indexes.include?(association.foreign_key.to_s)
155
+ config[association.name.to_s] = association_has_unique_index ? "unique" : "random"
131
156
  end
132
157
  config
133
158
  end
@@ -145,21 +170,45 @@ module Seedie
145
170
  end
146
171
 
147
172
  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
173
+ @get_models ||= begin
174
+ all_models = ActiveRecord::Base.descendants
175
+
176
+ if options[:include_only_models].present?
177
+ all_models.select! { |model| options[:include_only_models].include?(model.name) }
178
+ end
179
+
180
+ all_models.reject do |model|
181
+ @excluded_models.include?(model.name) || # Excluded Reserved Models
182
+ model.abstract_class? || # Excluded Abstract Models
183
+ model.table_exists? == false || # Excluded Models without tables
184
+ model.name.blank? || # Excluded Anonymous Models
185
+ model.name.start_with?("HABTM_") # Excluded HABTM Models
186
+ end
154
187
  end
155
188
  end
156
189
 
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 "##################################################"
190
+ def output_seedie_warning
191
+ @output.puts "Seedie config file generated at config/seedie.yml"
192
+ @output.puts "##################################################"
193
+ @output.puts "WARNING: Please review the generated config file before running the seeds."
194
+ @output.puts "There might be some things that you might need to change to ensure that the generated seeds run successfully."
195
+ @output.puts "##################################################"
196
+ end
197
+
198
+ def output_warning_for_extra_models(models)
199
+ if options[:excluded_models].present?
200
+ required_excluded_models = models.map(&:name) & @excluded_models
201
+
202
+ required_excluded_models.each do |model_name|
203
+ @output.puts "WARNING: #{model_name} has dependencies with other models and cannot be excluded."
204
+ end
205
+ elsif options[:include_only_models].present?
206
+ dependent_models = models.map(&:name) - @models.map(&:name)
207
+
208
+ dependent_models.each do |model_name|
209
+ @output.puts "WARNING: #{model_name} is a dependency of included models and needs to be included."
210
+ end
211
+ end
163
212
  end
164
213
  end
165
214
  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
@@ -0,0 +1,10 @@
1
+ module Seedie
2
+ class Configuration
3
+ attr_accessor :default_count, :custom_attributes
4
+
5
+ def initialize
6
+ @default_count = nil
7
+ @custom_attributes = {}
8
+ end
9
+ end
10
+ end
@@ -10,9 +10,12 @@ module Seedie
10
10
  @class_prefix = ""
11
11
  @method_prefix = ""
12
12
  @options = ""
13
+ @seedie_config_custom_attributes = Seedie.configuration.custom_attributes
13
14
  end
14
15
 
15
16
  def build_faker_constant
17
+ return @seedie_config_custom_attributes[@name.to_sym] if @seedie_config_custom_attributes.key?(@name.to_sym)
18
+
16
19
  @unique_prefix = "unique." if has_validation?(:uniqueness)
17
20
 
18
21
  add_faker_class_and_method(@column.type)
@@ -22,7 +22,6 @@ module Seedie
22
22
 
23
23
  private
24
24
 
25
-
26
25
  # Independent models need to be added first
27
26
  def add_independent_models_to_queue
28
27
  @models.each do |model|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Seedie
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/seedie.rb CHANGED
@@ -24,6 +24,7 @@ require_relative "seedie/associations/belongs_to"
24
24
 
25
25
  require_relative "seedie/seeder"
26
26
  require_relative "seedie/version"
27
+ require_relative "seedie/configuration"
27
28
 
28
29
  require "seedie/railtie" if defined?(Rails)
29
30
 
@@ -41,4 +42,14 @@ module Seedie
41
42
  class InvalidCustomFieldValuesError < StandardError; end
42
43
  class InvalidCustomFieldOptionsError < StandardError; end
43
44
  class CustomFieldNotEnoughValuesError < StandardError; end
45
+
46
+ class << self
47
+ def configure
48
+ yield configuration if block_given?
49
+ end
50
+
51
+ def configuration
52
+ @configuration ||= Configuration.new
53
+ end
54
+ end
44
55
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seedie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keshav Biswa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-13 00:00:00.000000000 Z
11
+ date: 2023-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.9'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.9'
27
27
  - !ruby/object:Gem::Dependency
@@ -125,12 +125,15 @@ files:
125
125
  - Rakefile
126
126
  - lib/generators/seedie/USAGE
127
127
  - lib/generators/seedie/install_generator.rb
128
+ - lib/generators/seedie/templates/blank_seedie.yml
128
129
  - lib/generators/seedie/templates/seedie.yml
130
+ - lib/generators/seedie/templates/seedie_initializer.rb
129
131
  - lib/seedie.rb
130
132
  - lib/seedie/associations/base_association.rb
131
133
  - lib/seedie/associations/belongs_to.rb
132
134
  - lib/seedie/associations/has_many.rb
133
135
  - lib/seedie/associations/has_one.rb
136
+ - lib/seedie/configuration.rb
134
137
  - lib/seedie/field_values/custom_value.rb
135
138
  - lib/seedie/field_values/fake_value.rb
136
139
  - lib/seedie/field_values/faker_builder.rb