rails_json_seeder 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +23 -0
- data/.rubocop.yml +8 -1
- data/Gemfile +9 -5
- data/Gemfile.lock +212 -0
- data/README.md +145 -29
- data/Rakefile +2 -2
- data/lib/rails_json_seeder/seeder.rb +82 -0
- data/lib/rails_json_seeder/version.rb +1 -1
- data/lib/rails_json_seeder.rb +10 -3
- metadata +5 -3
- data/rails_json_seeder.gemspec +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f2c58b24d9cc7bffbf180174e4f534366784e8518bb2e2e3b1d30a4413b0213
|
4
|
+
data.tar.gz: 8fd0377aa419a5a56e5032d81ecfefa0112ae4185affe90c65fa3b6df7806c48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0af397096de31b2cf74cbae4441808abe978152804ce7b863c71af5d9f1c01d6dff005b591e81b4b17acee6b82274454fe8f930ae30e72900076e374a37f287c
|
7
|
+
data.tar.gz: 9e9a654af9924762d9b21373ea19544e6a80ccf373b03295d99e2f3539d8688bf6fafcc19653b12b7541326b0b80c52e0cfe808d3a64b73412e864c9ebfe3298
|
data/.editorconfig
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Top-most EditorConfig file
|
2
|
+
root = true
|
3
|
+
|
4
|
+
# Default settings for all files
|
5
|
+
[*]
|
6
|
+
indent_style = space
|
7
|
+
indent_size = 2
|
8
|
+
end_of_line = lf
|
9
|
+
charset = utf-8
|
10
|
+
trim_trailing_whitespace = true
|
11
|
+
insert_final_newline = true
|
12
|
+
|
13
|
+
# JSON files
|
14
|
+
[*.json]
|
15
|
+
indent_size = 2
|
16
|
+
|
17
|
+
# YAML files
|
18
|
+
[*.yml]
|
19
|
+
indent_size = 2
|
20
|
+
|
21
|
+
[*.yaml]
|
22
|
+
indent_size = 2
|
23
|
+
|
data/.rubocop.yml
CHANGED
@@ -3,7 +3,7 @@ AllCops:
|
|
3
3
|
|
4
4
|
Style/StringLiterals:
|
5
5
|
Enabled: true
|
6
|
-
EnforcedStyle:
|
6
|
+
EnforcedStyle: single_quotes
|
7
7
|
|
8
8
|
Style/StringLiteralsInInterpolation:
|
9
9
|
Enabled: true
|
@@ -11,3 +11,10 @@ Style/StringLiteralsInInterpolation:
|
|
11
11
|
|
12
12
|
Layout/LineLength:
|
13
13
|
Max: 120
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 30
|
17
|
+
|
18
|
+
Metrics/ModuleLength:
|
19
|
+
Max: 150
|
20
|
+
|
data/Gemfile
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in rails_json_seeder.gemspec
|
6
6
|
gemspec
|
7
|
-
|
8
|
-
gem
|
9
|
-
|
10
|
-
|
7
|
+
gem 'rails', '~> 6.0'
|
8
|
+
gem 'rake', '~> 13.0'
|
9
|
+
gem 'rubocop', '~> 1.21'
|
10
|
+
|
11
|
+
group :development, :test do
|
12
|
+
gem 'factory_bot_rails'
|
13
|
+
gem 'rspec-rails', '~> 5.0'
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_json_seeder (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (6.1.7.6)
|
10
|
+
actionpack (= 6.1.7.6)
|
11
|
+
activesupport (= 6.1.7.6)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailbox (6.1.7.6)
|
15
|
+
actionpack (= 6.1.7.6)
|
16
|
+
activejob (= 6.1.7.6)
|
17
|
+
activerecord (= 6.1.7.6)
|
18
|
+
activestorage (= 6.1.7.6)
|
19
|
+
activesupport (= 6.1.7.6)
|
20
|
+
mail (>= 2.7.1)
|
21
|
+
actionmailer (6.1.7.6)
|
22
|
+
actionpack (= 6.1.7.6)
|
23
|
+
actionview (= 6.1.7.6)
|
24
|
+
activejob (= 6.1.7.6)
|
25
|
+
activesupport (= 6.1.7.6)
|
26
|
+
mail (~> 2.5, >= 2.5.4)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
actionpack (6.1.7.6)
|
29
|
+
actionview (= 6.1.7.6)
|
30
|
+
activesupport (= 6.1.7.6)
|
31
|
+
rack (~> 2.0, >= 2.0.9)
|
32
|
+
rack-test (>= 0.6.3)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
35
|
+
actiontext (6.1.7.6)
|
36
|
+
actionpack (= 6.1.7.6)
|
37
|
+
activerecord (= 6.1.7.6)
|
38
|
+
activestorage (= 6.1.7.6)
|
39
|
+
activesupport (= 6.1.7.6)
|
40
|
+
nokogiri (>= 1.8.5)
|
41
|
+
actionview (6.1.7.6)
|
42
|
+
activesupport (= 6.1.7.6)
|
43
|
+
builder (~> 3.1)
|
44
|
+
erubi (~> 1.4)
|
45
|
+
rails-dom-testing (~> 2.0)
|
46
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
47
|
+
activejob (6.1.7.6)
|
48
|
+
activesupport (= 6.1.7.6)
|
49
|
+
globalid (>= 0.3.6)
|
50
|
+
activemodel (6.1.7.6)
|
51
|
+
activesupport (= 6.1.7.6)
|
52
|
+
activerecord (6.1.7.6)
|
53
|
+
activemodel (= 6.1.7.6)
|
54
|
+
activesupport (= 6.1.7.6)
|
55
|
+
activestorage (6.1.7.6)
|
56
|
+
actionpack (= 6.1.7.6)
|
57
|
+
activejob (= 6.1.7.6)
|
58
|
+
activerecord (= 6.1.7.6)
|
59
|
+
activesupport (= 6.1.7.6)
|
60
|
+
marcel (~> 1.0)
|
61
|
+
mini_mime (>= 1.1.0)
|
62
|
+
activesupport (6.1.7.6)
|
63
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
64
|
+
i18n (>= 1.6, < 2)
|
65
|
+
minitest (>= 5.1)
|
66
|
+
tzinfo (~> 2.0)
|
67
|
+
zeitwerk (~> 2.3)
|
68
|
+
ast (2.4.2)
|
69
|
+
base64 (0.1.1)
|
70
|
+
builder (3.2.4)
|
71
|
+
concurrent-ruby (1.2.2)
|
72
|
+
crass (1.0.6)
|
73
|
+
date (3.3.3)
|
74
|
+
diff-lcs (1.5.0)
|
75
|
+
erubi (1.12.0)
|
76
|
+
factory_bot (6.2.1)
|
77
|
+
activesupport (>= 5.0.0)
|
78
|
+
factory_bot_rails (6.2.0)
|
79
|
+
factory_bot (~> 6.2.0)
|
80
|
+
railties (>= 5.0.0)
|
81
|
+
globalid (1.2.1)
|
82
|
+
activesupport (>= 6.1)
|
83
|
+
i18n (1.14.1)
|
84
|
+
concurrent-ruby (~> 1.0)
|
85
|
+
json (2.6.3)
|
86
|
+
language_server-protocol (3.17.0.3)
|
87
|
+
loofah (2.21.3)
|
88
|
+
crass (~> 1.0.2)
|
89
|
+
nokogiri (>= 1.12.0)
|
90
|
+
mail (2.8.1)
|
91
|
+
mini_mime (>= 0.1.1)
|
92
|
+
net-imap
|
93
|
+
net-pop
|
94
|
+
net-smtp
|
95
|
+
marcel (1.0.2)
|
96
|
+
method_source (1.0.0)
|
97
|
+
mini_mime (1.1.5)
|
98
|
+
minitest (5.20.0)
|
99
|
+
net-imap (0.4.1)
|
100
|
+
date
|
101
|
+
net-protocol
|
102
|
+
net-pop (0.1.2)
|
103
|
+
net-protocol
|
104
|
+
net-protocol (0.2.1)
|
105
|
+
timeout
|
106
|
+
net-smtp (0.4.0)
|
107
|
+
net-protocol
|
108
|
+
nio4r (2.5.9)
|
109
|
+
nokogiri (1.15.4-arm64-darwin)
|
110
|
+
racc (~> 1.4)
|
111
|
+
parallel (1.23.0)
|
112
|
+
parser (3.2.2.4)
|
113
|
+
ast (~> 2.4.1)
|
114
|
+
racc
|
115
|
+
racc (1.7.1)
|
116
|
+
rack (2.2.8)
|
117
|
+
rack-test (2.1.0)
|
118
|
+
rack (>= 1.3)
|
119
|
+
rails (6.1.7.6)
|
120
|
+
actioncable (= 6.1.7.6)
|
121
|
+
actionmailbox (= 6.1.7.6)
|
122
|
+
actionmailer (= 6.1.7.6)
|
123
|
+
actionpack (= 6.1.7.6)
|
124
|
+
actiontext (= 6.1.7.6)
|
125
|
+
actionview (= 6.1.7.6)
|
126
|
+
activejob (= 6.1.7.6)
|
127
|
+
activemodel (= 6.1.7.6)
|
128
|
+
activerecord (= 6.1.7.6)
|
129
|
+
activestorage (= 6.1.7.6)
|
130
|
+
activesupport (= 6.1.7.6)
|
131
|
+
bundler (>= 1.15.0)
|
132
|
+
railties (= 6.1.7.6)
|
133
|
+
sprockets-rails (>= 2.0.0)
|
134
|
+
rails-dom-testing (2.2.0)
|
135
|
+
activesupport (>= 5.0.0)
|
136
|
+
minitest
|
137
|
+
nokogiri (>= 1.6)
|
138
|
+
rails-html-sanitizer (1.6.0)
|
139
|
+
loofah (~> 2.21)
|
140
|
+
nokogiri (~> 1.14)
|
141
|
+
railties (6.1.7.6)
|
142
|
+
actionpack (= 6.1.7.6)
|
143
|
+
activesupport (= 6.1.7.6)
|
144
|
+
method_source
|
145
|
+
rake (>= 12.2)
|
146
|
+
thor (~> 1.0)
|
147
|
+
rainbow (3.1.1)
|
148
|
+
rake (13.0.6)
|
149
|
+
regexp_parser (2.8.1)
|
150
|
+
rexml (3.2.6)
|
151
|
+
rspec-core (3.12.2)
|
152
|
+
rspec-support (~> 3.12.0)
|
153
|
+
rspec-expectations (3.12.3)
|
154
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
155
|
+
rspec-support (~> 3.12.0)
|
156
|
+
rspec-mocks (3.12.6)
|
157
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
158
|
+
rspec-support (~> 3.12.0)
|
159
|
+
rspec-rails (5.1.2)
|
160
|
+
actionpack (>= 5.2)
|
161
|
+
activesupport (>= 5.2)
|
162
|
+
railties (>= 5.2)
|
163
|
+
rspec-core (~> 3.10)
|
164
|
+
rspec-expectations (~> 3.10)
|
165
|
+
rspec-mocks (~> 3.10)
|
166
|
+
rspec-support (~> 3.10)
|
167
|
+
rspec-support (3.12.1)
|
168
|
+
rubocop (1.56.4)
|
169
|
+
base64 (~> 0.1.1)
|
170
|
+
json (~> 2.3)
|
171
|
+
language_server-protocol (>= 3.17.0)
|
172
|
+
parallel (~> 1.10)
|
173
|
+
parser (>= 3.2.2.3)
|
174
|
+
rainbow (>= 2.2.2, < 4.0)
|
175
|
+
regexp_parser (>= 1.8, < 3.0)
|
176
|
+
rexml (>= 3.2.5, < 4.0)
|
177
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
178
|
+
ruby-progressbar (~> 1.7)
|
179
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
180
|
+
rubocop-ast (1.29.0)
|
181
|
+
parser (>= 3.2.1.0)
|
182
|
+
ruby-progressbar (1.13.0)
|
183
|
+
sprockets (4.2.1)
|
184
|
+
concurrent-ruby (~> 1.0)
|
185
|
+
rack (>= 2.2.4, < 4)
|
186
|
+
sprockets-rails (3.4.2)
|
187
|
+
actionpack (>= 5.2)
|
188
|
+
activesupport (>= 5.2)
|
189
|
+
sprockets (>= 3.0.0)
|
190
|
+
thor (1.2.2)
|
191
|
+
timeout (0.4.0)
|
192
|
+
tzinfo (2.0.6)
|
193
|
+
concurrent-ruby (~> 1.0)
|
194
|
+
unicode-display_width (2.5.0)
|
195
|
+
websocket-driver (0.7.6)
|
196
|
+
websocket-extensions (>= 0.1.0)
|
197
|
+
websocket-extensions (0.1.5)
|
198
|
+
zeitwerk (2.6.12)
|
199
|
+
|
200
|
+
PLATFORMS
|
201
|
+
arm64-darwin-22
|
202
|
+
|
203
|
+
DEPENDENCIES
|
204
|
+
factory_bot_rails
|
205
|
+
rails (~> 6.0)
|
206
|
+
rails_json_seeder!
|
207
|
+
rake (~> 13.0)
|
208
|
+
rspec-rails (~> 5.0)
|
209
|
+
rubocop (~> 1.21)
|
210
|
+
|
211
|
+
BUNDLED WITH
|
212
|
+
2.4.13
|
data/README.md
CHANGED
@@ -1,39 +1,155 @@
|
|
1
1
|
# RailsJsonSeeder
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_json_seeder`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
RailsJsonSeeder is a gem to easily read JSON seed data for Rails applications and reflect it in the database.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
-
|
11
7
|
Install the gem and add to the application's Gemfile by executing:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
8
|
+
```
|
9
|
+
$ gem install rails_json_seeder
|
10
|
+
```
|
11
|
+
|
12
|
+
## Quickstart
|
13
|
+
### Setup
|
14
|
+
First, call RailsJsonSeeder in the seed.rb file as follows:
|
15
|
+
```
|
16
|
+
RailsJsonSeeder.load_seeds('db/seed_config.yml')
|
17
|
+
```
|
18
|
+
|
19
|
+
### Creating a configuration file
|
20
|
+
`seed_config`
|
21
|
+
```
|
22
|
+
seed_file_base_path: 'db/seed_json/' # default value is db/
|
23
|
+
files:
|
24
|
+
'users.json':
|
25
|
+
model: User
|
26
|
+
'categories.json':
|
27
|
+
model: Category
|
28
|
+
'books.json':
|
29
|
+
model: Book
|
30
|
+
dependencies:
|
31
|
+
- class: Category
|
32
|
+
'user_books.json':
|
33
|
+
model: UserBook
|
34
|
+
dependencies:
|
35
|
+
- class: User
|
36
|
+
- class: Book
|
37
|
+
```
|
38
|
+
|
39
|
+
### Creating JSON files
|
40
|
+
`user_books.json`
|
41
|
+
```
|
42
|
+
[
|
43
|
+
{
|
44
|
+
"first_name": "John",
|
45
|
+
"last_name": "Doe"
|
46
|
+
},
|
47
|
+
{
|
48
|
+
"first_name": "Bob",
|
49
|
+
"last_name": "Brown"
|
50
|
+
}
|
51
|
+
]
|
52
|
+
```
|
53
|
+
`categories.json`
|
54
|
+
```
|
55
|
+
[
|
56
|
+
{
|
57
|
+
"label": "Science Fiction"
|
58
|
+
},
|
59
|
+
{
|
60
|
+
"label": "History"
|
61
|
+
}
|
62
|
+
]
|
63
|
+
```
|
64
|
+
`books.json`
|
65
|
+
```
|
66
|
+
[
|
67
|
+
{
|
68
|
+
"title": "The Time Traveler's Tale",
|
69
|
+
"description": "An intriguing story of a man who discovers a machine that can transport him through time.",
|
70
|
+
"category": {
|
71
|
+
"label": "Science Fiction"
|
72
|
+
}
|
73
|
+
},
|
74
|
+
{
|
75
|
+
"title": "Stars Beyond Reach",
|
76
|
+
"description": "Set in a distant future, a tale of interstellar exploration and the challenges faced by humanity in the vastness of space.",
|
77
|
+
"category": {
|
78
|
+
"label": "Science Fiction"
|
79
|
+
}
|
80
|
+
},
|
81
|
+
{
|
82
|
+
"title": "Rise and Fall of Empires",
|
83
|
+
"description": "An exhaustive study of the world's greatest empires, their dominance, and eventual decline.",
|
84
|
+
"category": {
|
85
|
+
"label": "History"
|
86
|
+
}
|
87
|
+
}
|
88
|
+
]
|
89
|
+
```
|
90
|
+
`user_books.json`
|
91
|
+
```
|
92
|
+
[
|
93
|
+
{
|
94
|
+
"book": {
|
95
|
+
"title": "The Time Traveler's Tale"
|
96
|
+
},
|
97
|
+
"user": {
|
98
|
+
"first_name": "John",
|
99
|
+
"last_name": "Doe"
|
100
|
+
}
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"book": {
|
104
|
+
"title": "Rise and Fall of Empires"
|
105
|
+
},
|
106
|
+
"user": {
|
107
|
+
"first_name": "Bob",
|
108
|
+
"last_name": "Brown"
|
109
|
+
}
|
110
|
+
}
|
111
|
+
]
|
112
|
+
```
|
113
|
+
|
114
|
+
|
115
|
+
## Contributions
|
116
|
+
🌱 Early Days of Our Project: We'd like to candidly express that RailsJsonSeeder is in its infancy. As with many open-source software (OSS) projects in their early stages, there may be areas that are not yet polished or might require further refinements.
|
117
|
+
|
118
|
+
🤝 Your Contribution Matters: Despite our project's budding phase, or perhaps because of it, every contribution holds significant value. Whether you're a veteran open-source contributor or a newcomer, your insights, feedback, and contributions can make a real difference.
|
119
|
+
|
120
|
+
### How Can You Help?
|
121
|
+
1. Identifying Bugs: If you notice any bugs or inconsistencies, please open an issue. Your detailed observations can help us rectify and improve.
|
122
|
+
2. Suggesting Enhancements: Have ideas on how we can do better? Propose enhancements or new features. We're all ears!
|
123
|
+
3. Code Contributions: Directly contribute to the codebase following the previously mentioned contribution steps. If you're uncertain about something, feel free to ask.
|
124
|
+
4. Documentation & Examples: Found a typo? Think a section of our documentation could be clearer? Your feedback can make our documentation more user-friendly.
|
125
|
+
5. Spreading the Word: The more people know about our project, the better. Share our project, write about it, or even give us a shoutout.
|
126
|
+
|
127
|
+
### An Invitation to All:
|
128
|
+
This is a space of collaboration, learning, and growth. We're committed to fostering a welcoming and inclusive environment for everyone. If you ever have questions, feedback, or suggestions, please don't hesitate to reach out.
|
129
|
+
|
130
|
+
Together, let's make RailsJsonSeeder a valuable tool for the Rails community!
|
131
|
+
|
132
|
+
### How to start development
|
133
|
+
#### clone repo
|
134
|
+
```
|
135
|
+
$ git clone git@github.com:yamamoto7/rails_json_seeder.git
|
136
|
+
$ cd rails_json_seeder
|
137
|
+
```
|
138
|
+
|
139
|
+
#### add or edit code
|
140
|
+
```
|
141
|
+
├─ lib/
|
142
|
+
├─ rails_json_seeder.rb # entry point
|
143
|
+
└─ rails_json_seeder/
|
144
|
+
└─ seeder.rb # core functions
|
145
|
+
```
|
146
|
+
|
147
|
+
#### try features
|
148
|
+
```
|
149
|
+
$ cd test/dummy/
|
150
|
+
$ rails db:seed
|
151
|
+
```
|
32
152
|
|
33
153
|
## License
|
34
|
-
|
35
154
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
36
155
|
|
37
|
-
## Code of Conduct
|
38
|
-
|
39
|
-
Everyone interacting in the RailsJsonSeeder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails_json_seeder/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'rails'
|
3
|
+
require 'active_support/core_ext/hash/keys'
|
4
|
+
require 'yaml'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module RailsJsonSeeder
|
8
|
+
# Provides functionality to load seed data from JSON files for Rails applications.
|
9
|
+
# This module encapsulates the logic to read configurations, parse JSON files,
|
10
|
+
# and seed the database with the provided data.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# Seeder.load_seeds('path_to/seed_config.yml')
|
14
|
+
#
|
15
|
+
class Seeder
|
16
|
+
def load_seeds(config_path = 'db/seed_config.yml')
|
17
|
+
config = load_config(config_path)
|
18
|
+
process_seeds(config)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def load_config(config_path)
|
24
|
+
YAML.load_file(config_path).deep_symbolize_keys.tap do |config|
|
25
|
+
config[:files] = config[:files].transform_keys(&:to_s).transform_values(&:symbolize_keys)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_seeds(config)
|
30
|
+
raise 'Rails.root is nil. This code must be executed in a Rails context.' if Rails.root.nil?
|
31
|
+
|
32
|
+
seed_file_base_path = config[:seed_file_base_path] ? Rails.root.join(config[:seed_file_base_path]) : Rails.root
|
33
|
+
config[:files].each do |file, item|
|
34
|
+
puts "Processing: #{seed_file_base_path.join(file)}"
|
35
|
+
process_file(seed_file_base_path.join(file), item)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def process_file(path, item)
|
40
|
+
klass = resolve_constant(item[:model])
|
41
|
+
|
42
|
+
dependencies = item[:dependencies] || {}
|
43
|
+
records = JSON.parse(File.read(path))
|
44
|
+
|
45
|
+
ActiveRecord::Base.transaction do
|
46
|
+
records.each do |record|
|
47
|
+
resolve_dependencies(record, dependencies)
|
48
|
+
klass.find_or_create_by!(record)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
puts "Finished: #{klass}"
|
53
|
+
rescue Errno::ENOENT
|
54
|
+
puts "Could not find file at #{path}"
|
55
|
+
rescue JSON::ParserError
|
56
|
+
puts "Could not parse JSON in file at #{path}"
|
57
|
+
rescue StandardError => e
|
58
|
+
puts "An error occurred at #{item}: #{e.message}"
|
59
|
+
pp e.backtrace
|
60
|
+
end
|
61
|
+
|
62
|
+
def resolve_constant(class_name)
|
63
|
+
model_name = class_name.constantize
|
64
|
+
raise "Could not find class #{class_name}." unless Object.const_defined?(class_name)
|
65
|
+
|
66
|
+
model_name
|
67
|
+
rescue NameError
|
68
|
+
raise "Could not find class #{class_name}. Please check your seed configuration."
|
69
|
+
end
|
70
|
+
|
71
|
+
def resolve_dependencies(record, dependencies)
|
72
|
+
dependencies.each do |dependency|
|
73
|
+
dependency_class = resolve_constant(dependency[:class])
|
74
|
+
foreign_key = dependency[:id] || "#{dependency_class.name.underscore}_id"
|
75
|
+
original_field_name = dependency[:alias] || dependency_class.name.underscore
|
76
|
+
dependent_record = dependency_class.find_by!(record[original_field_name])
|
77
|
+
record[foreign_key] = dependent_record.id
|
78
|
+
record.delete(original_field_name)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/rails_json_seeder.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
3
|
+
require_relative 'rails_json_seeder/version'
|
4
|
+
require_relative 'rails_json_seeder/seeder'
|
5
5
|
|
6
|
+
# Main module for RailsJsonSeeder
|
6
7
|
module RailsJsonSeeder
|
7
8
|
class Error < StandardError; end
|
8
|
-
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def load_seeds(config_path)
|
12
|
+
puts 'Loading seeds by RailsJsonSeeder...'
|
13
|
+
Seeder.new.load_seeds(config_path)
|
14
|
+
end
|
15
|
+
end
|
9
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_json_seeder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kenta.yamamoto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: RailsJsonSeeder provides an intuitive way to seed Rails applications
|
14
14
|
using JSON formatted seed files. With support for defining model dependencies, this
|
@@ -20,16 +20,18 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
|
+
- ".editorconfig"
|
23
24
|
- ".rubocop.yml"
|
24
25
|
- CHANGELOG.md
|
25
26
|
- CODE_OF_CONDUCT.md
|
26
27
|
- Gemfile
|
28
|
+
- Gemfile.lock
|
27
29
|
- LICENSE.txt
|
28
30
|
- README.md
|
29
31
|
- Rakefile
|
30
32
|
- lib/rails_json_seeder.rb
|
33
|
+
- lib/rails_json_seeder/seeder.rb
|
31
34
|
- lib/rails_json_seeder/version.rb
|
32
|
-
- rails_json_seeder.gemspec
|
33
35
|
- sig/rails_json_seeder.rbs
|
34
36
|
homepage: https://github.com/yamamoto7/rails_json_seeder
|
35
37
|
licenses:
|
data/rails_json_seeder.gemspec
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "lib/rails_json_seeder/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = "rails_json_seeder"
|
7
|
-
spec.version = RailsJsonSeeder::VERSION
|
8
|
-
spec.authors = ["kenta.yamamoto"]
|
9
|
-
spec.email = ["yamamoto7.pgming@gmail.com"]
|
10
|
-
|
11
|
-
spec.summary = "A gem for easy seeding of Rails projects from JSON files."
|
12
|
-
spec.description = "RailsJsonSeeder provides an intuitive way to seed Rails applications " \
|
13
|
-
"using JSON formatted seed files. With support for defining model dependencies, " \
|
14
|
-
"this gem makes seeding complex applications a breeze. Perfect for projects that " \
|
15
|
-
"want to keep their seeding process clean and maintainable."
|
16
|
-
|
17
|
-
|
18
|
-
spec.homepage = "https://github.com/yamamoto7/rails_json_seeder"
|
19
|
-
spec.license = "MIT"
|
20
|
-
spec.required_ruby_version = ">= 2.6.0"
|
21
|
-
|
22
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
-
spec.metadata["source_code_uri"] = "https://github.com/yamamoto7/rails_json_seeder"
|
24
|
-
spec.metadata["changelog_uri"] = "https://github.com/yamamoto7/rails_json_seeder/blob/main/CHANGELOG.md"
|
25
|
-
|
26
|
-
# Specify which files should be added to the gem when it is released.
|
27
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
-
spec.files = Dir.chdir(__dir__) do
|
29
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
30
|
-
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
31
|
-
end
|
32
|
-
end
|
33
|
-
spec.bindir = "exe"
|
34
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
35
|
-
spec.require_paths = ["lib"]
|
36
|
-
|
37
|
-
# Uncomment to register a new dependency of your gem
|
38
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
39
|
-
|
40
|
-
# For more information and examples about making a new gem, check out our
|
41
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
42
|
-
end
|