Browsing all articles in Ruby

Configuration for mailjet email delivery with Ruby on Rails

Posted Posted by Wes in Blog, Ruby, Ruby on Rails     Comments No comments
Jun
7

Setting up mailjet.com to deliver your mail via Ruby on Rails? Here’s how, because their Getting Started is nothing but placeholder headers right now.

Mailjet : Real-time Emailing - mailjet.com

read more

Ruby’s ParseExcel and the Extra Date

Posted Posted by Samuel Mullen in Blog, Programming, Ruby     Comments 1 comment
Apr
22

We have a feature in one of our application which allows the user to upload data from an Excel spreadsheet. One of the columns read in is a date field. Because we’re dealing with end users, sometimes those dates come in as “Date” objects, sometimes they come in as “String”s, and sometimes they come in as a number.

When Excel saves a date as a numeric, it is that value’s number of days since January 0, 1900 (No, we can’t say Dec 31, 1899). So a spreadsheet would store “May 1, 2011″ as 40664 if it was formatted as a number.

That’s great, peachy even, but if you’ll notice, 40664 days from Dec 31, 1899 is May 2, 2011, not May 1st. What happened? Compatibility.

When Microsoft introduced Excel way back when, Lotus123 dominated the spreadsheet marking. In order for Microsoft to be able to compete, it had to be compatible with Lotus123. It had to be compatible with all the formulas, all the features, and all the bugs. One of those bugs had to do with an erroneous leap day in 1900. This means that when you read in numbers from a spreadsheet in order to translate them to a date, you have to account for that extra leap day.

The solution? Just subtract one from the numeric. It might look something like this (from the Rails console):

Date.civil(1899, 12, 31) + 40664.days - 1.day

We could have did that anyway and just chalked it up to something weird, but now, at least, we know why it’s weird.

Notes:

Thebes, profiling spork, backup rubygem

Posted Posted by Wes in Ruby, Ruby on Rails     Comments No comments
Mar
14

Thebes, a new minimal sphinx gem for Rails

Link

Thebes is a wrapper around Sphinx, the search engine we use on most of our projects. Thebes differs from other solutions by staying as far away from your Rails code as possible. Instead of hiding the Sphinx configuration file behind a domain-specific language, this library assumes you will write Sphinx config files by hand. In Thebes, you edit an ERB template of your Sphinx configuration and populate it with variables at generation time. For developers needing the most flexible or fastest solution possible, this is a great way to work with Sphinx.

We’ve been doing some interesting things with search and reporting that are going to require faster lookups than directly querying the database. From the article “the [Thinking Sphinx project] has a lot of complexity and ties to ActiveRecord 2.x code. Consequently, the porting of TS to Rails 3 isn’t turning out to be the smooth road we hoped for. So, for Rails 3 projects, this looks like a good way to go if you’re willing to get your hands dirty and build some sphinx files yourself.

Profiling Spork for faster start-up time

Link

Spork allows you to preload Rails environment files into a process, then it forks that process and runs your tests against the new process. In essence, your tests will start faster because they’re not loading everything. You can specify files you want to be reloaded each time (for instance, model files).

The code:

This prints out everything being loaded up, so you can move files that don’t change into the preload block for that extra bit of snappiness.

Backup, a rubygem for database and file backups

Link

Backup is a RubyGem (for UNIX-like operating systems: Linux, Mac OSX) that allows you to configure and perform backups in a simple manner using an elegant Ruby DSL. It supports various databases (MySQL, PostgreSQL, MongoDB and Redis), it supports various storage locations (Amazon S3, Rackspace Cloud Files, Dropbox, any remote server through FTP, SFTP, SCP and RSync), it can archive files and folders, it can cycle backups, it can do incremental backups, it can compress backups, it can encrypt backups (OpenSSL or GPG), it can notify you about successful and/or failed backups (Mail or Twitter). It is very extensible and easy to add new functionality to. It’s easy to use.

Check out the README for all the details, but this allows you to backup your app via command line, pushing to S3 or rsync’ing to another server. You can schedule it with the fantastic Whenever gem too.

Getting the Runaround

Posted Posted by Samuel Mullen in Blog, Factories, Ruby, Ruby on Rails     Comments No comments
Oct
21

Yesterday was my third day on the job and to be honest, it wasn’t a lot of fun. I’m learning a lot of things right now: a new project, new methodologies, and new technologies. Combine all of that with a seemingly useless RSpec error and you can imagine where my blood pressure was reaching.

I had just added a new association to a FactoryGirl factory. I’ve not used FactoryGirl before, but it’s easy enough to copy and paste from other factories in the directory. So far, so good. I wasn’t watching the test output, but it was just one line, what could go wrong?

Several updates and commits later, I thought I’d better make sure we were still “Green”. Eh, not so much. Here’s the output I was seeing repeated over and over again.

Failure/Error: Unable to find matching line from backtrace
stack level too deep
# /home/user/.rvm/gems/ruby-1.9.2-p0@xxx/gems/activerecord-3.0.0/lib/active_record/locking/optimistic.rb:62

