Archive for the ‘General’ Category

Fun with active_record and sqlite3

Friday, April 20th, 2007

Active Record, the ORM layer in the Rails cake (and camping, and Merb, etc) is strong magic.

Before stumbling on this trick, I would create new rails projects for every little thing I needed to figure out. Running
rails test_proj -d sqlite3
is just so easy!

Yesterday, I needed to figure out a way to have a search term provide alternates (”dog” might have “puppy”, “hound”, etc.) After thinking about it for a few minutes, I decided I needed to figure out how to play with ActiveRecord without a full rails project. Here it is: ar_playground

[source:ruby]
#!/usr/bin/env ruby

%w|rubygems active_record irb|.each {|lib| require lib}

ActiveRecord::Base.establish_connection(
:adapter => “sqlite3″,
:database => “test.db”
)

ActiveRecord::Schema.define do
create_table :fruits, :force => true do |t|
t.column :name, :string
end
end

class Fruit < ActiveRecord::Base
validates_presence_of :name
end

Fruit.create(:name => “apple”)

IRB.start if __FILE__ == $0
[/source]

This will create a sqlite3 database in the directory where it’s run, and drop you into an IRB shell with all the Classes available for playing.

If you have trouble with sqlite3, you can use mysql, or get sqlite3 running by checking out:
SQLite3 announcement on Jamis’ blog

If you’re curious what I came up with for my problem, here’s the pastie.

Peglist - my first real camping project

Wednesday, April 11th, 2007

Update: I forgot to link to the repos! Here it is:
http://opus.winelibrary.com/peglist/trunk/.
There is a key for a popular photosharing site in there (rhymes with ticker), please don’t abuse it.

Camping, a ruby “micro-framework” is really a lot of fun once you get into it. It has sessions, ActiveRecord, MVC and a few other niceties that are great for “quick” things that don’t need to be full Rails projects.

So, in my spare time I’ve been playing with rewriting a php tool I wrote a while back for helping me memorize my Peg list. I made it for a few reasons: To play with Camping again, to work on my design skills, and make my old system much more usable.

Peg list by Meta | ateM.

    I wanted anyone to be able to play with the tool, I’ve added a few features that should allow anyone to jump right in, even if they have no interest in memory pegs:

  1. OpenID Logins
    These were pretty easy to get working in Camping. Even so, I’d like to make (or have) a plugin for Camping that would let me drop in OpenID auth anywhere.
  2. an “I’m too lazy for this. Do it for me.” button
    I figured that most people I showed this too wouldn’t really care about pegs. What better way then to pre-populate pegs for those people?
  3. Avatars from Flickr and Twitter
    Why bother writing an image uploader / cropper when I can just take a username and grab an existing avatar? I could add other services easily (like gravatar) and I might do that one day!
  4. Flickr API integration
    This is one of the few things my old tool did have. I’ve expanded it and made searching for an image easier and slicker.
  5. A Favicon!
    I always forget to make favicons. Thanks to Dan at Simplebits (from a recent set of slides he put up), I decided that this project HAD to have a favicon. It wasn’t hard, and ads a bit of polish to the site.

ma.gnolia thoughts

Thursday, February 22nd, 2007

So, I went to the Future of Web Apps summit in London this week. I may have a full post about that soon—but this isn’t it.

During a presentation by Tara Hunt and Chris Messina (Fostering Online Communities), Chris showed a cool new feature of ma.gnolia that really piqued my interest. Despite being a long-time del.icio.us user (my first del.icio.us bookmark was on 12/31/2003), I thought I’d try ma.gnolia (again) with a fresh perspective and an open mind.

Here are a few of my thoughts on the site and app after using it for just a few minutes:

  1. Using OpenID – Good job :)
  2. They need help with their “Blank state” pages (pages that don’t have data yet). When I first signed in, and didn’t have any bookmarks, friends or groups, my main page was a bit bare. Blank slates are a great opportunity to show the user either what the page could look like when they’re really rockin’ or, a call to action on how to start rockin’. 37Signals has lots to say about this subject.
  3. The Dashboard widget is neat. A few complaints about it:
    • it doesn’t allow me to sign in with OpenID (which the main site does)
    • just like their popups, the tag suggestions are based only off MY bookmarks, not other peoples. This is something that may seem small, but by showing what others have tagged a site with (and even helping you type them), you can amplify a common vocabulary.
    • It puts the contents of my clipboard in the description portion… yuch!
  4. My import from del.icio.us still hasn’t happened yet! What gives?
  5. It’s slow. Could be bad timing on my part, or my sketchy WiFi in London, but it’s a lot slower than del.icio.us at this moment. I’m going to reserve judgement on that for now.
  6. It’s NOT just a pretty a del.icio.us clone (anymore) – SHOCKING! I held this belief until talking to Chris about the site, and actually playing with it myself. I now see that I was unfairly judging it (I do think they have an image problem though). What it gives you above and beyond del.icio.us are the community aspects of something like Flickr. A really neat bit is the “give thanks” (which is the feature I alluded to before)
  7. Their pagination really stinks… in a few places, each “page” is 5 or so items… What.The.Hell!
  8. Commas instead of spaces for tagging. This is 100% a personal preference, but I like spaces!

