![My resume Raging against the machine with the Office Space crew.](https://user-images.githubusercontent.com/12698076/249662222-43db25e7-810c-461d-b4dc-b84f9b426dac.jpeg)
evanthegrayt / planter Goto Github PK
View Code? Open in Web Editor NEW🪴 Easily seed your rails application.
License: MIT License
🪴 Easily seed your rails application.
License: MIT License
"If you manually run docker-compose run app bundle install, but then docker system prune, the newly installed gems don’t persist unless you build. Maybe this is expected Docker behavior though?" - @evanthegrayt
There's no need to create the record through the parent's association. Just pluck the ids of the parent model.
The user should just pass the name of the belongs_to
association in the model. Then, the parent model, foreign key, and primary key can all be determined by doing the following.
model.constantize.reflect_on_association(parent).options
rails g planter
should generate the files/directories needed for the plugin to run.
Currently, all records are created with first_or_create!
. It might be worth having a raise: false
option in the seeding_method
macro that would use first_or_create
(no bang).
Currently, we have to test generators manually. This should be part of the automated test suite.
'<>'
trim mode default for ERB is problematic for rows that have erb at beginning and end of row. We should remove the trim mode default, but allow an option to be passed that sets it.
Allowed options should be
% enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
> omit newline for lines ending in %>
- omit blank lines ending in -%>
The Seeder
class is too big. There should be subclasses and modules that should be included based on seeding_method
, parent_model
, etc.
Rails 7 breaks the gem.
Add a way to specify
rails generate planter:seeder users --csv
rails generate planter:seeder users --data_array
rails generate planter:seeder users --custom
Depending on what's passed, the generator adds the correct text. The --csv
option will also add the csv file, and add the field names as the headers, pulled from that table.
Currently, if I make a migration for the dummy project, I have to docker-compose run app bash
, cd test/dummy
, and run bin/rails db:migrate
. Surely there's a way to do this from docker-compose.yml
. I tried adding && cd test/dummy && bin/rails db:migrate
to the command
, but it didn't seem to work.
Make sure the seeder seeds.
Add the ability to specify which columns to search by, and the rest will be used in during the creation if it doesn't already exist.
Example:
# users_seeder.rb
seeding_method :csv, unique_columns: %i[username email]
Assuming the CSV contains the following
username,email,bio
evanthegrayt,[email protected],I'm a developer
The seeder would then do User.where(username: 'evanthegrayt', email: '[email protected]').first_or_create!(bio: "I'm a developer")
Also look into using these methods: create_with
and find_or_create_by
.
I've seen @hkn34 implement a nice progress bar in some of our work projects. This feature would be a great addition.
There should be a config option to enable/disable it. Probably default to true
.
Planter.configure do |config|
config.progress_bar = true
end
It would also be nice to have a flag for the command line that can also enable/disable it at runtime, in case you want to change it for one run only. It would be a pain to edit the initializer every time. If not a flag, than an environmental variable?
rails planter:seed --progress
# or
rails planter:seed PROGRESS=true
Add contribution guidelines so people know how to build in docker and run tests.
Make a Version model like I have in standupmd.
Currently, create_records
will transform "NULL"
to nil
, but create_records_from_parent
doesn't. No should be the default, but we should add a transform_null: true
option to seeding_method
.
The name tables
is misleading; it's actually used to call the seeder files.
Host docs on gh pages.
If someone was to add Planter.seed
into db/seeds.rb
and they didn't populate config.seeders
in initializers/planter.rb
this wowuld cause rails to abort, maybe a warning message should be shown instead?
User should be able to specify use ERB, or put .csv.erb
as the filetype.
seeding_method :standard_csv, erb: true
Or name the file user.csv.erb
More examples of seeding_method
options should be in the readme.
When seeding people from csv and phone belongs to another table(contact_informations), then we should be able to detect what can be searched for in the Person.where query.
first_name,last_name,phone
Evan,Gray,123-456-7890
Person.where(first_name: 'Evan', last_name: 'Gray').first_or_create!(phone: '123-456-7890')
People may wish to keep db:seed
task separate from planter. We should add our own task.
Should be able to test this project in Docker.
New ruby is out. Need to make sure it's compatible.
When importing from CSV, there are often cases where the value needs to be manipulated in some form. I solved this problem in a project at work by adding a COLUMN_TRANSFORMATIONS
hash. Basically, you define this hash in your seeder, where the keys are the column name, and the values are lambdas to be run on the value from the CSV (which is the argument to the lambda).
For example, if you have a column called time
, and it's a string (always, because it's a CSV) that needs to be converted to a date object, and a field called transportation_needed
that's 'yes' or 'no' in the CSV, but it needs to be a boolean, you can define a hash like so.
COLUMN_TRANSFORMATIONS = {
time: ->(v) { DateTime.strptime(v, '%m/%d/%Y %I:%M:%S %p') },
transportation_needed: ->(v) { v[0].upcase == 'Y' }
}.freeze
Then, when the seeder runs, if a transformation exists for the current column, it will be automatically transformed before it's imported via a map
.
We're not always in control of csv exports, and sometimes they contain columns that aren't in the table being seeded. We need a way to either determine what the valid fields are, or let the user specify which ones to exclude.
When deciding whether to use erb
when seeding from csv, the order currently matters. We should change the regex to
/\.erb(?:.csv)?$/
This will allow for .csv.erb
OR .erb.csv
to be the file extension.
Given
class PropertiesSeeder < Planter::Seeder
seeding_method :data_array, number_of_records: 4, parent: :user
def data
[{
street_1: Faker::Address.street_address,
city: Faker::Address.city,
state: Faker::Address.state_abbr,
zip: Faker::Address.zip
}]
end
end
it appears that it will not seed more than one record per parent.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.