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

GMail Tips

Usually I do not like to post a topic so short but I learnt a few tricks that a few of you may not know. I know most people I know use Google Mail so thought it’d be helpful.

Suggesting your email is foobar@gmail.com, you may put periods in there as you please. foo.bar@gmail.com and f.o.o.b.a.r@gmail.com both work (Please do not use the latter; i beg).

Secondly, plus signs can be added onto the end followed by anything you want. You could have foobar+work@gmail.com and such and that works as a catch-all.

Obviously it can be used in conjunction with each other.

0 Comments

GeekThreads: Introducing: Git Push tshirt

I have been planning doing tshirts for a while under the company ‘Geek Threads’. I finally took the plunge and have shirts to sell here at RailsConf.

They are black shirts, with white monospaced text on the front and back. The front reads ‘Git push: just the tip’ and the back reads ‘just a little bit. just to see how it feels’. It’s a case of first come, first serve (I will be carrying some with me at all times but may not have your size). You can also custom order if I have run out or such. They are selling at $20 each (sorry guys, have to make a profit.)

I’m accepting ideas for both text-based and graphic based tshirts. So if you have any ideas, message me. I pay 25% profit royalties.

To find me, either look for one of the Hashrocketeers, email me or @zachinglis on twitter.

Cheers.

0 Comments

RailsConf

As always I forget to say until the last minute.

I and the rest of the Hashrocket team will be going to RailsConf next week, so if you see me there, do stop me and say Hi. :)

0 Comments