PullMonkey Blog http://pullmonkey.com Thu, 16 Aug 2012 23:40:41 +0000 en-US hourly 1 https://wordpress.org/?v=5.6 Dynamic Select Boxes – Ruby on Rails 3 http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/ http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/#comments Sat, 11 Aug 2012 01:11:51 +0000 http://pullmonkey.com/?p=57726 Over 4 years ago, I wrote a tutorial for doing dynamic (cascading) select boxes.
Still getting comments and emails to this day. Mostly asking how to get this working with rails 3, which has moved from prototype to jquery.
So here's a tutorial for getting 3 select boxes to trigger updates for each other.

First set things up:

I just used the html5 haml twitter bootstrap, etc template. Really useful.

If you need data, here's what I used - put this in your db/seeds.rb file:

Next, setup your model associations:

Genres have many artists.
Artists have many songs.
Genres have many songs through artists.

I'm just using a home controller to setup variables for the index page as well as setup variables for use in the dynamic updating:

Now the view just has the 3 select boxes and the unobtrusive javascript (triggered onchange) to make the ajax calls for updating:

We need our rjs files for updating the select boxes, one for the songs (when artist changes) and one for the artists and songs (when genre changes):

Our routes are simple:

That's it.

UPDATE: Here's an erb alternative for index.html.

And the js.haml can be converted to js.erb by taking #{...} and converting to <%= ... %> :

]]>
http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/feed/ 12
VIN API – Do you know about the complete data set? http://pullmonkey.com/2012/08/04/vin-api-do-you-know-about-the-complete-data-set/ Sat, 04 Aug 2012 15:42:04 +0000 http://pullmonkey.com/?p=57715 Looking at our data trends for VIN API, we show about 15% of all requests are for the complete data set. It's the same price as the basic data set and rich with a ton of data.

Check out the data set from this post over 1 and half years ago. On top of the basic data set's year, make and model, the complete data set comes with data like MSRP, MPG (city and highway), dealer invoice, gas tank size, etc.

We get tons of hits for basic data, but at no additional charge we also provide the complete data set. Just add a complete
=> "true" to your request. So simple and there's no performance hit.

For more information, check out or examples and faq.

Enjoy!

Here's another look at the data elements from a comprehensive query:

]]>
Using Ruby to read from a MagTek USB Card Reader http://pullmonkey.com/2012/07/27/using-ruby-to-read-from-a-magtek-usb-card-reader/ Fri, 27 Jul 2012 15:39:59 +0000 http://pullmonkey.com/?p=57712 Been playing around with a lot of USB devices lately, to get them to play nicely with some of our touch screen Rails apps.
One of the devices is a card reader - specifically a MagTek reader, and the same code works for the ELO reader.

I've tried a billion different ways to ensure we're reading in all the data. At first, I was just asking the usb stream to present me with a certain number of characters, b/c that's how all the examples do it.
But we're using a lot of different cards with the readers, so that didn't work too well. So now I just read one character at a time until we find the new line.
Here's the code to find the device, open it, close it and read from it, all whilst finding and converting the data as required.

So using this code, we take the swiped data and send it to our server for storage.

]]>
Signature Pad in Linux http://pullmonkey.com/2012/07/26/signature-pad-in-linux/ Thu, 26 Jul 2012 16:57:56 +0000 http://pullmonkey.com/?p=57708 I'm using the topaz model T L462 HSB. It's nice now that it works. But what a PITA to get it going. The biggest problem with the device is the documentation. There is no low level documentation explaining the representation of the bytes coming in over usb. So you are stuck using their libraries which expose some poorly explained API.

Anyway, like I said, great when it works. So I'm working in linux which basically leaves me having to use java libs and a shared object file.

Here's my code:

It sits and waits until the sig pad gets some points on it. Once we have points it waits for a lapse in time where there is no change in the number of points.
Once we've decided that we have captured a signature, we store it to a file.
Then we shell out to a ruby script - see the httpi and curb article - which is how I get the file to our rails server for display and verification from the user.

Now, you'll need a few files. Most of which can be found in the java zip download from the vendor site.
From that zip, you should grab the following:
SigPlus2_60.jar
RXTXcomm.jar
libSigUsb.so - if you need the 64 bit version, go here - http://www.topazsystems.com/Software/libSigUsb7242012.zip

Then I just used eclipse to set everything up and build. Works well so far.
The only thing you may need to change is the model (I have mine set to SignatureGem1X5) and com port (I have mine set to HID1).

]]>
Using HTTPI with Curb to do multipart file uploads with spnego http://pullmonkey.com/2012/07/25/using-httpi-with-curb-to-do-multipart-file-uploads-with-spnego/ http://pullmonkey.com/2012/07/25/using-httpi-with-curb-to-do-multipart-file-uploads-with-spnego/#comments Wed, 25 Jul 2012 22:25:11 +0000 http://pullmonkey.com/?p=57705 It took a while to find a library that handled spnego requests, as opposed to shelling out to a curl --negotiate. Found httpi to work well with the curb adapter.
The problem was that the documentation didn't really explain how to do file uploads withing the httpi request block.
Wasn't too much of a big deal, but I thought it may save someone else some time.

]]>
http://pullmonkey.com/2012/07/25/using-httpi-with-curb-to-do-multipart-file-uploads-with-spnego/feed/ 2
Rubygem version incompatability with older Rails versions http://pullmonkey.com/2012/05/30/rubygem-version-incompatability-with-older-rails-versions/ Wed, 30 May 2012 15:35:39 +0000 http://pullmonkey.com/?p=57701 We maintain a rails 2.3.8 application and haven't had to touch it in quite some time. But when we did, we came across this error:

uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)

Then after applying the solution found here, we started getting this error:

undefined method `name' for "actionmailer":String (NoMethodError)

Both errors were resolved by using RVM to specify the rubygem version best suited for this application.

rvm rubygems 1.3.7

So our .rvmrc file now looks like this:

rvm use @
rvm rubygems 1.3.7
]]>
Headers and Footers in ruby’s Spreadsheet gem http://pullmonkey.com/2012/04/26/headers-and-footers-in-rubys-spreadsheet-gem/ http://pullmonkey.com/2012/04/26/headers-and-footers-in-rubys-spreadsheet-gem/#comments Thu, 26 Apr 2012 19:17:20 +0000 http://pullmonkey.com/?p=57696 Have you ever needed to add a header or footer to your spreadsheets in ruby?
Yah, well, we have 🙁
Yes, you can do this with the Write Excel gem.
But we've already written years worth of spreadsheet code with the spreadsheet gem and don't want to rewrite it all.

Anyway, we thought we'd share our little trick to get page headers using the spreadsheet gem.

So what you see here (above) goes into a config/initializer, something like RAILS_ROOT/config/initializer/enable_headers_in_spreadsheet_gem.rb. Here's what's going on:

1) We know that the write_from_scratch method is called when everything is said and done and the data is ready to be written to the spreadsheet file. So we make use of this and alias that method to write_from_scratch_without_header. Which opens us up to call our write_header method inside our write_from_scratch method which, of course, will call the original write_from_scratch method.

2) Our write_header method makes use of the already existing opcode for Header in the spreadsheet gem. It's not being used, so my guess is the developers intend on solving this issue at some point. We have to send the opcode, the length info and the string we want to write out. This was the trickiest part to figure out.

3) We expose an add_header method that simply takes a string and stores it in the header accessor. This means, to set the header, you simply say sheet.add_header("foo header").

To implement the footer, you'd just do the same thing, create a footer accessor, add a method to update it. Then build the writer_footer method with opcode(:footer) and append write_footer at the end of write_from_scratch.

Well, that took us some time to figure out, so enjoy and let me know if you have any questions.

]]>
http://pullmonkey.com/2012/04/26/headers-and-footers-in-rubys-spreadsheet-gem/feed/ 1
Ruby’s stacked conditionals – who knew? http://pullmonkey.com/2012/03/10/rubys-stacked-conditionals-who-knew/ Sat, 10 Mar 2012 23:40:59 +0000 http://pullmonkey.com/?p=57687 puts "hello readers" if you_are_a_reader if you_can_read unless you_already_read_it

I was playing around with some project euler code I'm working on (anyone else figure out the Pencils of Rays with code that executes in less than a minute??). Anyway - I was going through my code and adding "if debug" to the end of all my puts statements b/c I didn't want to see them for the next run through. So by mistake, I blindly added "if debug" at the end of 'puts "odd" if odd', giving me 'puts "odd" if odd if debug'. Neat. Then, going through trying to find the bottlenecks, I noticed it. So jumped into irb to make sure it was legit and it was.

Interesting. Not sure where I'd ever use it, though.

]]>
Formgen 0.3.0 and some generated examples http://pullmonkey.com/2011/11/17/formgen-030-and-some-generated-examples/ Thu, 17 Nov 2011 00:01:03 +0000 http://pullmonkey.com/?p=57676 Formgen generates views in erb and haml based on these keywords: has_many, belongs_to, accepts_nested_attributes_for, and has_attached_file. It follows best practice and standard patterns so you don't have to. Here is example of its usage:

Doing this

rails generate skizmo:form Foo

Generates this

app/helpers/foo_setup_helper.rb

app/views/foos/new.html.erb

app/views/foos/edit.html.erb

app/views/foos/_form.html.erb

app/views/foos/_bar_fields.html.erb

app/views/foos/_baz_fields.html.erb


That's it:

Ok, so I say that, but there are a few things to note. First, this probably only works with Rails 3, given some of the calls I make for figuring things out. Next, you'll want to yield :head, somewhere in your layout so that the content_for :head that loads the add and remove jQuery will be useful. Finally, you should note that the nested attributes are only processed from the first level, but that will soon change. Anyway, enjoy it and if you have any problems, just let me know.

]]>
Form Gen – ruby on rails gem http://pullmonkey.com/2011/11/16/form-gen-ruby-on-rails-gem/ Wed, 16 Nov 2011 04:38:31 +0000 http://pullmonkey.com/?p=57670 I've always dreaded doing the view code for nested attributes, setting up the helpers, the javascript, partials etc. Just so tedious, so I thought, I'd try my hand at a form generator. This generator takes a class and then processes all the nested attributes of that class's has_many's and belongs_to's to generate the typical pattern of form partial and field partials you find here.

Yah I did ruby syntax-highlighting ... so not the best for something like that, but eh ... it'll do.

The formgen gem is a 0.1.0 starting point, but has worked for all the things I've needed. If you're feeling brave give it a shot:

# In your Gemfile -
gem 'formgen'
# then from your rails projects -
rails g skizmo:form SomeClass

That outta do it.

]]>