Archive for December, 2005

Zen Spider Software

I found Zen Spider Software tonight and it looks like an interesting site for both its content as well as its underlying software. It’s got a Ruby section as well.

Advertisements

Upcasing Dynamic Find Methods in Rails

Earlier today I discovered that dynamic finder methods such as `find\_by\_some\_attribute` wouldn’t work with my `UPPER\_CASE` column names in my legacy database tables unless I wrote them as their literal case: `find\_by\_SOME\_ATTRIBUTE`. The hack was as simple for the class level `method\_missing` as it was for the instance method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# This enhances Base's class method
def self.method_missing(method_id, *arguments)
  if match = /find_(all_by|by)_([_a-zA-Z]\w*)/.match(method_id.to_s)
    finder = determine_finder(match)

    attribute_names = extract_attribute_names_from_match(match)
    # just upcase! the names
    attribute_names.each{|name| name.upcase!}
    super unless all_attributes_exists?(attribute_names)

    conditions = construct_conditions_from_arguments(attribute_names, arguments)

    if arguments[attribute_names.length].is_a?(Hash)
      find(finder, { :conditions => conditions }.update(arguments[attribute_names.length]))
    else
      send("find_#{finder}", conditions, *arguments[attribute_names.length..-1]) # deprecated API
    end
  elsif match = /find_or_create_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
    attribute_names = extract_attribute_names_from_match(match)
    # just upcase! the names
    attribute_names.each{|name| name.upcase!}
    super unless all_attributes_exists?(attribute_names)

    find(:first, :conditions => construct_conditions_from_arguments(attribute_names, arguments)) || 
      create(construct_attributes_from_arguments(attribute_names, arguments))
  else
    super
  end
end

I just put this in an abstract model class that is extended by the legacy models and I can now use lower-cased `find\_by` dynamic methods.

More Rails-Legacy Integration Issues

Today I wanted to use the find\_all\_by\_some\_attribute() dynamically generated method. Since my table has UPPPER_CASE column names, this only works if the attribute part of the dynamic method is also: find\_all\_by\_SOME\_ATTRIBUTE(). I am hoping I can hack this without too much problem. The code must be using the attributes rather than the methods.

TextMate Manual

Glad to see that TextMate now has a manual.

One of Those Days…

Was gonna work at home, at 8am the electricity went out reminding of the work being done in my building, went off to a cafe, worked on a tough problem for a while, needed svn\_load\_dirs.pl, but wasn’t on my version of Subversion, figured I’d upgrade my DarwinPorts installations, everything broke, couldn’t fix, went into client’s office, tried to install DarwinPorts from scratch, but client’s Internet connection is lame, so DarwinPorts kept stalling, went home because electricity was supposed to be back on, got home to “two more hours!”, went to another cafe, finally got good Internet connectivity, got DP reinstalled, went home got my real work going again, tests passing for MySQL, but not SQLite, “what the ?$?$?”, later realized I needed Swig installed, uninstalled sqlite3-ruby gem, installed Swig, installed sqlite3-ruby again, tests passing for SQLite, VPN connection not working so I can’t commit changes, nearly 1am. Whew.

Every once in a blue moon it just goes that way, doesn’t it?

People Want Fun!

Well, we do don’t we? And we’re built like this for a reason. As this blog post says, brains need playful work. We learn better when playing.

Rails Auto Completion

I am glad to see how simple it is to add an auto-completion text field to a Rails app. Since I want this available to all my controllers, I stuck the controller code in ApplicationController:

1
2
3
class ApplicationController < ActionController::Base
  auto_complete_for :recipe, :name
end

Then to create the simple form, I just add the following code into my layout file:

1
2
3
4
<%= form_tag :controller => "recipe", 
    :action => "find_recipe_for_name" %>
  <%= text_field_with_auto_complete :recipe, :name, {:size => '12'} %>
<%= end_form_tag %>

Finally, I set up my action:

1
2
3
4
5
def find_recipe_for_name
  @recipe = Recipe.find(:first, 
    :conditions => ["name = ?", params[:recipe][:name]])
  render :template => 'recipe/show'
end

Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved