Hash default value

Comments

The other day while working on a problem on exercism.io I was wanting to keep a hash of arrays, and I wanted each key in the hash to default to a empty array. So I started with

1
hash = Hash.new([])

I then procedded to be able to append to the array for each key, so I tried:

1
hash[key] << "value"

I was expecting this to append to the empty array and set the key to:

1
["value"]
Read on →

Serving fonts from s3/cloudfront for firefox

Comments

I recently started using s3/cloudfront to host assets for a few apps that I am working on. Initially I thought everything just worked. I moved all of my assets to S3 and updated all of their urls to point to the s3/cloudfront location. All seemed well until I looked at Firefox. My @font-face fonts were not rendering in firefox, which was odd to me. After a long search it seems that you need to use CORS to enable it. To do this log into you AWS console and look at your buckets. For the bucket serving the fonts you want to click on permissions and then edit CORS configuration. In here you want to add something like

Read on →

Serving Rails assets from the cloud

Comments

Recently I have had to build several high performance websites. These sites were simple sites but were heavy in images and we were expecting large amounts of traffic. I was using heroku to host the sites so I could easily scale to a large number of dynos but why have all of the excess http request for all of the assets hit my heroku app too.

So I knew I was going to offload all of my images, css, fonts, and javascript onto s3 and maybe even run cloudfront to speed things up further, but managing:

  1. copying all of those assets to s3
  2. being sure that every time I change an asset I upload it to S3
  3. managing the urls to the assets

was going to be a pain in the ass.

Luckily with the help of Rails 3.2’s asset pipeline and a little gem called ‘asset_sync’ everything can be made much simpler.

Read on →

Pushing testing branches to Heroku

I really love using heroku for my rails applications. It provides me easy deployments, easy access to a host of add-ons, but mostly the fact that I can use the free plans to test/develop. I often find that for small projects I will start developing and push to heroku after I have something significant to deploy. I will then continue to test/develop locally and only push new big features as they are completed.

The problem with this is when you start developing multiple seperate feature branches and a client needs to remotely view the individual branches to review changes at different times. So how do you solve this? The problem is that heroku only deploys the master branch of any git repository.

Read on →

Stubbing Paperclip Attachments in Rspec

If you are using RSpec, paperclip, and your models are validating the existence of attachments, you are going to want to stub out the saving of the attachments. You will want to do this to speed up your test, keep from having extraneous files that you need to clean up (those saved by running your test).

Adding the code below to my RSpec suppot folder, sped up my testing suite from 1~2 minutes to about 10-15sec.

Read on →

Taggable Mongoid Models

I was looking for a simple way to add tagging to my Mongoid 3+ models and most of the options I found were for Mongoid 2 only. Luckily I stumbled upon Mongoid Simple Tags from Mauro Torres(chebyte) a ruby gem that supports Mongoid 3+.

Its a nice simple way to add tagging. Simply include “Mongoid::Document::Taggable” to your model.

1
2
3
4
class User
  include Mongoid::Document
  include Mongoid::Document::Taggable
end

Once added you can assign tags through the #tag_list= method. This assumes a string of comma seperated tags, which are then stored in the tags field as an array.

1
u = User.create(:name => "Tuquito", tag_list: "linux, tucuman, free software")
Read on →

ZSH and Rake Parameters

Comments

If you are using ZSH and trying to pass a parameter into a task:

1
rake task['param']

or

1
rake task[param]

you may be getting an error that says:

1
zsh: no matches found: task[param]

To solve this issue I had to add the following alias to my .zshrc file:

Read on →