I think they’re doing some good stuff—I’m not ready to throw out del.icio.us just yet, but I will be posting to both services until one of them is less compelling to me.

5 thingz u didnt no bout me

Monday, February 12th, 2007

Dr Nic hit me up with the 5 things meme that’s been floating around for a few months now. It’s one of those parties you think is totally lame until you get your invitation. Without further Ado, I thee blog:

  1. I lived in my car for a few months.
    While I was in San Francisco, I was homeless, but had my car (An Isuzu Rodeo). Each night, I’d drive to the top of a hill near where I had stayed previously, prop up a sheet around all my windows so the cops wouldn’t look in, and sleep on the futon I had brought with me. It’s not as glamours as it sounds.
  2. I got evicted once.
    After a long and strange series of events, I found myself moving back to SF after having moved back to New Jersey with an interesting character. Turns out we didn’t get far because:
  3. I dated a crack-head
    I didn’t know it at the time, but on the road to SF, she started having withdrawal symptoms (from heroin, not crack) and we had to come back (she told me it was the Flu). Another time she stole all our rent money to buy crack.
  4. I can pop my shoulder out of it’s joint—almost to the point of dislocation.
    I decided that this one shouldn’t be about some of the strange happenings in my life… It’s just a trick I can do, and I have a few others. Ask me to demonstrate if you see me.
  5. My Isuzu Rodeo was taken by the city of San Francisco.
    So, while working at Pacific Bell Internet, I didn’t like parking in the faraway lot each day. I’d plop my car in front of 303 2nd ave and invariably get a ticket each afternoon (sometimes even two, score!) I thought, “I’m sure no one’s tracking this – I have out of state plates!” I was wrong. One day, I came out of work to find the spot I had parked in that morning taken by someone else. I thought my car was stolen—turns out the city took it as “Payment.”

Are we done already? I have a few more stories… oh well – next time.
Just to CYA, one of these might be made up :)

I hereby infect:
John K.
Amy Hoy
Chris Wanstrath
Phil Hagelberg
Dan Benjamin

Using Yahoo!’s BBAuth with Rails

Friday, September 29th, 2006

I’ve been meaning to try simply_restul for a while now, and today’s announcement on rails-core pushed me over the edge. It’s no longer a plugin, it lost it’s name and changed a few things, but it’s pretty much the same. I’m going to do a quick tutorial on setting up a RESTful controller. It’s not hard — I was just scared of it!

rails simple

This creates a new rails project named “simple”. Without a -d flag, it will use mysql by default

cd rails

mysqladmin -u root create simple_development

mysqladmin -u root create simple_test

create our databases - if this were a longer tutorial, we’d test it as we went.

rake rails:freeze:edge

Make your project use the latest version of rails “Edge”. You have to do this if you want to follow along. In the future, this step will be unnecessary

script/generate model Person

Our example object - a Person
[source:ruby]
db/migrate/001_create_people.rb:
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column "name", :string
t.column "bio", :text
end
end

def self.down
drop_table :people
end
end
[/source]

rake db:migrate

We’re creating our table now using a migration. If everything went well, you’ll now have a table in your database - “people”, with two columns, name and bio.

Add the following line to your config/routes.rb

map.resources :people

(this is a change from simply_restful, plural now) Here is where the magic happens. This one line of code will give you a bunch of stuff. All the mappings between REST and your controller will now just work.

script/generate controller people index new create show edit update destroy

Create our controller with all the REST / rails actions.

app/controllers/people_controller.rb:

Here’s a lot of code. This is the person controller and all the actions that you want to do on a person. They map to HTTP verbs very nicely. You should type the code (the generator made a lot if it for you, you just need to fill in the methods)

You’ll notice the funny “respond_to do |format|” stuff. This is one of the other slick features: you can send the client the format they want. So if someone were to request /person/1.xml - they clearly want xml output - this handles that (and so much more).

That was all fine and good. We could write a very nice test suite for all the code to now and still be lost on the views. Let’s go through them quickly.

app/views/people/index.rhtml

app/views/people/new.rhtml

app/views/people/show.rhtml

app/views/people/edit.rhtml

app/views/people/_form.rhtml

This concludes the whirlwind tour of the new RESTful Rails controllers.
I’ll be changing this post a lot based on feedback, so check back.

REST and Rails - no reason to be scared

Monday, July 31st, 2006