I’ll not bore you with the details of my fruitless search to track this down. Suffice it to say, Wes – that would be my new boss – helped me back out my changes and track down the error.

It turns out that my “simple” addition to the “facility” factory wasn’t so simple. It actually resulted in a never ending loop of weeping and gnashing of teeth.

There are three models which were being dealt with: Facility, User, and Department. Departments have many users; facilities have many departments, and a facility can have a user who is defined as a contact. The schema looks like this:

rounaround schema

In my “facility” factory, I was making an association to the “users” table. Unbeknownst to me, the “user” factory was making an association to “department”, which was in turn making an association to “facility”, which was making an association to “user”, ad nauseum. The result: a “stack level too deep” error.

The solution was to just create the user association with the “department” set to nil.

Before:

t.association :contact, :factory => :user

After:

t.association :contact, :factory => :user, :department => nil

Hopefully this will make someone else’s first week on the job go a little smoother and keep them from getting the runaround.

Converting erb to haml in rails3

Posted Posted by Wes in Ruby, Ruby on Rails     Comments No comments
Sep
19

I’m taking over a project that’s just started and prefer to use haml. In setting up my environment to work with rails3 and haml, here’s the best way I found to convert the fledgling app to haml.

Install gems

Run this

This will convert all .erb files in app/views into .haml files right next to them, leaving the originals in case you need to refer to them to fix somethig the converter missed.

This isn’t rails 3 specific, except for the Gemfile part, really.

rspec2, rails3, and spork with drb to get faster tests

Posted Posted by Wes in Ruby, Ruby on Rails     Comments 1 comment
Sep
17

I started using rspec2 and rails3 on a project and wanted faster tests.

Gemfile:
gem 'spork'

bundle install

spork --bootstrap

Bootstrapping will edit your spec_helper.rb file with some instructions. I had a vanilla spec_helper file, so everything got moved into the Spork.prefork block.

Also, I’m using autotest, so I added --drb to my .rspec file.

Finally:

spork &
autotest

I only had 15 tests at the time that I did this, but they went from running in 6 seconds to running in 1.5 seconds. Significant.

UPDATE:

From the comments, here’s a fix for running rake on Windows (“can’t find executable rake”):

Using Factory Girl with Rails 3

Posted Posted by Wes in Programming, Ruby, Ruby on Rails     Comments 1 comment
Sep
7

Setting up a new project, I couldn’t get FactoryGirl working. From the README:

If you want to use factory_girl with Rails 3, see http://github.com/thoughtbot/factory_girl_rails

I added this to my Gemfile:

gem 'factory_girl_rails'

A bundle install to install and done.

Note: I’m using rspec2 and putting my factories in spec/factories/*_factory.rb and I’ve seen references to spec/support/factories/*_factory.rb as well. I’m not sure if either is the blessed location.

Kansas City Ruby Users: Luke Pillow on Jeweler

Posted Posted by Wes in Kansas City Ruby User Group, Ruby, Ruby on Rails, Videos     Comments No comments
Mar
13

Luke Pillow present to the Kansas City Ruby Users Group on Jeweler and Gemcutter rubygems.org.

Also, Luke is helping to organize the Ruby Midwest conference, so you should check that out.

Kansas City Ruby Users Group | March 2010 | Luke Pillow | Jeweler for Rubygems | kcrug.org from Wes Garrison on Vimeo.

Kansas City Ruby Users: Ryan Smith on Heroku

Posted Posted by Wes in Kansas City Ruby User Group, Ruby, Ruby on Rails, Videos     Comments 2 comments
Mar
12

Ryan Smith presents to the Kansas City Ruby Users Group on Heroku: why it’s wonderful for deploying Ruby on Rails applications, how to set up a new application and deploy it to Heroku in minutes, and how to use Heroku add-ons to support search.

Kansas City Ruby Users Group | March 2010 | Ryan Smith | Heroku | kcrug.org from Wes Garrison on Vimeo.

Installing sqlite headers on ubuntu (sqlite3.h not found)

Posted Posted by Wes in Ruby, Ruby on Rails     Comments 7 comments
Mar
5

I was setting up Integrity for the first time and ran into this on my server when bundling gems:

~$ bundle install
...
Installing do_sqlite3 (0.10.0) from rubygems repository at
  http://gemcutter.org/ with native extensions
  /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:482:in
  `build_extensions':
ERROR: Failed to build gem native extension.
  (Gem::Installer::ExtensionBuildError)

/usr/bin/ruby1.8 extconf.rb
checking for sqlite3.h... no
*** extconf.rb failed ***
...

The key is the line: checking for sqlite3.h... no
The do_sqlite3 gem gets compiled natively, but the development headers weren’t installed on the system and so the compilation won’t work.

Install them:

~$ sudo apt-get install sqlite3
~$ sudo apt-get install libsqlite3-dev

Then, re-bundle:

~$ bundle install

blog Categories

about databasically

We live and work in Kansas City, USA.

We're passionate about helping small businesses succeed and want to help you use technology to get more done.

From server, desktop, network management to programming custom web applications in Ruby on Rails, we're here to lend a hand.

Contact us if you have any questions!