rails

Quick HOWTO for Hirb formatter for irb/Rails console…

Hirb is pretty object formatting for irb (and the Rails console). If squinting at the standard Object#inspect output is slowing you down (trust me, it is), you should look into Hirb. Here’s <5 minute setup…

Install the gem:

sudo gem install hirb

Add these lines to your $HOME/.irbrc file, which will cause Hirb to be auto-loaded at startup for both irb and script/console:

require 'rubygems'

require 'hirb'
extend Hirb::Console
Hirb::View.enable

Try it out in irb:

$ irb
irb(main):001:0> table ['foo', 'bar']
+-------+
| value |
+-------+
| foo   |
| bar   |
+-------+
2 rows in set
=> true
irb(main):002:0>

Try it out in script/console:

jay@dandelion:~/Projects/myproject
$ script/console
Loading development environment (Rails 2.3.2)
>> MyModel.all
+----+-----------------+-------------------------+-------------------------+
| id | name            | created_at              | updated_at              |
+----+-----------------+-------------------------+-------------------------+
| 1  | Foo             | 2009-06-16 23:38:10 UTC | 2009-06-16 23:38:10 UTC |
| 2  | Bar             | 2009-06-17 00:38:16 UTC | 2009-06-17 00:38:16 UTC |
+----+-----------------+-------------------------+-------------------------+
2 rows in set

Hirb has customization options I haven’t even begun to explore, so be sure to look at its docs.

development
rails
ruby

Permalink

Me with Jason Seifer and Greg Pollack (The RailsEnvy guys) after their talk at RailsConf Thursday.

RailsEnvy

If you’re a Rails developer and haven’t been listening to their podcast, you need to start!

development
rails
ruby

Permalink

Notes from RailsConf - Starting Up Fast: Lessons from the Rails Rumble…

Hosted by Nick Plante (Nth Interactive)

Would’ve liked to give attributions for these quotes, but it wasn’t practical. Sorry!

Rails Rumble:
        48 hours to design, develop, and deploy a micro-app.
        Public voting.
Panel:
        Joe Fiorini
                Grand prize winner, 2008
                meetbetween.us
                Team of 4
        Ben Scofield
                Solo Division, 2007, 2008
                Forever Home (down)
        Chris Saylor
                Grand prize winner, 2007
                tastyplanner.com
                Team of 4
        James Golick
                Most Useful, 2008
                "What Does This Error Mean?" site
"How is the Rumble like working on a normal Web project?  How does it differ?"
        Time constraint means you won't be building a social network, etc.
        What can you cut out?  Do you need a login system, for example?
        People likely to visit site, make snap judgement on first page, and
        leave again.
"Is it possible to build a real product this way?  Have you continued to
develop your entry apps?"
        Real Simple Magazine did a couple hacknights later.  Someone contacted
        them to acquire it later.
        Joe got approached by yellowpages.com, though nothing has materialized
        yet.
        No matter how great your initial idea is, you don't REALLY know what
        you want to build until you start.
                Getting app active and getting feedback is great.
"What sort of up-front planning and design work did you do?"
        Complete wireframe on pen and paper (digital wireframes not allowed),
        assignment of tasks to developers.
        "None."
        5 or 10 stories in Pivotal, not much.
        Color selection, finding stock photos.
"How much time did you spend in prep, and does that affect the spirit of the
Rumble?"
        You can plan as much as you want if you're not creating digital assets.
         Much of this is on the honor system.
        "We didn't want to spend 48 hours thinking, [so we planned up front]."
        Moderator: We want to encourage use of the sea of third-party plugins
        that are out there.
        "We got shit marks for design, probably because we didn't do any."
"How did you plan for the 80/20 rule?  (20% of small tasks take 80% of the
time.)"
        We ensured that even if there were small tasks we hadn't completed, we
        could still deploy.
        Ben Scofield expounds: Version he was working on at deadline had a bug,
        but version control let him back out to a stable version.
        We talked about features we wanted up front.  Got a backlog going.  Our
        mantra: "Think of Google".  Try to have nothing but a search box and a
        couple buttons.
        A 48-hour competition is a terrible place to mentor someone.  Make sure
        you know all the
"Did you use automated testing when you built your application?"
        No.  TDD can make you faster for big apps, but it slows you down on
        small ones.
        Yes, some.
        Yes.  Automated tests are great for catching regression.  It's just a
        good development practice.
        Testing is a method of design.  If I need to get something done quickly
        and can't test, I do 'design by wishful thinking'.
                Write the code the way you want to see it, then go implement
                it.  [Sounds like comment-driven development to me.]
        Don't let them fool you.  You can still win if you don't do testing.
"What plugins/gems/tools did you find most helpful?"
        Plugin by James Golick helped build RESTful contollers without need to
        code.
        Starter apps - Bort, Blank.
        Moderator: Rails 2.3 Templates offer starter-app-like functionality.
        Factory Girl, Shoulda, Mocha.  Make Resourceful.
        New Relic RPM - it helped us at one point when LifeHacker post
        generated 2000 hits during voting period.  MySQL fried, not Rails, and
        RPM helped spot that.
        Almost everyone used Campfire.  Inline paste, inline images, chat
        history are nice.
        Basecamp.
        Pivotal Tracker.  Super-lightweight tool to keep organized without
        eating our time.
