MetaSkills.net

Bit Fat Legacy Schema Inspection

Posted On: July 7th, 2010 by kencollins

Sometime in rails 2.x the #inspect method for an ActiveRecord class was changed to show you all the column names (attributes) of that class. This is fine when things are small but if your working on a big legacy schema and you want clean terse debugging, all those column names can be noisy. I just set this initializer up today to kill it. Now the class just shows the number of columns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

module ActiveRecord
  class Base
    class << self
      
      def inspect
        if self == Base
          super
        elsif abstract_class?
          "#{super}(abstract)"
        elsif table_exists?
          "#{super}(#{columns.count} columns)"
        else
          "#{super}(Table doesn't exist)"
        end
      end
      
    end
  end
end

Custom Webrat::Session formatted_error For Rails With Nokogiri

Posted On: July 6th, 2010 by kencollins

I never liked how Webrat shows the complete response body for exceptions when integration testing rails applications. For the longest time I redefined the formatted_error method to simple do nothing. Today I used Nokogiri to parse out the good bits from that page. Here is the result. Not pretty, but it's working fine so far. Got a better example? Please share!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

module Webrat
  class Session
    def formatted_error
      doc = Nokogiri::HTML(response_body)
      exception_name = doc.css('head title').inner_html.squish
      exception_msg = doc.css('body h1').inner_html.squish
      exception_detail1 = "".tap do |detail|
        d = doc.css('body p')[0]
        detail << d.content.strip
        detail << d.next_sibling.content.squish
      end
      exception_detail2 = "".tap do |detail|
        d = doc.css('body p')[1]
        detail << d.content.strip
        detail << d.next_sibling.css('code').first.content.strip
      end
      app_trace = doc.css('#Application-Trace pre code').inner_html
      [exception_name, exception_msg, exception_detail1, exception_detail2, app_trace].join("\n")
    rescue
      "Could not format page exception. Perhaps try to use Nokogiri on this: \n#{response_body}"
    end
  end
end
Tags: ruby, rails, bdd, webrat

RESTful AJAX with Forgery Protection (In Rails 3)

Posted On: April 12th, 2010 by kencollins

A while back ago I wrote an article about how to use Rails built-in forgery protection in your RESTful AJAX calls. Normally AJAX requests, those responding true to request.xhr? in rails, are forgery whitelisted. But sometimes, and under what conditions I am not sure, AJAX methods are subjected to forgery protection. Maybe you have the ActionDispatch::Request#forgery_whitelisted? overridden to not include AJAX requests? Either way and for whatever reason – if you like to use forgery protection in your RESTful AJAX calls to rails, then here is the new implementation under Rails 3 beta2.

Synchronizing Core Data With Rails (3.0.0.pre)

Posted On: February 11th, 2010 by kencollins

This is my presentation to our local @757rb/@757objc users group this past Tuesday. Hope some find it useful. Lessons learned from building HomeMarks native iPhone application to synchronize Core Data with a RESTful backend built using rails 3.0.0.pre. This covers a previous design methodology called the AJAX head pattern which decouples rails applications from the views they present which allowed an easy API foundation for the iPhone application and data sync methods.

Resources

Rails Button Links In Embedded Forms

Posted On: January 5th, 2010 by kencollins

This is one I have had sitting around for almost 3 years now in my toolbox and thought I would share. Have you ever had complicated rails forms and needed simple form buttons that just took you to a simple link? Were you bitten by the button_to helper code because it generates another form inside of a form? If so, here is a simple rails view helper I made that creates simple button links for embedded forms by making an input with a javascript function. Tag soup you ask, hell yeah, but worth if if you need it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

def button_to_link(name, link, options={})
  confirm_option = options.delete(:confirm)
  popup_option = options.delete(:popup)
  link_function = popup_option ? redirect_function(link,:new_window => true) : redirect_function(link)
  link_function = "if (confirm('#{escape_javascript(confirm_option)}')) { #{link_function}; }" if confirm_option
  button_to_function name, link_function, options
end

def redirect_function(location, options={})
  location = location.is_a?(String) ? location : url_for(location)
  if options[:new_window]
    %|window.open('#{location}')|
  else
    %|{window.location.href='#{location}'}|
  end
end
Tags: ruby, rails, form

Authenticated S3 GETs For Private Objects Using Paperclip

Posted On: November 23rd, 2009 by kencollins

Yea I know, I am probably the last person on earth that is just getting around to using Paperclip. To be honest, most of my file upload code was written way before Paperclip or even AttachementFu was ever conceived. And frankly, I do not do much social app coding on the side - so the need never came up. But that changed recently and I wanted a really really good way of leveraging AWS::S3 storage with the best local app security while maintaining tight control over the files.

Tags: ruby, rails, paperclip, s3

757 Studio

Posted On: October 4th, 2009 by kencollins
757 Studio Website

Well it took up quite a bit of my free time and a good 2 weeks of no iPhone development, but I am very very happy that the organization of my first event has been a success so far. Check out 757studio.org for full details. Andy Hunt is headlining the event with a talk on Pragmatic Thinking and Learning. Clinton Nixon and Jamie Pinkham will be following with talks on Ruby/Rails and iPhone/Cocoa. So even though I can not make it to the West Coast for RubyConf this year, it is not so bad with a local event like this.

Tags: ruby, rails, studio, objc, 757, cocoa