I’ve been meaning to try simply_restul for a while now, and today’s announcement on rails-core pushed me over the edge. It’s no longer a plugin, it lost it’s name and changed a few things, but it’s pretty much the same. I’m going to do a quick tutorial on setting up a RESTful controller. It’s not hard — I was just scared of it!

rails simple

This creates a new rails project named “simple”. Without a -d flag, it will use mysql by default

cd rails

mysqladmin -u root create simple_development

mysqladmin -u root create simple_test

create our databases - if this were a longer tutorial, we’d test it as we went.

rake rails:freeze:edge

Make your project use the latest version of rails “Edge”. You have to do this if you want to follow along. In the future, this step will be unnecessary

script/generate model Person

Our example object - a Person
[source:ruby]
db/migrate/001_create_people.rb:
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column "name", :string
t.column "bio", :text
end
end

def self.down
drop_table :people
end
end
[/source]

rake db:migrate

We’re creating our table now using a migration. If everything went well, you’ll now have a table in your database - “people”, with two columns, name and bio.

Add the following line to your config/routes.rb

map.resources :people

(this is a change from simply_restful, plural now) Here is where the magic happens. This one line of code will give you a bunch of stuff. All the mappings between REST and your controller will now just work.

script/generate controller people index new create show edit update destroy

Create our controller with all the REST / rails actions.

app/controllers/people_controller.rb:

Here’s a lot of code. This is the person controller and all the actions that you want to do on a person. They map to HTTP verbs very nicely. You should type the code (the generator made a lot if it for you, you just need to fill in the methods)

You’ll notice the funny “respond_to do |format|” stuff. This is one of the other slick features: you can send the client the format they want. So if someone were to request /person/1.xml - they clearly want xml output - this handles that (and so much more).

That was all fine and good. We could write a very nice test suite for all the code to now and still be lost on the views. Let’s go through them quickly.

app/views/people/index.rhtml

app/views/people/new.rhtml

app/views/people/show.rhtml

app/views/people/edit.rhtml

app/views/people/_form.rhtml

This concludes the whirlwind tour of the new RESTful Rails controllers.
I’ll be changing this post a lot based on feedback, so check back.

The most important things I learned about Rails development from RailsConf and RailsDay

Wednesday, June 28th, 2006

I was fortunate enough to attend Rails Conf 2006, held between 6/23 and 6/25 in Chicago. I also participated in RailsDay 2006 on June 17th.
I’ve had a very edifying few weeks. I’m going to take a few minutes and enumerate (get it?) some of the bigger lessons learned:

  1. Know your problem domain.
    This has to be my biggest stumbling block. I’m all about going into development, guns blazing and worry about the big picture later. This is a mistake in the Rails’ world of convention over configuration. If you take some time in the beginning to figure out what models you’re going to need and how they interact with each other, you’ll be much much better off.

  2. Put as much functionality into your models as you can.
    Models are for your Business Logic. That concept escaped me until RailsDay (and even more so at RailsConf). In simple terms, you should be able to interact with your application from script/console and still have it follow the rules for your specific area.

  3. Let Rails do it’s thing and get out your way.
    This one was more from Martin Fowler’s keynote then anything. What Rails gives you is the ability to not worry so much about the “plumbing” of your app, and focus on what makes your problem domain interesting and unique.

  4. Don’t worry about the Rails Way, just get it out there and itterate!
    I heard more then once, “But I want to know the Rails Way of doing it”. I’ve felt that MANY times, and I’m giving up. This is the worst kind of analysis paralysis. Just get it out there so you can work on something real and formed.

  5. Testing isn’t scary!
    This was HUGE for me. I never tested because I thought it was hard and scary. During my RailsDay project (My one goal), I decided to just go for it with testing. You know what I found? It’s EASY. That plus Mike Clark’s Panel really taught me not only the ease of testing, but the utility. Here’s a side story, I just moved my app from Text Drive to my local server. It wasn’t working at all, so I decided to give my out of date tests a run (I gave up TDD at about 7pm on RailsDay), a LOT of them failed. It took some time to get them running, but now it works better. And each bug report that goes in gets a test case before it’s fixed.

  6. Everything is CRUD
    This was a big part of DHH’s keynote. It was hotly debated afterwards, but regardless, it’s an interesting way to look at how you set up your application. The gist is this: most of your controllers should JUST do CRUD, and if you think that a controller can’t (like a login), maybe it can. And by reframing your problem in this way, problems just go away some times. I haven’t been able to try this out yet, but I’m looking forward to it.

  7. Nobody does it right the first time
    This goes back to point 4. Basecamp didn’t spring-forth fully formed. Scott Raymond is still tweaking the “done” iconbuffet. What amazes me is that not only do all these apps keep getting bug-fixes, but developers actively go back and make their code “prettier”, which brings me to my final point

  8. Beauty matters
    When you craft your code (even if it’s not the first iteration of it) to be pleasing to you, not only are you more willing to go back and work on it, but you’re more likely to be able to bring someone new into the project. And you’ll feel better about the whole process.

  9. Shoulder Surfing is way underrated
    I was able to watch Amy Hoy and Scott Raymond work “live”. Amy was working on a design comp (whom received it right there over irc – AGILE!). Scott was doing Rails work. I have to say that those two experiences were huge for me. I’d like to see a panel next year of just live coding with breaks for questions. Between learning editor shortcuts, Rails and Ruby idioms and how some people “flow”, I think more can be conveyed then any text, video or talk can. Watching someone do what they do live is a very wide band. So thanks to both of you!

