Cory O'Daniel – These are just words Software development, thoughts, and randomness

17Nov/112

Rails 3 Easy Search forms using SimpleForm and ActiveModel

For the sites I'm currently working on I have search pages all over the place for different resources, especially on the backend. In the past I would forego using form builders and just do some HTML to make a form GET the data. I hate HTML.

So I did some poking around to see what I could do to DRY up my time on creating these search forms. My first thought was to create a Search class and use active model, but it sucked because I everytime I had to create a new form I had to come into the search class and add a bunch of attr_accessors for all of the search fields. That sucks.

So I decided to mix it up with OpenStruct and ActiveModel

require 'ostruct'
class Search < OpenStruct
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming
 
  def initialize(*args)
    super
  end
 
  def persisted? ; false ; end;
end

Now I use this one model for searching of all of my resources.

I create the form super easily with SimpleForm:

= simple_form_for(@search, url: my_cool_path(), html:{method: :get}) do |f|
  = f.input :text
  = f.select :category,  Product::CATEGORIES
  = f.input :minimum_price
  = f.input :maximum_price
  = f.submit :search

In your controller you are going to get a params[:search] hash with :text, :category, :minimum_price, :maximum_price.

Want to persist that search across page reloads?

class ApplicationController < ActionController::Base
  before_filter :persist_search
  protected
  def persist_search
    @search = Search.new params[:search]
  end
end

Now you'll have a Search object in all of your requests with your user's search params. Use it as you will to return them the resources they are looking for.

*Side note*
I use the above logic for all of my resources, until one of my resources has a requirement like a validation. Then I break it into something like FlightSearch:

require 'ostruct'
# ostruct for that lazy goodness
class FlightSearch < OpenStruct
  # Get activemodel in there for all its cool functionality like model name, pluralization, validation, yada yada yada
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend  ActiveModel::Naming
 
  validates_presence_of :origin_airport, :destination_airport
 
  # you can do cool things here, just make sure you call super so ostruct 
  # takes all those hash params and makes them methods
  def initialize(*args)
    super
  end
 
  # ActiveModel needs to know that this wasn't persisted.
  def persisted? ; false ; end;
end

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

17Oct/110

Airbrake’s Javascript Error Reporting and Google

So today I turned on the experimental JavaScript Error Reporting in Airbrake today. Within about 5 minutes my inbox was bombarded.

Looks like Google the masters of writing software for everything are very diligent in testing their code.

I have a feeling I'm going to have to add a lot of exclusions to my "Global Error Classes" list.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

7Oct/110

Using devise’s scoped url helpers in Cucumber; new_registration_path, new_session_path

You can use devise's scoped url helpers from cucumber, but you have to use them by their real Url Helper method names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module NavigationHelpers
  def path_to(page_name)
    case page_name
    when /the sign up page/
      # dont use the helper's helper
      # new_registration_path(:user)      
 
      # use the underlying url helper
      new_user_registration_path
    when /the sign in page/
      # dont use the helper's helper
      # new_session_path(:user)      
 
      # use the underlying url helper
      new_user_session_path
    else
      #...yada, yada

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

14Apr/110

Application stuck installing on iPhone

I was trying to install an app last night while buzzed in a shady dive bar with no signal.

It took forever and eventually my phone gave up. I figured when I was on a decent network it'd continue installing. Nopecity.

Today the app has about half the meter full.

How do you fix it?

I tried clicking. Nope. Deleting? Nope - it was disabled.

Reboot the phone? Yea! It prompted me for my password and all is good.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

12Apr/110

cucumber.yml was found, but could not be parsed. Please refer to cucumber’s documentation on correct profile usage.

Yeah, I got that message today and wasted about 30 minutes of my life.

I hadn't changed my cuke yaml file since I started work on the app.

I tried:
* upgrading cucumber (FAIL)
* using another yaml file (FAIL)
* upgrading gherkin (FAIL)
* hard reseting the branch I was on (FAIL)

Turned out to just be a bum rerun.txt file. So I deleted it.

I need a refund on that 30 minutes.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon