Archive for the 'Ruby, Rails and Merb' Category

‘Missing the Rails x.x.x gem’? Even though you have that version.

Myself and many other people in the office have been getting this problem, particularly when using Applications like Textmate. I have finally found the answer to this thanks to a Ruby-Forum post.

Type type rails in your console to find out where Rails is stored. This will give you something like the following: rails is /usr/local/bin/rails.

Edit your .bash_profile file and add the path returned, like PATH=/usr/local/bin/rails:$PATH or whatever your equivalent may be. Make sure there is an export PATH line somewhere underneath that, and no more errors!

3 Comments

Extending your irb

As a Ruby developer, you probably spend a reasonable amount of time in script/console or irb. What you may not have known was that you can extend and give your irb more functionality and make your life easier. I’d like to introduce you to ~/.irbrc.

Let’s add tab method completion as well, that’ll make life more simple.

    require 'irb/completion'
  

One thing that had always annoyed me about irb is the lack of colour differentiating objects, strings, classes, etc. The Wirble gem solves this. I also installed utility_belt which has a bunch of extra features for your irb. (Don’t forget to install it via: sudo gem install wirble utility_belt)

    # load rubygems, wirble and utility_belt
    require 'rubygems'
    require 'wirble'
    require 'utility_belt'    

    # load wirble
    Wirble.init
    Wirble.colorize
  

utility_belt allows us to do a lot of great things, such as editing irb in Textmate, pastie-ing (OS X only), grep methods and classes, Google the last output (good for errors), only having to type x for quit

When debugging, I use ‘pp‘ (aka pretty_print) for examining my objects in Ruby. It’s annoying that I have to require it every time. Instead of having to constantly type in the require command, I have just added it to my irbrc file.

    require 'pp'
  

You can also add methods to the file for later use . I have extended the Object class, to simulate the following: Post.methods - Class.methods

    class Object
      def non_class_methods
        self.methods - Class.methods
      end
    end
  

I also added q as my exit command so i can exit by typing q and hitting return. utility_belt provides x for me, but I am more familiar with q.

alias q exit

You can see this file and all my other dot-files on Github.

7 Comments

Unobtrusive RESTful links in Rails

In a recent project, I wanted a link that modifies a row in the database. The provided mockup wanted a link. “To follow the rules of RESTful architecture, anything modifying the database, should not be a GET request” I thought to myself. I realised that Rails provides me with both a link_to and a button_to helper but no hybrid that would be unobtrusive, so I knew I had to make one.

The snippet adds a link after the form, hides the form and make the link submit the form. I have made it non-conflicting to those who use other libraries and jQuery.

To install:

  • Use jRails or have the jQuery library included (Prototype not needed)
  • Add the snippet to your application.js
  • Create your button_to’s with the class :class => "form_to_link"

Thanks to Kyle Neath and Rein Henrichs with this.

6 Comments

Quick Bash Tip: Less mouse when finding commands

When I run rake --tasks looking for a particular command, I hate the process of picking up the mouse to copy and paste. I am very weak with my bash-fu and so I decided to ask someone. Thanks to one of my friends and co-workers, Sandro Turriate, I do not have to pick up the mouse anymore.

I use grep to single out the specific tasks I want, such as rake --tasks | grep db would only find commands containing the letters ‘db’.

Run rake --tasks | grep faker (where faker is what you are looking for), until you find ONLY the task you want to run.

Then run
rake --tasks | grep faker | sh and it runs the output. And of course # gets ignored.

1 Comment

Introducing is_rateable

Wow. I seem to be on an open-source roll lately. A lot of that is in thanks to Hashrocket and my job there. We strongly believe giving back to the community and thus I try to anytime I can.

I need a rating system and wanted something different than acts_as_rateable and decided that I would try to come up with one more tailored to my needs.

Installing this plugin, you do not have to worry about creating a star system(The CSS and XHTML on a star system is troublesome), it comes with one.

Along with all my plugins, you can get it on my github at http://github.com/zachinglis/is_rateable/.

As always patches welcome!

0 Comments

Using faker to test your application

Recently I needed some data to test such features as pagination and search. I only had 10 fixtures in the database and this wasn’t enough to test on.

