auto_seed 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 570a80405f90bdf4c8b3aa55314ef5ec3388507c29cd8aa353ab4e82beec1cfc
4
- data.tar.gz: fcf0e87251ab915a047808aa55aa11780ff75c84f251d66e7825550556aedf83
3
+ metadata.gz: fcbda2a17e35171f9bf903093b8d32dd0fed8e7ae289f092edee39ebbf7e5e20
4
+ data.tar.gz: 38e0d57abbbde6e0cec647c4008fae09b1f06f99b3f12999553920a74989b892
5
5
  SHA512:
6
- metadata.gz: 9044caea2f2c2866202b65515de9db6bab7452bccb32a5f8710fcb516dd74f5d529523d2c50527ae7d5cf63848250f6f74a962d86face3350a569e4f2415dfca
7
- data.tar.gz: 3217d78dc3e51e06cda25a317ca1690c0e186c8af8476a423e3971eb81b4612c0d74183ef710bbf8c56cc1a6af0adb36100a46256a9a5e642eced50683bf143f
6
+ metadata.gz: 301efb1abe01e3ea3927bd1a69588fb179b1a11ecfdeb77e1b4622e22f9bf1140d158db0798d0b710ac9525a922d3d90716c4a23b317c6221d5775c1c5e10342
7
+ data.tar.gz: a0c2010af1460f10f27f1fd4fc068490b180e2a938404d21c1ae79255207420aa9ee5ad98f42ef320e8cb27a2955c4e22f0b7ea557b460b8ad17c34fd4632217
data/README.md CHANGED
@@ -1,39 +1,140 @@
1
1
  # AutoSeed
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- 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/auto_seed`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_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.
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'auto_seed'
17
+ ```
10
18
 
11
- Install the gem and add to the application's Gemfile by executing:
19
+ And then execute:
12
20
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+ bundle install
23
+ ```
14
24
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
25
+ Or install it yourself as:
16
26
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
27
+ ```
28
+ gem install auto_seed
29
+ ```
18
30
 
19
31
  ## Usage
20
32
 
21
- TODO: Write usage instructions here
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
97
+ To install this gem onto your local machine, run:
98
+
99
+ ```
100
+ bundle exec rake install
101
+ ```
26
102
 
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).
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/[USERNAME]/auto_seed. 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]/auto_seed/blob/main/CODE_OF_CONDUCT.md).
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
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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
- ## Code of Conduct
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
- Everyone interacting in the AutoSeed project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/auto_seed/blob/main/CODE_OF_CONDUCT.md).
140
+ Make sure to replace placeholders such as `your_username` with your actual GitHub username and adjust any other project-specific details as needed.
@@ -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 model.name.in?(%w[ApplicationRecord ActiveRecord::Base]) # Skip base classes
12
+ next if skip_model?(model)
7
13
 
8
14
  file.puts "## #{model.name} seeds"
9
15
 
10
16
  begin
11
- attributes = model.columns.reject { |col| col.name.in?(%w[ip_address current_sign_in_at]) }
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
- attributes.each do |attribute|
15
- file.puts " #{attribute.name}: 'sample_value',"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutoSeed
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shobhit Shukla