noizZze

Asset URLs and Paths in Rake Tasks

Sometimes you need to access an asset from a Rake task. One example is one of our projects where we need to compile a package for a Phonegap app that includes images, Javascript and CSS. Before Rails 3 and the Asset Pipeline we knew the location and the names of our assets exactly, but now they may be located anywhere and have the hashes in the names. Instead of recreating all the hashing and locating functionality, the most logical path is to reuse what’s there in Rails.

One way I figured is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace :app do

  task :my_task => :environment do
    AppyTravels::Application.configure do
      config.assets.debug = false
      config.assets.digest = true
    end

    include AssetsMaster

    image_path('my_image.png')
  end

  # Assets master gives the access to helpers like #asset_path from rake tasks
  module AssetsMaster
    include Sprockets::Helpers::RailsHelper

    def config; Rails.application.config; end
    def controller; nil; end
    def relative_url_root; nil; end
  end

end

We have an AssetsMaster module that includes the Sprockets::Helpers::RailsHelper. This helper contains all path / url methods and some other asset-related goodness. Including this file would be sufficient in Rails 3.0, but it’s not in Rails 3.1+. In the latest version we need to have three more methods: config, controller and relative_url_root. The first one should refer to the app configuration, and the other two can be nil.

You may have noticed how I adjust the assets configuration in my Rake task (lines 4-6). As you can see, I disable debugging and enable digests explicitly to break the ties from the environment config.

After all this magic, you can finally enjoy your desired image_path helper that will obey Asset Pipeline configuration to the letter.

Rails 3 Asset Pipeline on Heroku When Using Mongoid

Mongoid is an excellent piece of work and makes MongoDB a first-class citizen in the Rails world, but in its present state it has one nasty trait – it attempts the connection to DB even when you do something as innocent as precompiling your assets. Here’s what you’ll see if you try to run bundle exec rake assets:precompile with your MongoDB being shut down:

Failed to connect to a master node at localhost:27017

Kaminari Tips and Tricks

Moving a Rails 3.0 project to Rails 3.1 can be surprisingly tricky and especially if you use Mongoid, embedded collections, pagination and AJAX updates.

Mongoid 2.0.2 dropped own pagination and now fully relying on third-party tools. That’s a great idea and modularisation effort. However, now there’s a dilemma about what to use. I chose Kaminari for its up-to-date state and overall simplicity. I’d like to show a tricky part of making it all work.

Fixing MySQL Database After Moving to Ruby 1.9

This is a short post describing how I managed to revive the database full of content in national character set after moving to Ruby 1.9.2.

Ruby1.9 is known for many things – reduced memory footprint, increased performance, friendlier syntax – but it also introduced a long-awaited concept of encodings. The basis of this is encoding-aware strings.

Filling the Gap

Today during a long walk with son I had time to think what is it that’s missing that I can’t make a profitable product (or to put it precisely, to make a product profitable). I have plenty of ideas for tools and services, but no matter how I try to make the product nice and usable, it’s usually not enough to attract users. What I think is missing is even basic “marketing” and “customer development” education.

I’m a hardcore technology geek who can create things, but have no idea how to promote, market and sell them. That’s sad. Whole last year I dedicated to working on user experience of products and services to make them usable, not just functional. Now it seems to be a good idea to switch focus to my newly discovered underdeveloped skill.

One resource that looks specifically helpful is Mixergy. Before subscribing I watched several free interviews and the amount of useful data was just overwhelming. If you happen to know any other master-classes and courses on the subject (promotion, marketing, ads, interviewing, surveys, customer development, networking etc) drop me a line (in comments or personal). I will appreciate it greatly.

Ok, back to viewing and note taking!

Going Static

You may have noticed a change in the graphics design and slight functionality changes. That’s because today I moved on from Wordpress to a fully static website. The website now is generated from the collection of layouts and articles every time I change something and then deployed to the server as a bunch of HTML, CSS and JavaScript files along with some assets.

Monit, Jenkins and Ruby on Rails

My latest challenge was setting up a continuous integration environment on one of the staging servers. If you don’t know what CI is, allocate a couple of hours to study the concept. If you are in software development, my bet is you need the thing, and it will probably change the way you look at your project release cycle forever.

I chose Jenkins for its ease of installation and extensive plugin net. You’ll need Java 1.5+ for it, but that’s rarely is a problem. I just grabbed a copy from java.com and got it all up and running in less than 5 minutes on our CentOS 5.4 box. Previously we tried CruiseControl.rb and it worked well for some time, but as I started linking it to our Rails 3 projects, I figured that it’s easier to try something else than make the tool work. I’m sure it’s a great tool and all, just didn’t work for me.

Monit Configuration for Rails Stack

It’s more like a self-note, but also an answer to a question what I like to monitor on the servers where we run Rails apps and how. Thought that it could save some time to anyone. Let me know if you have any questions. I like to create the directory for Monit configuration bits and keep different services separate. It easier to update and reuse this way.

HTML5 Mobile Pro Desktop Edition? Really?

Disclaimer: Here comes an angry and seriously disappointed customer. If you are fan or a friend of authors stop reading here. I’m expressing some strong opinions. You’ve been warned.

Another disclaimer: There is a line you don’t cross… even if it’s beta… even if you cut those $30 off. There’s no preview (for a reason), and I had to trust your word. Which was a mistake.

A while back I was lured into purchase of a video workshop named “HTML5 Mobile Pro Desktop Edition” by Amy Hoy and Thomas Fuchs. The thought that people behind Letsfreckle and zepto.js released a 3+ hour workshop for a “bargain” price of US$99 (only for a limited period while in beta) didn’t let me sleep at night. And so I figured I’d better grab a copy before it jumps to insane US$129 in August. (Note that I’m writing it in the dead of September and it’s still in beta.)

MongoDB: Queries Against Arrays and Plain Fields

Did you know that with MongoDB you can easily run queries against fields that vary in type, like this?

> db.m.insert({ body: '1', phone_id: 1 })
> db.m.insert({ body: '2', phone_id: [ 1, 2 ]})

> db.m.find({ phone_id: 1 })
{ "_id" : ObjectId("4e56089fbdfcdc0f68c894ae"), "body" : "1", "phone_id" : 1 }
{ "_id" : ObjectId("4e5608afbdfcdc0f68c894af"), "body" : "2", "phone_id" : [ 1, 2 ] }

> db.m.find({ phone_id: 2 })
{ "_id" : ObjectId("4e5608afbdfcdc0f68c894af"), "body" : "2", "phone_id" : [ 1, 2 ] }

What’s curious is that when I query for “phone_id: 1”, it looks into the array of IDs as well as into the integer field. Quite a flexibility!

Amazingly, indexing is supported as well.