house9

random code and what not

Rails - ActiveRecord created_by and updated_by gem


Often your application will want to track who created and updated your data.
On a previous rails 2 project I used a plugin called userstamp which worked well, but until recently it did not support rails 3+

I didn’t find any other gems out there at the time, so I put together the clerk gem,
you can install it as a gem in your rails 3+ applications by adding gem ‘clerk’ to your Gemfile, see the README on github for details

NOTE: the current version relies on your database tables having columns named created_by_id and updated_by_id and the gem does not support any custom configuration.



postgres: random useful things

Run sql statements from the command line, use the -c flag


create a random value


crazy updates using regexp_matches


Git Workflow

This is the current git workflow we are using at work, your mileage may vary:




Resources

Get table and column information in postgres

Use the information_schema catalog - http://www.postgresql.org/docs/9.1/static/information-schema.html

Get table and columns from the public schema




This can be useful, maybe you want to auto generate some text that creates foreign keys based on a naming convention - for instance let’s say we have a ruby/rails method called make_fk_unless_exists




The ‘code’ column would render something like

make_fk_unless_exists :projects, :milestone_id, :milestones
make_fk_unless_exists :tasks, :project_id, :projects



Generating barcodes with ruby and rails

Recently I had to convert some functionality from an old MS Access system to a Ruby on Rails application. The Access system generated a report that included a barcode; they were using a barcode font installed on the machine running access. I figured generating the barcode server side in the web app was going to be much nicer than installing barcode fonts on the client browsers. And since this is Ruby, I figured…
There must be a gem for that 
Sure enough, the barby gem. It allows you to output your barcode as png, gif, svg, pdf, etc…

In my case I generated png barcodes and included image references to those in my view files.

Sample code




There is no pixel setting, it uses some other type of unit to determine size; using the xdim, margin and height options you can tweak the size, but I found that it was not very precise - for my use case it was good enough.

The generated barcode does not include the value, it is just the image which can be scanned using a barcode scanner, but it would be nice if there was an option to include the value below that. This is somewhat trivial to add in your view using html.

Overall I was really pleased with this gem!

Resources

Comments

House 9
As far as i know the gem does not support that feature. You need to add that text to your pdf, html, etc… Might want to check the google groups link above for additional help
Icaro
Perfect, thanks. How can i display the value at the bottom of the same image?
House 9
Try setting your path to "#{Rails.root}/public/barcodes"
Icaro
Im getting
No such file or directory - /public/barcodes/barcode_BARBY.png

Is there anyway to not to create the png and then display it from public resources? like streaming each time i call a coupon for instance?.

Thanks