RailsConf: Things I’ve learned

Sunday, June 25th, 2006

Here’s a list of stuff I’ve learned and pointers to cool stuff.

RailsConf Panel: Selling Music in the 21st Century with Ben Curtis

Sunday, June 25th, 2006

!http://static.flickr.com/45/174612466_dc75eb189b_m.jpg!:http://flickr.com/photos/kastner/174612466/

Ben Curtis runs the plugin directory on rails

21st music is trying to fill the gap between 100% independent and 100% soul-sucking RIAA.
Ben’s major role was in making the shopping cart e-commerce site.

h3. Who is Ben Curtis?

* a startup guru
* worked at real networks
* worked at upscale USA (perl and php)
* March of ‘05 he learned about Rails
* He’s not the Dell guy - even though they have the same name
* Also not the pro golfer
* “bencurtis.com”:http://bencurtis.com
* “agilewebdevelopment.com”:http://agilewebdevelopment.com
* “tesly.com”:http://tesly.com
* stympy on #ror

h4. He wrote a book - “http://www.agilewebdevelopment.com/book”:http://www.agilewebdevelopment.com/book

h3. Why build a store?

* Because you’d be out of a job if you told your client to just use Shopify!
* It’s hard.
* Do NOT build one of you want to sell one product tomorrow - use Shopify or PayPal
* But if you hate all the OSS ones, and need a real cart, write your own
* BoxCart is a Rails cart
* If you have specific business rules

h4. “It’s Just” - but it’s really not.

Ben thought it was going to be easier then it actually was.

Issues
* inventory
* attributes
* options
* payment methods
* shipping methods
* refunds
* affialiates

CDBaby and 21st both thought it would be easier then it actually was!
21st is some where around 6,000 LOC

Your client needs to do work for you! Product shots, product info, shipping info, etc etc.
Don’t forget your SSL cert.
Site design takes work also.

h4. Products and Variants

!http://static.flickr.com/53/174627666_1da2d949d6_m.jpg!:http://flickr.com/photos/kastner/174627666/

Make sure you have to write the cart.
Put all your business logic in your models!

h4. Payment Processing

TrustCommerce.com - Ben says they’re great.
PayPal’s API doesn’t like ruby (SOAP4R)
ActiveMerchant? (what is this?)

h3. Sensitive Data

* Objective: speedy check out for customer
* Problem: CC data is a liability
* Solution: Pass the buck
* Alternate solution: Encryption

h3. Gift Certificates suck.

h3. Working with Sales

h3. Taxes

h3. Returns and Refunds also suck

h3. Shipping - it’s a big deal.

h3. Affiliates

h3. Testing - DO IT and use Mocks with it

RailsConf Panel: Open Laszlo with Mike Pence

Sunday, June 25th, 2006

!http://static.flickr.com/71/174516202_d32c95948f_m.jpg!:http://flickr.com/photos/kastner/174516202/

“Laszlo on Rails”:http://wiki.openlaszlo.org/Laszlo_On_Rails

“http://mikepence.wordpress.com”:http://mikepence.wordpress.com

OpenLaszlo is blurring the line between desktop and web apps.
On the internet today, there is a movement towards a better look, feel and more interactivity.
Pimp my site (mypace, et. al).

Brookers from youtube demo :)

Web apps need Sex, Drugs and rock and roll which is a fun way of saying Pretty, Immersive and Rich.

Pandora is written in Laszlo.

Lzx is laszlo’s language. Kind of like XUL for Mozilla. It uses declarative XML.

Can I edit lzx in my favorite editor?

h4. Databinding

XML, even with remote http. Uses XPATH to access nodes and collections.

(And now a group hug for those of us who hate flash)

There is also a DHTML runtime, so you don’t have to worry about flash.

“openlaszlo.org”:http://openlaszlo.org

Demo of LZPics - a Flickr viewer in DHTML by way of Laszlo. Very cool and responsive.

“gliphy.com”:http://gliphy.com/

BlogBoxes are a way to give users a widget to take with them - to MySpace or wherever!

Not all roses - resources, searching and others aren’t the best. But there is a accessibility API.