Mustache is Real

So I finally got around to getting legit and filed my papers. I'll now be operating under the name Mustache, Inc. But mainly just Mustache. WTF? Mustache? Why?

Why would I name my company Mustache? Well, why not. I came up with the logo and liked it, and wanted something a bit abstract. Plus it is going to work well as a first line of defense for boring stuck up clients. If you can't work with a company named Mustache, then Mustache can't work with you.

If you've met me, you'd know I'm a pretty laid back guy. I don't kiss ass and I don't bullshit. I let my work and commitment to projects speak for itself. I've tried to convey this with initial consults with clients. I tend to give them "100% Maddox" during these meetings and lay it all on the table. So far it has worked out great. It's amazing what happens when you operate a business as an honest and transparent person. It actually works! Imagine that!

So let it be known, I'm officially available for hire. Check out the site and let me know what you think.

http://www.mustacheinc.com

Popularity: 15% [?]

    Categories: Rails, Ruby     3 Comments »

Spec Out Your Cookies with Rspec

Here's something that got me this weekend and I couldn't find much info on it anywhere. So like my Internet taught me, I'm sharing.

links_controller.rb
cookies[:phone] = params[:phone]
links_controller_spec.rb
it "should save the phone number in a cookie" do
  @my_cookies = mock('cookies')
  @my_cookies.stub!(:[]=)
  controller.stub!(:cookies).and_return(@my_cookies)
 
  @my_cookies.should_receive(:[]=).with(:phone, '804-755-7555')
 
  do_post
end

Here I'm making the expectation that a cookie will be set with the key of 'phone' and a value of a passed in parameter. My do_post method takes care of the post and passes in the parameter :phone => '804-755-7555'.

Popularity: 14% [?]

    Categories: Rails, Snippets, Testing     No Comments »

Open Mic Night At CVREG Tonight

Tonight the Central Virginia Ruby Enthusiast Group will be hosting an Open Mic night. Anyone attending will be able to jump up and give a 5-10 minute lightning talk about a topic they're excited about.

If you haven't been to a CVREG meeting, tonight would be a great time to visit and introduce yourself. The meeting starts at 6:00, but you can show up at 5:30 if you want to hang out and meet everyone.

All the relevant information can be found at the event's Upcoming page

Hope to see you there!

Popularity: 16% [?]

    Categories: Rails, Ruby     No Comments »
    Tags:

Display Validation Errors For Your Ajaxified Form

One of the sweet things Rails provides is practically free error notification on forms. With just a little bit of

<%= error_messages_for :post %>

validations on your model, and some logic in your controller, bam, instant error messaging. Very simple.

But what if your form is using AJAX to post? The page doesn't re-render, so how is our buddy, error_messages_for, going to work? Well its not, we're gonna have to use AJAX to display the errors. It turns out this isn't too touch, but as usual we want to keep it DRY.

The Controller

class PostsController < ApplicationController
  def create
    @post = Post.new(params[:post])
    @post.save!
 
    # automatically renders create.rjs.erb
 
  rescue ActiveRecord::RecordInvalid
 
    @model = @kit_item
    render :template => 'shared/validation_error.rjs.erb'
 
  end
end

We will attempt to create the Post. If all goes well, then the create rjs action will fire and all is well. But if the record fails validation, we catch it, assign the current model to @model, and render shared/validaiton_error.rjs.erb. This is our generic AJAX action for rendering errors for Models.

The AJAX

Create shared/validation_error.rjs and use this little snippit, and thats pretty much it for this part.

page.replace_html "errors_for_#{@model.class.name.underscore}", "Oops! <ul>" + @model.errors.collect{|k,v| "<li>The #{k} #{v}</li>"}.to_s + "</ul>"
page.visual_effect :highlight, "errors_for_#{@model.class.name.underscore}", :startcolor => "'#ff0000'", :endcolor => "'#aca2b6'"

So, the errors just get collected and printed out to a div in the page. Let's go add this fancy div.

The View

This part is pretty simple. You just need to open up your form partial, posts/_form.html.erb and add this little view helper to the form:

<%= error_div_for post %>

This does nothing but prints out a div with a specific ID to prepare for our RJS action, if it's needed. So let's go make the view helper.

module ApplicationHelper
 
  def error_div_for(model)
      %{<div id="errors_for_#{model.class.name.underscore}"></div>}
  end
 
end

Now we can just call this simple view helper and pass in any model and it will dynamically build a div with the appropriate ID inside.

And thats pretty much it. To go through it again, when the form is loaded, the error div will be placed in the page, waiting to be used. Once the form is submitted in the background, the controller's create action will attempt to save it. If the record is invalid with validation errors, then the universal validation_errror RJS template will be fired off and replace all the errors from the failed validation, into the div we placed in the page. Oh and of course a little red flash action to make sure the user sees the errors.

Popularity: 18% [?]

    Categories: Rails     7 Comments »

Start and Stop MySql in Mac Os X 10.5 Leopard

Yay! Its finally here! The newest version of my favorite operating system. And it comes with 98% of my dev stack ALREADY installed! Yay! This is perfect.

Wait.. ok, no MySql. Thats ok, I'll just install it...Wait.. why isn't it running...why isn't this stupid preference panel turning it on!!!! ARGHHH!

Ok, that may have just happened to you, and you dramatically hit the Google looking for answers. So answers I give you.

If you want to start MySql in Leopard, you need to call the startup item directly from the command line:

sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

And to stop it:

sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop

Not so bad. But long... So make an alias in your .bash_profile

alias start_mysql="/Library/StartupItems/MySQLCOM/MySQLCOM start"
alias stop_mysql="/Library/StartupItems/MySQLCOM/MySQLCOM stop"

Happier? Relaxed? Good.

Popularity: 57% [?]

    Categories: Rails     36 Comments »
    Tags: