This question already has answers here:
Rails random records generator
(3 answers)
Closed 1 year ago.
I am new to Ruby and I want to load test my application written using Ruby on Rails frame and I need a tool/gem/library to generate random unique data to use it for the load testing since some functions such as user creation require unique data. Can you suggest a tool for that?
These are two gems used for creating objects in test setup with Ruby/Rails:
Factory Bot: This gem allows you to create instances of your active record models and instantiate them in your test setup in reproducible way:
https://github.com/thoughtbot/factory_bot_rails
Faker: Good for generating random bits of information, such as names, email addresses, phone numbers, text blobs, when 'foo bar' doesn't quite cut it:
https://github.com/faker-ruby/faker
Reading into these two should give you a start!
Related
This question already has an answer here:
Which plugins/gems should I use to dynamically generate thumbnails on-the-fly in Rails 3?
(1 answer)
Closed 9 years ago.
Is there any gem that allows you to generate automatic thumbnails with basic info in the frontpage?
Sites like indiegogo and kickstarter have thumbnails in groups of 3 or 4 per row. Each one represent a recent post. I am trying to do something like that.
I am a ruby noobie and would also give it a try if somebody explain me how to get this done with or without a gem. I could work with ruby or javascript.
Basically the point is to create posts and have them displayed as thumbnails in the index, in groups of 3.
Thanks!
Will the content come from your app? If yes, then I reckon you are looking for a way to layout your list of objects like how they did it in kickstarter and indiegogo. There are different libraries that you can use. Here are some of the libraries which you can use:
masonry: http://masonry.desandro.com/
isotope: https://github.com/desandro/isotope
packery: http://packery.metafizzy.co/
These libraries will help you to create brick / grid layouts.
Or, do you want to create previews from links? Then maybe this gem can help you as well: https://github.com/gottfrois/link_thumbnailer
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I must create an application using only tools available in ruby core or stdlib. Do YAML or SQLite come with ruby? What are some of the other tools available that would allow me to store data to a file? What are their advantages or disadvantages?
Ruby's stdlib is deep. Maybe too deep. I knew sqlite wasn't in there, but I figured something was. Here is what I found...
There are up to 4 different simple databases already in the stdlib:
PStore - Very simple persistent hash. Handles marshaling for you, so you can store trees of ruby objects. Pure ruby solution.
SDBM - C-based key/value store. Ruby ships with the entire source so it should be portable across platforms. Simple string keys and values only.
GDBM - Another string only key/value store. Uses GNU dbm. Its "enumerable" so its a little more hash-like. Possibly not very portable.
DBM - Uses the DBM headers available on the platform ruby was compiled on, so it could be one of several DBM implementations (read: not portable). Yet another string only key/value store. That's 3. Unlike GDBM though this one will allow you to store non-string values and silently ruin them by calling #to_s or #inspect.
I might actually use PStore for small things myself now. SQLite is probably better, but PStore is undoubtedly simpler so if the job is small enough it makes sense.
You can also use serialization. Marshal will dump actual ruby objects and their data. YAML can sort of do this as well. Using JSON/YAML/CSV you can finely control the format of the data. All of these can be used with File to write their output to a file.
You can you ruby's stdlib CSV library to store any database data. Its format is very useful, for storing, exporting, and importing DB data. See documentation on CSV here. As example, just do:
require 'csv'
# save
CSV.open("file.csv", "wb") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
...
end
#load
CSV.foreach("file.csv") do |row|
row # => ["row", "of", "CSV", "data"]
...
end
File.open 'local.rbdb', 'w+' do |f|
f.write JSON.generate(write_target)
End
Build your write_target data in a typical manner, and then use JSON as a storage format.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm brand new to Ruby and programming. I'd like to create a little program to automate one of my more tedious work tasks that I'm currently doing by hand but I'm not sure where to start.
People register to take courses through an online form, and I receive their registration information at the end of each day as a CSV document. I go line by line through that document and generate a confirmation email to send to them based on their input on the online form: the course they'd like to take, their room preference, how much they chose to pay for the course (sliding scale), etc. The email ends up looking something like this:
Dear So and so, Thank you for signing up for "Such-and-such An Awesome Course," with Professor Superdude. The course starts on Monday, September 1, 2030 at 4pm and ends on Thursday at 1pm. You paid such-and-such an amount...
et cetera. So ideally the program would take in the CSV document with information like "student name," "course title," "fee paid," and generate emails based on blocks of text ("Dear , Thank you for signing up for _,") and variables (the dates of the course) that are stored externally so they are easy to edit without going into the source code (maybe as CSV and plain text files).
Additionally, I need the output to be in rich text, so I can bold and underline certain things. I'm familiar with Markdown so I could use that in the source code but it would be ideal if the output could be rich text.
I'm not expecting anyone to write a program for me, but if you could let me know what I should look into or even what I should Google, that would be very helpful.
I assume you're trying to put together an email. If so, I'd probably start with a simple ERB template. If you want to generate HTML, you can write one HTML template and one plain text template; variable substitution works the same way for both, with the exception that you'll need to html-escape anything that contains characters that HTML considers special (ampersands, greater than, less then, for example). See ERB Documentation here.
If you're trying to parse CSV, user FasterCSV or a similar library. FasterCSV is documented here.
If you want to send an email, you can use ActionMailer, the mail gem, or the pony gem. ActionMailer is part of rails, but can be used independently. Pony is a good facade for creating email, as well; both ActionMailer and Pony depend on the "mail" gem, so unless you want to spend more time thinking about how email formats work, use one of those.
If you're not trying to send an email, and instead are trying to create a formatted document, you can still use ERB, but use it to generate output in TeX, or if you're more adventurous than I am, a Word compatible XML document. Alternatively, if you're wedded to Microsoft Word or RTF, you might try either http://ruby-rtf.rubyforge.org/ (Ruby RTF) or use COM/OLE interop to talk to Word, but I would only do that if really I had to; if I had to go that route, I'd probably suck it up and just use the built in mail merge feature in Word perhaps with a little VBA code.
I'm currently writing an rails application that generates some PDFs. The generated documents are things like proposals, invoices, order confirmations etc.
My intention is to provide 2 default layouts and to allow the user to request a custom layout. The user will never even see the generator code, this is all handled by trusted persons -> security is no concern at all.
My requirements:
Easy to change / add new generators (for developers, users never see those generators)
No redeployment
Works well with any number of generators
The way I see it, implementing those generators as regular classes in .rb files in the source code falls flat (redeployment required, file clutter with enough of them).
I'm currently thinking about the following and would like some input into the viability / better ways to do it:
The generator code is stored in the database and whenever a document is to be printed, the code is evaled in a scope where all the relevant variables are already set (document (-> line items | customer ..), pdf, user etc.) and the generator code just uses them.
My questions is basically: Is this the way to do it and if that's the case, are there some things I should be aware of?
I'm writing bug-tracking software in Ruby using Sinatra and MongoMapper. The problem is that the ticket IDs aren't very user friendly; they are composed of 24 hexadecimal digits (e.g. 4fcfa9e1c3e7f20bc2000004). I would like that the ticket IDs start at 1 and automatically increment (i.e. 1, 2, 3…). In MySQL this would be done using AUTO_INCREMENT.
I'm using MongoMapper, however. Is there a way to make the IDs start at 1 and then auto-increment, or do I need to store them separately?
If you have lots and lots of object creation, it may not scale b/c you have to hit the DB for every new object.
If you won't need that kind of scaling, there's a couple gems out there to allow incremental id's.
Pablo Cantero's gem, mongomapper_id2.
My gem, mongo_sequence.
Be on the lookout, there may be a few places where MongoMapper has special behavior for ObjectID's that doesn't work with integers. I at least know that as of a year and a half ago all the basic stuff works fine with integer id's.
The MongoDB docs list some strategies for auto-incrementing IDs.
In general, the problem with auto-incremented IDs is that they don't scale. With auto-increment, you need a single entity to provide the keys and this becomes a bottleneck. So the default is to use Object IDs.
That stated, your rate of creation is probably not that fast and you may be able to leverage the ideas in the linked document. Also, you may want to look at something like JIRA where the ID is generated in some rational way with regards to the project.