I could have created a bunch of records filling in the fields with random characters but that is a not a very eloquent solution. Instead I wrote a rake task saved as: lib/tasks/faker.rake.

Here is my code

  desc "Insert 100 projects using the faker gem"
  task :insert_projects => :environment do
    require 'faker'
    1..100.times do |i|
      b = Project.create( :name => Faker::Company.name,
                            :slogan => Faker::Company.bs)
    end
    puts "Faked project records"
  end

Originally I used .create! but I found occasionally there were conflicting records so a .save may pass false, now it won’t fail if it passes false and will carry on with the rest.

0 Comments

Crummy New Feature: Symbol Argument

I have just added a feature to Crummy where you can give an argument of a symbol which is then translated to get the instance variable and evaluated like so:


before_filter :load_comment
add_crumb :comment # => add_crumb(@comment.to_s, comment_path(@comment.to_param))

0 Comments

Introducing Crummy

I have worked on many applications that require breadcrumbs. Every time, one of the developers has built a quick custom solution. Finally, I decided that we needed a complete solution that we could easily add to future projects, and thus Crummy was born.

Requirements

I set a few requirements that must be fulfilled in order for me to call the plugin a success. They were:

  • Ability to add breadcrumbs at Controller class level (like a before filter)
  • Ability to add breadcrumbs in the controller methods (such as actions)
  • Ability to add breadcrumbs from the views
  • Ability to add breadcrumbs with or without an url
  • No variable conflicts (such as instance variables)
  • Full and strong spec coverage
  • Good documentation

I managed to fulfill these requirements and add a few shiny methods to make life easy.

Examples of Usage

    class ApplicationController < ActionController::Baseee
      add_crumb 'Home', '/' # This will display on EVERY list of breadcrumbs
    end
  


    class BusinessController < ApplicationController
      # The load order DOES matter.
      add_crumb("Businesses") { |instance| instance.send :businesses_path } # Show on all actions
      add_crumb("Comments", :only => [:comments, :show]) { |instance| instance.send :business_comments_path } # Only add the comments link on the comments and show actions.
      before_filter :load_comment, :only => "show"

      def show
       add_crumb @business.display_name, @business
      end

      def load_comment
       @comment = Comment.find(params[:id])
      end
    end
  

Golden Feature

One of the most useful argument usage is add_crumb :comment. What this does is turns the symbol into an instance variable (@comment.) Then it roughly translates into:

  # add_crumb :comment
  before_filter :add_comment_breadcrumb
  def add_comment_breadcrumb
    add_crumb @comment.to_s, @comment
  end

Installation

script/plugin install git://github.com/zachinglis/crummy.git

2 Comments

Introducing Holler

One thing that separates us at Hashrocket is the fact that we have a great team of Ruby experts. This is great if you get stuck, there is always at least one person who can help you on something. We also follow the great agile principle of pairing, which means that if someone is doing something you are interested in, then you can watch and learn with them.

Everyone is hesitant to get into Backpack for the purposes of updating their status and it is too private to talk about on Twitter so I thought I would come up with my own solution. Originally the Backpack feature came from their In/Out application, my good friend David Smalley came up with his own Merb solution too, but I wanted something Rails and thus Holler was born.

Holler Almost v1

It has only taken me 4 hours so far but I would like to introduce Holler. There are a few big features that I am planning on: Fluid integration similar to Shout, Javascript where its needed, tags, twitter integration (direct messaging to the application), more ways to view statuses.

Please feel free to fork it and make your own changes! Get Holler.

8 Comments

Rails Tip: to_s

I haven’t posted much recently as I’ve been very busy at work (more on that later) so I thought I would post another Rails tip.


## Model
class Person < ActiveRecord::Base
def to_s
self.title
end
end

# View
link_to person

# Output
<a href=\"http://foo.bar/person/1\">Zach Inglis</a>

View Pastie

Furthermore you could extend it like this:

## Model
class Person < ActiveRecord::Base
def to_s
self.title
end

def to_param
self.slug
end
end

# View
link_to person

# Output
<a href=\"http://foo.bar/person/zach-inglis\">Zach Inglis</a>

View pastie

1 Comment