rails_json_seeder 0.1.0 → 0.1.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: 351aacc798148986d4366f6e1b47b7502e786601cb87c1d56c944d0d65e8b800
4
- data.tar.gz: aa085a37cda6f63cdb45093cfa186b2ec5ec978ff51f433ae5adeccefd0bf98f
3
+ metadata.gz: 92ad673104e2356514757c6ca7b6c7e7c301a93ed17499a48ea7eacf67142270
4
+ data.tar.gz: 1500a9b9f478b034504f9eeea268744b22db60e5efa15f7b9618065b6d7dbe3a
5
5
  SHA512:
6
- metadata.gz: 8455da430de27e8b9cae305b4e6dd04f96402f03845a416784487a5ad880b53c7bf6d1c3a383f9502d91be826689d3ab6073c46a3bfe15a9749a0eaa4981f3aa
7
- data.tar.gz: d6e469c703325f643ad5b6c1431237f912e937d90b76f5c75053268848a6c7ffada48e10ade04d3426e979ed231466b0aa36052b9cb930e729a0feeb02f332ce
6
+ metadata.gz: 04d440f3ed93818fed9adcffebcbf1eb160e972aac6bbe93d51bf8cc9c8ade722a94b978eb54b04983074ee97de83292c01b0880128e2372c10740c78ccfd5c4
7
+ data.tar.gz: f080a0df2f5cf50030608fee152d7bc1b4003b9c41c33bd46f0533f891a8395dcf9cf6a01a80522a875e3b9c0c73865c03a485a654273ee6854fc67483434838
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: double_quotes
6
+ EnforcedStyle: single_quotes
7
7
 
8
8
  Style/StringLiteralsInInterpolation:
9
9
  Enabled: true
