auto_seed 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +115 -14
- data/lib/auto_seed/seeder.rb +87 -24
- data/lib/auto_seed/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcbda2a17e35171f9bf903093b8d32dd0fed8e7ae289f092edee39ebbf7e5e20
|
4
|
+
data.tar.gz: 38e0d57abbbde6e0cec647c4008fae09b1f06f99b3f12999553920a74989b892
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 301efb1abe01e3ea3927bd1a69588fb179b1a11ecfdeb77e1b4622e22f9bf1140d158db0798d0b710ac9525a922d3d90716c4a23b317c6221d5775c1c5e10342
|
7
|
+
data.tar.gz: a0c2010af1460f10f27f1fd4fc068490b180e2a938404d21c1ae79255207420aa9ee5ad98f42ef320e8cb27a2955c4e22f0b7ea557b460b8ad17c34fd4632217
|
data/README.md
CHANGED
@@ -1,39 +1,140 @@
|
|
1
1
|
# AutoSeed
|
2
2
|
|
3
|
-
|
3
|
+
AutoSeed is a Ruby on Rails gem that uses schema comments in the model to automatically generate seed files by running a rake task.
|
4
4
|
|
5
|
-
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Automatically generates seed data based on your Rails models and their schema comments.
|
8
|
+
- Handles various associations, including `has_many`, `belongs_to`, `has_one`, and polymorphic associations.
|
9
|
+
- Provides an easy-to-use rake task for seed generation.
|
6
10
|
|
7
11
|
## Installation
|
8
12
|
|
9
|
-
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'auto_seed'
|
17
|
+
```
|
10
18
|
|
11
|
-
|
19
|
+
And then execute:
|
12
20
|
|
13
|
-
|
21
|
+
```
|
22
|
+
bundle install
|
23
|
+
```
|
14
24
|
|
15
|
-
|
25
|
+
Or install it yourself as:
|
16
26
|
|
17
|
-
|
27
|
+
```
|
28
|
+
gem install auto_seed
|
29
|
+
```
|
18
30
|
|
19
31
|
## Usage
|
20
32
|
|
21
|
-
|
33
|
+
To generate the seed file, run the following rake task:
|
34
|
+
|
35
|
+
```
|
36
|
+
rails db:generate_seeds
|
37
|
+
```
|
38
|
+
|
39
|
+
This will create or overwrite the db/seeds.rb file with seed data based on your application's models and their associations.
|
40
|
+
|
41
|
+
## Example
|
42
|
+
|
43
|
+
Assume you have the following models:
|
44
|
+
|
45
|
+
```
|
46
|
+
class User < ApplicationRecord
|
47
|
+
has_many :posts
|
48
|
+
end
|
49
|
+
|
50
|
+
class Post < ApplicationRecord
|
51
|
+
belongs_to :user
|
52
|
+
has_many :comments, as: :commentable
|
53
|
+
end
|
54
|
+
|
55
|
+
class Comment < ApplicationRecord
|
56
|
+
belongs_to :commentable, polymorphic: true
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Running the `rails db:generate_seeds` task will generate a `db/seeds.rb` file similar to the following:
|
61
|
+
|
62
|
+
```
|
63
|
+
## User seeds
|
64
|
+
User.create!(
|
65
|
+
id: 'sample_value',
|
66
|
+
name: 'sample_value',
|
67
|
+
email: 'sample_value',
|
68
|
+
created_at: 'sample_value',
|
69
|
+
updated_at: 'sample_value',
|
70
|
+
posts: [Post.create!(...)],
|
71
|
+
);
|
72
|
+
|
73
|
+
## Post seeds
|
74
|
+
Post.create!(
|
75
|
+
id: 'sample_value',
|
76
|
+
title: 'sample_value',
|
77
|
+
content: 'sample_value',
|
78
|
+
created_at: 'sample_value',
|
79
|
+
updated_at: 'sample_value',
|
80
|
+
user: User.first,
|
81
|
+
comments: [Comment.create!(...)],
|
82
|
+
);
|
83
|
+
|
84
|
+
## Comment seeds
|
85
|
+
Comment.create!(
|
86
|
+
id: 'sample_value',
|
87
|
+
content: 'sample_value',
|
88
|
+
created_at: 'sample_value',
|
89
|
+
updated_at: 'sample_value',
|
90
|
+
commentable_type: 'Post',
|
91
|
+
commentable_id: Post.first.id,
|
92
|
+
);
|
93
|
+
```
|
22
94
|
|
23
95
|
## Development
|
24
96
|
|
25
|
-
|
97
|
+
To install this gem onto your local machine, run:
|
98
|
+
|
99
|
+
```
|
100
|
+
bundle exec rake install
|
101
|
+
```
|
26
102
|
|
27
|
-
To
|
103
|
+
To release a new version, update the version number in version.rb, and then run:
|
104
|
+
|
105
|
+
```
|
106
|
+
bundle exec rake release
|
107
|
+
```
|
28
108
|
|
29
109
|
## Contributing
|
30
110
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/your_username/auto_seed.
|
112
|
+
|
113
|
+
Fork it (https://github.com/your_username/auto_seed/fork)
|
114
|
+
|
115
|
+
Create your feature branch (git checkout -b my-new-feature)
|
116
|
+
|
117
|
+
Commit your changes (git commit -am 'Add some feature')
|
118
|
+
|
119
|
+
Push to the branch (git push origin my-new-feature)
|
120
|
+
|
121
|
+
Create a new Pull Request
|
32
122
|
|
33
123
|
## License
|
34
124
|
|
35
|
-
|
125
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
126
|
+
|
127
|
+
Acknowledgments
|
128
|
+
Special thanks to all contributors and the open-source community.
|
129
|
+
|
130
|
+
## Explanation:
|
36
131
|
|
37
|
-
|
132
|
+
1. **Features**: Lists what the gem can do.
|
133
|
+
2. **Installation**: Provides instructions for adding the gem to a Rails project.
|
134
|
+
3. **Usage**: Explains how to use the rake task to generate seed files.
|
135
|
+
4. **Example**: Demonstrates an example output of the generated seeds file.
|
136
|
+
5. **Development**: Describes how to install the gem locally and release a new version.
|
137
|
+
6. **Contributing**: Provides guidelines for contributing to the project.
|
138
|
+
7. **License**: Mentions the project’s license.
|
38
139
|
|
39
|
-
|
140
|
+
Make sure to replace placeholders such as `your_username` with your actual GitHub username and adjust any other project-specific details as needed.
|
data/lib/auto_seed/seeder.rb
CHANGED
@@ -1,38 +1,26 @@
|
|
1
1
|
module AutoSeed
|
2
2
|
class Seeder
|
3
|
+
DEVISE_COLUMNS = %w[
|
4
|
+
last_sign_in_at current_sign_in_at last_sign_in_ip current_sign_in_ip
|
5
|
+
encrypted_password reset_password_token reset_password_sent_at
|
6
|
+
remember_created_at sign_in_count
|
7
|
+
].freeze
|
8
|
+
|
3
9
|
def self.generate_seed_file
|
4
10
|
File.open('db/seeds.rb', 'w') do |file|
|
5
11
|
ActiveRecord::Base.descendants.each do |model|
|
6
|
-
next if
|
12
|
+
next if skip_model?(model)
|
7
13
|
|
8
14
|
file.puts "## #{model.name} seeds"
|
9
15
|
|
10
16
|
begin
|
11
|
-
attributes = model
|
17
|
+
attributes = fetch_attributes(model)
|
18
|
+
attribute_values = generate_attributes(attributes)
|
12
19
|
file.puts "#{model.name}.create!("
|
20
|
+
attribute_values.each { |attr, value| file.puts " #{attr}: #{value.inspect}," }
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
model.reflect_on_all_associations.each do |assoc|
|
19
|
-
case assoc.macro
|
20
|
-
when :has_many
|
21
|
-
file.puts " #{assoc.name}: [#{assoc.klass}.create!(...)]"
|
22
|
-
when :belongs_to
|
23
|
-
file.puts " #{assoc.name}: #{assoc.klass}.first"
|
24
|
-
when :has_one
|
25
|
-
file.puts " #{assoc.name}: #{assoc.klass}.create!(...)"
|
26
|
-
when :has_many_attached
|
27
|
-
file.puts " #{assoc.name}: [#{assoc.klass}.attach(...)]"
|
28
|
-
end
|
29
|
-
|
30
|
-
# Handle polymorphic associations
|
31
|
-
if assoc.polymorphic?
|
32
|
-
file.puts " #{assoc.name}_type: '#{assoc.klass}',"
|
33
|
-
file.puts " #{assoc.name}_id: #{assoc.klass}.first.id"
|
34
|
-
end
|
35
|
-
end
|
22
|
+
associations = handle_associations(model)
|
23
|
+
associations.each { |assoc| file.puts " #{assoc[:name]}: #{assoc[:value]}," }
|
36
24
|
|
37
25
|
file.puts ");"
|
38
26
|
rescue StandardError => e
|
@@ -43,5 +31,80 @@ module AutoSeed
|
|
43
31
|
end
|
44
32
|
end
|
45
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.skip_model?(model)
|
38
|
+
model.name.in?(%w[ApplicationRecord ActiveRecord::Base])
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.fetch_attributes(model)
|
42
|
+
model.columns.reject { |col| col.name.in?(DEVISE_COLUMNS + %w[ip_address current_sign_in_at]) }.map(&:name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.generate_attributes(attributes)
|
46
|
+
attributes.each_with_object({}) do |attribute, hash|
|
47
|
+
hash[attribute] = generate_sample_value(attribute)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.generate_sample_value(attribute)
|
52
|
+
case attribute
|
53
|
+
when /_at$/
|
54
|
+
"'#{Time.now}'"
|
55
|
+
when /_on$/
|
56
|
+
"'#{Date.today}'"
|
57
|
+
when /date$/
|
58
|
+
"'#{Date.today}'"
|
59
|
+
else
|
60
|
+
"'sample_value'"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.handle_associations(model)
|
65
|
+
associations = []
|
66
|
+
model.reflect_on_all_associations.each do |association|
|
67
|
+
next if association.options[:optional]
|
68
|
+
|
69
|
+
case association.macro
|
70
|
+
when :has_many
|
71
|
+
associations.concat(handle_has_many_association(association))
|
72
|
+
when :belongs_to
|
73
|
+
associations.concat(handle_belongs_to_association(association))
|
74
|
+
when :has_one
|
75
|
+
associations.concat(handle_has_one_association(association))
|
76
|
+
when :has_many_attached
|
77
|
+
associations.concat(handle_has_many_attached_association(association))
|
78
|
+
end
|
79
|
+
|
80
|
+
if association.polymorphic?
|
81
|
+
associations.concat(handle_polymorphic_association(association))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
associations
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.handle_has_many_association(association)
|
88
|
+
[{ name: association.name, value: "[#{association.klass}.create!(...)]" }]
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.handle_belongs_to_association(association)
|
92
|
+
[{ name: association.name, value: "#{association.klass}.first" }]
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.handle_has_one_association(association)
|
96
|
+
[{ name: association.name, value: "#{association.klass}.create!(...)" }]
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.handle_has_many_attached_association(association)
|
100
|
+
[{ name: association.name, value: "[#{association.klass}.attach(...)]" }]
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.handle_polymorphic_association(association)
|
104
|
+
[
|
105
|
+
{ name: "#{association.name}_type", value: "'#{association.klass}'" },
|
106
|
+
{ name: "#{association.name}_id", value: "#{association.klass}.first.id" }
|
107
|
+
]
|
108
|
+
end
|
46
109
|
end
|
47
110
|
end
|
data/lib/auto_seed/version.rb
CHANGED