"How did you find other people to work with and decide that you could work with
them?  Is it anything like finding co-founders for a startup?"
        Talked to good friends first.  At last minute, had to call every local
        Rails developer we knew to replace a dropout.  Worked out well.
        Find a well-rounded team.  Sysadmin, DB design, graphic design,
        developer.
        Good friends from local developer community.  Wound up hiring two of
        them.
        Local user groups.  Refresh to find designers.  Ruby User Groups for
        developers.
"What was the most rewarding part?"
        Getting to work with awesome people.  Did screencast too.  It was a lot
        of fun.
        Getting to know people better.  Getting to release a completed app- so
        often, real life interferes with completing a project; not here.
        You get so bogged down working on large apps for an employer.  This
        reminds you what's great about coding.
        Moderator: "Launching shit is awesome."
"How much sleep did you get?"
        4 each.
        6 each.
        4-6 each.  One member went to concert, but coded in car on way there
        and way back.
        6 hours per night.
        You don't have to work 44 of the 48 hours, just set realistic
        expectations.

development
rails
ruby

Permalink

Notes from RailsConf - Rails 3: Step Off the Golden Path…

Hosted by Matt Aimonetti.

Rails philosophy:
	Convention over configuration
Merb philosophy:
	Performance
	Framework agnosticism
Rails 3 philosophy is also a merger:
	Performance
	Modularity
	Framework agnosticism
What you get:
	Public API (It was closed for Rails 2)
	Mountable apps (mount your blog app in your CMS app)
Default stack:
	ActiveRecord
	Test::Unit
	Prototype
	ERB
But, it's less opinionated:
	Other Javascript frameworks:
		jQuery
		MooTools
		ExtJS
		Yahoo!
	Other templating engines:
		Haml
	Other ORMs (turn DB records into objects):
		DataMapper
		Sequel
		CouchRest
	Other test frameworks:
		Cucumber
		RSpec
When to step off the golden path:
	If your templating, JS, ORM, or performance requirements differ.
	Otherwise, use the default stack- it's real-world tested.
Datamapper:
	Procrastination as a virtue:
		Lazy Loading - Don't pull fields until they're asked for specifically.
		Strategic Eager Loader
		ActiveRecord:
			Student.all.each.books.map {|b| b.name}
			select * from "students"
			select * from books where student_id = "1"
			select * from books where student_id = "2" #etc. This is slow!
		DataMapper:
			Student.all.each.books.map {|b| b.name}
			select id, name from students order by id
			select id, name, student_id from books where
				(student_id in (1, 2, 3, X)) order by id --Faster
	Multiple "repos" (databases)
		Config:
			production:
				adapter: mysql
				database: production-app
				host: localhost
				...
			repositories:
				nightly_backup:
					adapter: sqlite3
					database: shared/nightly.db
				weekly_backup:
					...
		Then set up a task to copy from DB to repo:
			Article.copy(:default, :nightly_backup, :created.gt => 1.day.ago)
		Legacy databases:
			class Page
				include DataMapper::Resource
				property :id, Serial
				property :name, String
				#Specify different fields when talking to legacy database:
				repository(:legacy) do
					property :name, String, :field => "title"
				end
			end
	Query::Path
		#Joins people with addresses, finds records where street column LIKE '%street%'
		Person.all("addresses.street.like" => "%street%")
	Many adapters:
		RDBMS
		file system
		IMAP
		YAML
		SalesForce
		REST APIs
		your API here - write your own.
Sequel
	High performance
	Sharding
	Prepared statements
	Highly customizable SQL statements
Hibernate
	Built in sharding
	ActionORM
	JRuby
	Many Java libraries for legacy databases
Non-RDBMS systems
	AppEngine::DataStore
	CouchRest for CouchDB
	Redis, Tokyo Cabinet, etc...
More customizations available:
	File structure (including very_flat)
	Custom router DSL
	Custom request handlers
		class Presentation < ActionController::Http
			def index
				self.response_body = "Hello!"
			end
		end
		Presentation.action(:index).call(Rack::MockRequest.env_for("/railsconf"))

development
rails
ruby

Permalink

Notes from RailsConf - ActiveScaffold BoF session…

Thanks to Mike Gaffney and Kenney Ortmann for hosting this session, and for writing a cool framework!

http://activescaffold.com/

Install:
        script/plugin install
        git://github.com/activescaffold/active_scaffold.git
        Site says to pass -r rails-2.2 option; HEAD works fine for Rails 2.3.

Setting up a quick test app in Rails 2.3:
        Create app/views/layouts/application.html.erb:
                Ensure it contains a default HTML document.
                Ensure it has <%= yield %> in the body.
        Add these lines to the HEAD tag:
                <%= javascript_include_tag :defaults %>
                <%= active_scaffold_includes %>
        Create a model with a few fields and a controller.
        On your controller, add:
                active_scaffold :model_name
        Go view the index for the model (no need to create index.html.erb, def
        index, etc.):
                You should have full CRUD, sorting by column, etc.