@@ -11,3 +11,12 @@ 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
+
21
+ Metrics/BlockLength:
22
+ Max: 40
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,22 @@
1
+
2
+
3
+ ### How to start development
4
+ #### clone repo
5
+ ```
6
+ $ git clone git@github.com:yamamoto7/rails_json_seeder.git
7
+ $ cd rails_json_seeder
8
+ ```
9
+
10
+ #### add or edit code
11
+ ```
12
+ ├─ lib/
13
+ ├─ rails_json_seeder.rb # entry point
14
+ └─ rails_json_seeder/
15
+ └─ seeder.rb # core functions
16
+ ```
17
+
18
+ #### try features
19
+ ```
20
+ $ cd test/dummy/
21
+ $ rails db:seed
22
+ ```
data/Gemfile CHANGED
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
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 "rake", "~> 13.0"
9
-
10
- gem "rubocop", "~> 1.21"
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,216 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_json_seeder (0.1.2)
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
+ mini_portile2 (2.8.8)
99
+ minitest (5.20.0)
100
+ net-imap (0.4.1)
101
+ date
102
+ net-protocol
103
+ net-pop (0.1.2)
104
+ net-protocol
105
+ net-protocol (0.2.1)
106
+ timeout
107
+ net-smtp (0.4.0)
108
+ net-protocol
109
+ nio4r (2.5.9)
110
+ nokogiri (1.15.4)
111
+ mini_portile2 (~> 2.8.2)
112
+ racc (~> 1.4)
113
+ parallel (1.23.0)
114
+ parser (3.2.2.4)
115
+ ast (~> 2.4.1)
116
+ racc
117
+ racc (1.7.1)
118
+ rack (2.2.8)
119
+ rack-test (2.1.0)
120
+ rack (>= 1.3)
121
+ rails (6.1.7.6)
122
+ actioncable (= 6.1.7.6)
123
+ actionmailbox (= 6.1.7.6)
124
+ actionmailer (= 6.1.7.6)
125
+ actionpack (= 6.1.7.6)
126
+ actiontext (= 6.1.7.6)
127
+ actionview (= 6.1.7.6)
128
+ activejob (= 6.1.7.6)
129
+ activemodel (= 6.1.7.6)
130
+ activerecord (= 6.1.7.6)
131
+ activestorage (= 6.1.7.6)
132
+ activesupport (= 6.1.7.6)
133
+ bundler (>= 1.15.0)
134
+ railties (= 6.1.7.6)
135
+ sprockets-rails (>= 2.0.0)
136
+ rails-dom-testing (2.2.0)
137
+ activesupport (>= 5.0.0)
138
+ minitest
139
+ nokogiri (>= 1.6)
140
+ rails-html-sanitizer (1.6.0)
141
+ loofah (~> 2.21)
142
+ nokogiri (~> 1.14)
143
+ railties (6.1.7.6)
144
+ actionpack (= 6.1.7.6)
145
+ activesupport (= 6.1.7.6)
146
+ method_source
147
+ rake (>= 12.2)
148
+ thor (~> 1.0)
149
+ rainbow (3.1.1)
150
+ rake (13.0.6)
151
+ regexp_parser (2.8.1)
152
+ rexml (3.2.6)
153
+ rspec-core (3.12.2)
154
+ rspec-support (~> 3.12.0)
155
+ rspec-expectations (3.12.3)
156
+ diff-lcs (>= 1.2.0, < 2.0)
157
+ rspec-support (~> 3.12.0)
158
+ rspec-mocks (3.12.6)
159
+ diff-lcs (>= 1.2.0, < 2.0)
160
+ rspec-support (~> 3.12.0)
161
+ rspec-rails (5.1.2)
162
+ actionpack (>= 5.2)
163
+ activesupport (>= 5.2)
164
+ railties (>= 5.2)
165
+ rspec-core (~> 3.10)
166
+ rspec-expectations (~> 3.10)
167
+ rspec-mocks (~> 3.10)
168
+ rspec-support (~> 3.10)
169
+ rspec-support (3.12.1)
170
+ rubocop (1.56.4)
171
+ base64 (~> 0.1.1)
172
+ json (~> 2.3)
173
+ language_server-protocol (>= 3.17.0)
174
+ parallel (~> 1.10)
175
+ parser (>= 3.2.2.3)
176
+ rainbow (>= 2.2.2, < 4.0)
177
+ regexp_parser (>= 1.8, < 3.0)
178
+ rexml (>= 3.2.5, < 4.0)
179
+ rubocop-ast (>= 1.28.1, < 2.0)
180
+ ruby-progressbar (~> 1.7)
181
+ unicode-display_width (>= 2.4.0, < 3.0)
182
+ rubocop-ast (1.29.0)
183
+ parser (>= 3.2.1.0)
184
+ ruby-progressbar (1.13.0)
185
+ sprockets (4.2.1)
186
+ concurrent-ruby (~> 1.0)
187
+ rack (>= 2.2.4, < 4)
188
+ sprockets-rails (3.4.2)
189
+ actionpack (>= 5.2)
190
+ activesupport (>= 5.2)
191
+ sprockets (>= 3.0.0)
192
+ thor (1.2.2)
193
+ timeout (0.4.0)
194
+ tzinfo (2.0.6)
195
+ concurrent-ruby (~> 1.0)
196
+ unicode-display_width (2.5.0)
197
+ websocket-driver (0.7.6)
198
+ websocket-extensions (>= 0.1.0)
199
+ websocket-extensions (0.1.5)
200
+ zeitwerk (2.6.12)
201
+
202
+ PLATFORMS
203
+ arm64-darwin-22
204
+ arm64-darwin-23
205
+ x86_64-linux
206
+
207
+ DEPENDENCIES
208
+ factory_bot_rails
209
+ rails (~> 6.0)
210
+ rails_json_seeder!
211
+ rake (~> 13.0)
212
+ rspec-rails (~> 5.0)
213
+ rubocop (~> 1.21)
214
+
215
+ BUNDLED WITH
216
+ 2.4.13
data/README.md CHANGED
@@ -1,39 +1,49 @@
1
1
  # RailsJsonSeeder
2
+ [English ver](https://github.com/yamamoto7/rails_json_seeder/blob/main/README_EN.md)
2
3
 
3
- TODO: Delete this and the text below, and describe your gem
4
+ [![Gem Version](https://badge.fury.io/rb/rails_json_seeder.svg)](https://badge.fury.io/rb/rails_json_seeder) [![Open Source Helpers badge](https://codetriage.com/yamamoto7/rails_json_seeder/badges/users.svg)](https://codetriage.com/yamamoto7/rails_json_seeder)
4
5
 
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.
6
+ ![rails_json_seeder](https://github.com/yamamoto7/rails_json_seeder/assets/37437497/4923637f-baf9-4e98-97e9-8d2d40539ab7)
6
7
 
7
- ## Installation
8
+ Json形式で定義されたシードデータを簡単に取り込むことができるgemです
8
9
 
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
+ ### Install
12
+ Gemfileに本gemを追加してください。
13
+ ```
14
+ gem 'rails_json_seeder'
15
+ ```
10
16
 
11
- Install the gem and add to the application's Gemfile by executing:
17
+ 追加後以下のコマンドを実行してください。
18
+ ```
19
+ $ bundle install
20
+ ```
12
21
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
22
+ ### SeedデータでRailsJsonSeederを利用する例
14
23
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
24
+ 1. [seed.rbの例](https://github.com/yamamoto7/rails_json_seeder/blob/main/test/dummy/db/seeds.rb)
25
+ 2. [設定ファイルの例](https://github.com/yamamoto7/rails_json_seeder/blob/main/test/dummy/db/seed_config_sample_1.yml)
26
+ 3. [jsonファイルの例](https://github.com/yamamoto7/rails_json_seeder/tree/main/test/dummy/db/seed_json_sample_1)
16
27
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
28
+ ## 貢献
29
+ 🌱 プロジェクトの状態: 率直に申し上げますが、RailsJsonSeederはまだ初期段階にあります。初期段階にある多くのオープンソースソフトウェア (OSS) プロジェクトと同様、まだ洗練されていない部分や、さらなる改良が必要な部分があるかもしれません。
18
30
 
19
- ## Usage
31
+ 🤝 あなたの貢献が重要です: 私たちのプロジェクトは萌芽的な段階ですが、あるいは萌芽的だからこそ、すべての貢献は重要な価値を持ちます。ベテランのオープンソース貢献者であろうと、新参者であろうと、あなたの洞察力、フィードバック、そして貢献は、真の変化をもたらすことができます。
20
32
 
21
- TODO: Write usage instructions here
33
+ ### どのように貢献できますか?
34
+ 1. バグの特定: バグや矛盾にお気づきの場合は、issue を開いてください。あなたの詳細な観察は、私たちが修正し、改善するのに役立ちます。
35
+ 2. 機能強化の提案: より良いものを作るためのアイデアをお持ちですか?機能強化や新機能をご提案ください。お待ちしております!
36
+ 3. コードへの貢献: 前述の貢献ステップに従って、コードベースに直接貢献してください。不明な点があれば、遠慮なく質問してください。
37
+ 4. ドキュメントとサンプル: タイプミスを見つけましたか?私たちのドキュメントのセクションがより明確になると思いますか?あなたのフィードバックやPull Requestは、私たちのドキュメントをよりユーザーフレンドリーにすることができます。
38
+ 5. 初心者の方へ: PRの送り方がわからないですか?実装の相談がしたいですか?issueにて気軽にご質問ください。(※本OSSは日本語に対応しています)
22
39
 
23
- ## Development
40
+ 準備ができたら、こちらから開発手順をご確認ください。
24
41
 
25
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+ ### すべての人への招待状:
43
+ ここはコラボレーション、学習、成長の場です。私たちはすべての人を歓迎し、包括的な環境を育むことを約束します。質問、フィードバック、提案があれば、遠慮なく声をかけてください。
26
44
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
-
29
- ## Contributing
30
-
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_json_seeder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rails_json_seeder/blob/main/CODE_OF_CONDUCT.md).
45
+ 一緒にRailsJsonSeederをRailsコミュニティにとって価値あるツールにしていきましょう!
32
46
 
33
47
  ## License
34
-
35
48
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
49
 
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/README_EN.md ADDED
@@ -0,0 +1,47 @@
1
+ # RailsJsonSeeder
2
+ [![Gem Version](https://badge.fury.io/rb/rails_json_seeder.svg)](https://badge.fury.io/rb/rails_json_seeder) [![Open Source Helpers badge](https://codetriage.com/yamamoto7/rails_json_seeder/badges/users.svg)](https://codetriage.com/yamamoto7/rails_json_seeder)
3
+
4
+ ![rails_json_seeder](https://github.com/yamamoto7/rails_json_seeder/assets/37437497/4923637f-baf9-4e98-97e9-8d2d40539ab7)
5
+
6
+ RailsJsonSeeder is a gem to easily read JSON seed data for Rails applications and reflect it in the database.
7
+
8
+ ## Installation
9
+ Add this gem to your Gemfile.
10
+
11
+ ```
12
+ gem 'rails_json_seeder'
13
+ ```
14
+
15
+ After adding this gem, please execute the following commands.
16
+ ```
17
+ $ bundle install
18
+ ```
19
+
20
+ ### Example of using RailsJsonSeeder with Seed data
21
+
22
+ 1. [Example of seed.rb](https://github.com/yamamoto7/rails_json_seeder/blob/main/test/dummy/db/seeds.rb)
23
+ 2. [Example of configuration file](https://github.com/yamamoto7/rails_json_seeder/blob/main/test/dummy/db/seed_config_sample_1.yml)
24
+ 3. [Example of json file](https://github.com/yamamoto7/rails_json_seeder/tree/main/test/dummy/db/seed_json_sample_1)
25
+
26
+ ## Contributions
27
+ 🌱 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.
28
+
29
+ 🤝 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.
30
+
31
+ ### How Can You Help?
32
+ 1. Identifying Bugs: If you notice any bugs or inconsistencies, please open an issue. Your detailed observations can help us rectify and improve.
33
+ 2. Suggesting Enhancements: Have ideas on how we can do better? Propose enhancements or new features. We're all ears!
34
+ 3. Code Contributions: Directly contribute to the codebase following the previously mentioned contribution steps. If you're uncertain about something, feel free to ask.
35
+ 4. Documentation & Examples: Found a typo? Think a section of our documentation could be clearer? Your feedback can make our documentation more user-friendly.
36
+ 5. Newbies: Not sure how to send a PR? Want to discuss implementation? Please feel free to ask questions on the [issue](https://github.com/yamamoto7/rails_json_seeder/issues).
37
+
38
+ When you are ready, please click [here](https://github.com/yamamoto7/rails_json_seeder/blob/main/CONTRIBUTING.md) to review the development process.
39
+
40
+ ### An Invitation to All:
41
+ 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.
42
+
43
+ Together, let's make RailsJsonSeeder a valuable tool for the Rails community!
44
+
45
+ ## License
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
47
+
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rubocop/rake_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'rubocop/rake_task'
5
5
 
6
6
  RuboCop::RakeTask.new
7
7
 
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'rails'
5
+ require 'active_support/core_ext/hash/keys'
6
+ require 'yaml'
7
+ require 'json'
8
+
9
+ module RailsJsonSeeder
10
+ # Provides functionality to load seed data from JSON files for Rails applications.
11
+ # This module encapsulates the logic to read configurations, parse JSON files,
12
+ # and seed the database with the provided data.
13
+ #
14
+ # Example:
15
+ # Seeder.load_seeds('path_to/seed_config.yml')
16
+ #
17
+ class Seeder
18
+ def load_seeds(config_path = 'db/seed_config.yml')
19
+ config = load_config(config_path)
20
+ process_seeds(config)
21
+ end
22
+
23
+ private
24
+
25
+ def load_config(config_path)
26
+ YAML.load_file(config_path).deep_symbolize_keys.tap do |config|
27
+ config[:files] = config[:files].transform_keys(&:to_s).transform_values(&:symbolize_keys)
28
+ end
29
+ end
30
+
31
+ def process_seeds(config)
32
+ raise 'Rails.root is nil. This code must be executed in a Rails context.' if Rails.root.nil?
33
+
34
+ seed_file_base_path = config[:seed_file_base_path] ? Rails.root.join(config[:seed_file_base_path]) : Rails.root
35
+ config[:files].each do |file, item|
36
+ puts "Processing: #{seed_file_base_path.join(file)}"
37
+ process_file(seed_file_base_path.join(file), item)
38
+ end
39
+ end
40
+
41
+ def process_file(path, item) # rubocop:disable Metrics/AbcSize
42
+ klass = resolve_constant(item[:model])
43
+
44
+ dependencies = item[:dependencies] || {}
45
+ records = JSON.parse(File.read(path))
46
+
47
+ ActiveRecord::Base.transaction do
48
+ records.each do |record|
49
+ resolve_dependencies(record, dependencies)
50
+ klass.find_or_create_by!(record)
51
+ end
52
+ end
53
+
54
+ puts "Finished: #{klass}"
55
+ rescue Errno::ENOENT
56
+ puts "Could not find file at #{path}"
57
+ rescue JSON::ParserError
58
+ puts "Could not parse JSON in file at #{path}"
59
+ rescue StandardError => e
60
+ puts "An error occurred at #{item}: #{e.message}"
61
+ pp e.backtrace
62
+ end
63
+
64
+ def resolve_constant(class_name)
65
+ model_name = class_name.constantize
66
+ raise "Could not find class #{class_name}." unless Object.const_defined?(class_name)
67
+
68
+ model_name
69
+ rescue NameError
70
+ raise "Could not find class #{class_name}. Please check your seed configuration."
71
+ end
72
+
73
+ def resolve_dependencies(record, dependencies)
74
+ dependencies.each do |dependency|
75
+ dependency_class = resolve_constant(dependency[:class])
76
+ foreign_key = dependency[:id] || "#{dependency_class.name.underscore}_id"
77
+ original_field_name = dependency[:alias] || dependency_class.name.underscore
78
+
79
+ next unless record[original_field_name]
80
+
81
+ dependent_record = dependency_class.find_by!(record[original_field_name])
82
+ record[foreign_key] = dependent_record.id
83
+ record.delete(original_field_name)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsJsonSeeder
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.2'
5
5
  end
@@ -1,9 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "rails_json_seeder/version"
4
- require_relative "src/seeder"
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
- # Your code goes here...
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.0
4
+ version: 0.1.2
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-10 00:00:00.000000000 Z
11
+ date: 2024-12-23 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,20 @@ 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
27
+ - CONTRIBUTING.md
26
28
  - Gemfile
29
+ - Gemfile.lock
27
30
  - LICENSE.txt
28
31
  - README.md
32
+ - README_EN.md
29
33
  - Rakefile
30
34
  - lib/rails_json_seeder.rb
35
+ - lib/rails_json_seeder/seeder.rb
31
36
  - lib/rails_json_seeder/version.rb
32
- - rails_json_seeder.gemspec
33
37
  - sig/rails_json_seeder.rbs
34
38
  homepage: https://github.com/yamamoto7/rails_json_seeder
35
39
  licenses:
@@ -53,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
57
  - !ruby/object:Gem::Version
54
58
  version: '0'
55
59
  requirements: []
56
- rubygems_version: 3.4.10
60
+ rubygems_version: 3.5.11
57
61
  signing_key:
58
62
  specification_version: 4
59
63
  summary: A gem for easy seeding of Rails projects from JSON files.
@@ -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