Customizing:
        ActiveScaffold.set_defaults {|config| …} in your application
        controller.
        Model-specific settings in individual controllers.
        Override ActiveScaffold’s default views by creating a special folder in
        your views directory.

Additional notes:
        Home page doesn’t get updated as often as it should, but project is
        being actively maintained and used, and will support Rails 3.0 shortly
        after its release.
        For many-to-many relationships, you may need to have the view reference
        the join model.  See the mailing list.

Had this up and running in 10 minutes:

ActiveScaffold

development
rails
ruby

Permalink

RailsConf notes - Kevin Barnes - In Praise of Non-Fixtured data

Fixtures bad:
	Get highly unmanageable, especially when someone decides to import real data (:user_275, :user_276...)
	Live outside test.
	Can't change loaded values easily.
Factories good:
	Let you use sensible labels and tweak the created data.
	They reduce dependence on external data (manipulate values right in the test).
FactoryGirl:
	http://github.com/thoughtbot/factory_girl/tree/master
	spec/factories/first_model.rb second_model.rb...
	Code:
		Factory.define :user {|f| f.name 'Joe'; f.zip '34279'; f.association :employer}
		Factory(:user).should be_valid
		Factory(:user, :name => nil).should_not be_valid
	.association calls another factory (that you define) to generate associated model object.
Object Daddy:
	http://github.com/flogic/object_daddy/tree/master
	spec/exemplars/*_exemplar.rb
	Code:
		class User
			generator_for :name => 'Test User'
			generator_for :ssn, :start => '12343214' do {|prev| prev.succ}
		end
		@user = User.generate!
		@user.should be_valid
	[Barnes likes ObjectDaddy.  I don't 'cause of modificat
Others:
	machinist
	foundry
	fixjour

development
rails
ruby

Permalink

Bort with Rails 2.3, Authlogic, and Cucumber

We have a series of small new projects coming at work, and I hate project setup. We’d used Bort previously, but it uses Restful Authentication, and my boss was recommending Authlogic. I also didn’t want to have to set up Cucumber on five new code bases.

So, Git/GitHub being the wonderful platform it is, I forked the Bort repo and set to work:

Bort with Rails 2.3, Authlogic, and Cucumber

…I might have done better starting from scratch. Bits of Restful Auth kept disrupting my Authlogic setup, likewise Rails 2.2 code with 2.3.

But in the end I prevailed (I think). There may still be odd errors lurking in there. Those should be teased out by the many projects I hope to use it on, though.

development
rails
ruby

Permalink

Folders suck.

Maybe I’ll be navigating around Rails projects in a more coordinated fashion eventually, but right now I’m finding myself doing a full-project search for a string I saw in the rendered page somewhere. I don’t have to worry about whether it occurred in a template or a partial (or someplace else it really shouldn’t be), I’m just taken to the right place.

Jef Raskin promoted tossing out folders and filenames and so forth in favor of just searching the text of documents. He argued they’re overhead that people don’t use anyway. And seeing the rise of Google and desktop search (not to mention witnessing my own behavior), I think I agree.

development
rails
ruby

Comments (2)

Permalink

Cucumber/Webrat…

Is it just me, or are Cucumber tests in combination with Webrat really brittle? I’m generating messages to go in my inbox, and they weren’t showing up because I forgot to make them “from” the current user. I can’t say:

Given I am on the messages page #BAD
And there are 4 messages #BAD

…because that would load a page with no messages on it. It has to be:

Given there are 4 messages
And I am on the messages page

Not as versatile as Cucumber advocates seem to be promising. Maybe I’ll get used to this kind of stuff over time, but right now I’m encountering/generating way too many head-scratching bugs to be productive.

development
rails
ruby

Permalink

We’re testing a Javascript Jabber client…

[11:28:21] [chris@xmpp.foobar.com] test
[11:28:51] [chris@xmpp.foobar.com] The power of the Internet
[11:28:58] [Jay] internets ftw!
[11:29:42] *** chris@xmpp.foobar.com is Offline
[11:29:55] *** chris@xmpp.foobar.com is Online
[11:30:02] [Jay] miss me?
[11:30:10] [chris@xmpp.foobar.com] No :-)
[11:31:11] *** chris@xmpp.foobar.com is Offline [Replaced by new connection]
[11:31:13] *** chris@xmpp.foobar.com is Online
[11:31:19] [Jay] it's spiffy!!!!
[11:31:24] [Jay] hi ward!
[11:31:44] [chris@xmpp.foobar.com] hello
[11:32:06] *** chris@xmpp.foobar.com is Offline
[11:32:14] *** chris@xmpp.foobar.com is Online
[11:32:20] [Jay] wooo!
[11:33:14] [Jay] Jabber is not a truck.
[11:33:25] [Jay] It's like a series of tubes.
[11:42:34] [chris@xmpp.foobar.com] need to make a friends list
[11:42:45] [Jay] woo! i liek frenz
[11:42:57] [chris@xmpp.foobar.com] I can has friends.

development
rails
ruby

Permalink