Posts

Showing posts with the label Rails

Oracle Foreign Key without Index Test

We've been having some Oracle deadlock issues that have been hard to reproduce locally. After a lot of investigation and solving of important problems that happened not to be THE problem we figured out that while we've been pretty good creating integrity constraints in the database we have not been very good about making sure that every foreign key has a corresponding index. And that can lead to problems. So we had a situation were our documents table had a foreign key on the accounts table that was not indexed. So updating an account row lead to a whole table lock on documents (instead of just a row lock which would have happened if there was an index) and that was very bad when we had two separate processes where one was doing a bunch of accounts stuff and the other was doing a lot of documents stuff. Deadlocks for everyone! The sad thing is that if we had just drank the Rails cool-aid about having no integrity constraints in the db we would have been fine but we got into ...

Rails Conf 2010 Day 3

A few days late on this because I've been sick all weekend. There just has to be a way to do climate control in conference centers in such a way as to not destroy the planet and, more importantly, Jake's health. I seriously wore black jeans and 2 shirts on hot summer days and yet I was shivering and caught a cold. But let's get to last day of Rails Conf 2010 and my exciting adventures therein. (tweets are in italics ) Woke to "Relax" by Frankie Goes to Hollywood. Which is good 'cause my heart was pounding. Settled it with some bacon. #railsconf Yep, I could feel cold coming but I was in denial. I ran into Neal Ford at breakfast and we chit chatted about this and that and then he mentioned that he had read my blog post from yesterday. First thought: I published that sucker at 1am this morning and he's read it already? Second thought: Oh shit, what did I say about his keynote -- I think I called him a jerk at some point. But my brain was foggy and so...

Rails Conf 2010 Day 2

Image
Only 40 tweets today (re and otherwise). I must be slowing down in my old age. Tweets are in italics . Today's morning music was "Bulletproof" by Pop Will Eat Itself. Cram that in your head @ryanbriones Ryan was complaining yesterday that he had the theme from flash stuck in his head all day because of me. That sounds awesome! I had a Daft Punk song stuck in my head all day today and I was a better person for it. Trying to work on my Lightning Talk. Lightning talks are usually the best part of any conf so it better be good. 4:25 today #railsconf Lightning talk is finished. Title: "ActiveMQ and ActiveMessaging: I've Experienced the Pain So You Don't Have To" #railsconf Yeah, I got up, worked out, ate eggs wrapped in cheese and bacon, showered and then realized I had gotten up an hour early by mistake. Opportunity! So I worked on the lightning talk. To be honest, I was kinda pissed at myself for the boneheaded alarm mishandling but if it doesn't ...

Iteration Zero

I spent the week doing an iteration zero for new project. The idea behind iteration zero is to get the development environment as automated as possible so developers can spend their time coding when the real development begins. So here's what we did: 1. Set up new git repository We use GitHub so that only took a few minutes. 2. Make new Rails app Again, just a few minutes 3. Set up geminstaller and basic gems (HAML etc.) Geminstaller installs gems needed by the project and makes sure they are there before starting the app. This functionality has been pulled into modern versions of Rails now, but I've found it a little wonky and I'm working with a former Pivotal Labs guy who just loves the Pivotal stuff (don't even get him started on Tracker vs Mingle ). 4. Set up developer tests. We looked into Shoulda , but ultimately went with RSpec (although we may use the Shoulda macros to test ActiveRecord) because most of the criticisms of RSpec are th...

Finding a Rails Route from a Path

Fred Polgardy and I were looking up how to find out what path in the browser corresponds to what controller/action combination in a Rails application today and I realized that I've looked this up before... So I'm putting this information here so I can find it in the future: (from inside the console) >> ActionController::Routing::Routes.recognize_path '/seo/hotness/path/705105' => {:controller=>"products", :action=>"show", :id=>"705105"} Yeah I know you can do a 'rake routes' and hunt around but this gets me where I need to be quicker.

Delete Like a Crazed Madman

Lately I've been working with this guy Felix who likes to rip the application into shreds when confronted with a problem. Initially, it's a bit scary. I know we have source control and we can get it all back, but ripping out large crucial swaths of code just seems so violent and yet it's a pretty effective strategy. On this one project we've had a problem for well over a year of our tests not rolling back the database after each test. We've got all the configuration set up for that to happen, but it just doesn't and nobody could figure out why. One of the problems is that this is a Rails application that is 2.5ish years old and serves millions of users every day. So in order to make that happen back in the early days of Rails we had to extend and modify a lot of the Rails framework. Apparently one of those modification broke the mechanism for rolling back the database after every test. Not rolling back the database means that one tests creation/updating of...

Easy Charging of Credit Cards with Spreedly

When we built SQuiD (easy project management for link building), we had a problem in that the client was on a limited budget and yet the site had to charge people subscriptions for usage. Which, if we wrote that part ourselves, would take all sorts of time to research, implement, and (most importantly) not screw up. When you take people's credit card numbers there is no such thing as a small security breach. Luckily my teammate, Joseph Leddy , remembered hearing about Spreedly -- all they do is "Make Selling Subscriptions Easy." We decided to use Spreedly and it saved us a ton of time (and consequently money). Here's 9 reasons why I loved working with Spreedly: 1. Free to start and test with! Spreedly doesn't charge any money to sign up and run tests against their API. You start paying only as soon as you're ready to start collecting real money. 2. Ridiculously test friendly You can create a pretend (test) area that you can charge accounts to. It looks...

I'll be Speaking at Rails Conf

Just a quick note to let you all know that Rails Conf accepted my talk. It's called "Using metric_fu to Make Your Rails Code Better" and you can find more details here . If you'd like to join me in Vegas for 15% off you can use this nifty code: RC09FOS Hope to see you there.

Metric Fu Now Includes Flay, Roodi, and Reek

I released metric_fu 0.9.0 last night and just look at all the super cool changes since 0.8.0: Flay task finds duplication and structural similarities in your project Reek identifies common code smells Roodi finds mistakes and troublesome code A configuration class so you can configure like the big boys Standardization of the reports look and feel Increased RSpec coverage Source control type is auto-detected for Churn task Under the hood refactoring to make adding new metrics reports easy Of course it still does all the old stuff like 2 different types of complexity reports, code churn analysis, code coverage, and good old Rails stats. Set up a Cruise Control task to generate metrics reports every day and then you pinpoint the exact moment your project went to hell (or possibly avoid that fate -- your call). Check out the project home page at: http://metric-fu.rubyforge.org/ The best part about this whole thing is that almost everything added since release 0.8 has been added by the...

Sprinkle Some Integration Tests into a Mock Heavy Test Suite

I like mocks and stubs and I use them liberally in my tests. This practice, however, is not without danger: If my models, views, and controllers are all tested in isolation then how will I know if a model change busts a controller or a view? Here's something I like to drop in at the end of every controller spec: 1 2 3 4 5 6 7 8 9 10 11 12 describe JerkStoreController , " with views integrated and real objects " do integrate_views it " should render the page without blowing up " do store = JerkStore .create!( :name => " Costanza's house of bargins " ) store.products << Jerk .create!( :name => " Puddy " ) << Jerk .create!( :name => " Newman " ) << Jerk .create!( :name => " Steinbrenner " ) post :show , :id => store.id end end Not a mock or a stub as far as the eye can see and I'm even integrating views. No...

Rails Rumble, Apprenticeship, and a Running Website

Image
Apprenticeship is a big part of Obtiva (the consultancy where I work) so I introduced my friend Leah to Obtiva, they hit it off, and now she's an apprentice in the studio. Now apprenticeship at Obtiva is a paid, billable position. She's pairing everyday on a medium sized Rails business skill development project and learning a lot, but what just blew me away was the development of the "Run. Track. Run." project. Leah is a runner and for years she's been talking about how she'd like to create a decent running website that could import Gamin GPS data and produce useful graphs for runners. Apparently all the sites out there plot every single point of data returned from the GPS device, with little-to-no smoothing, and so the graphs are extremely noisy and hard to read. Wouldn't it be great if an actual marathon runner designed a website for runners? At which point I would usually say: "Yeah, that's a great idea -- we should do that someday....

If you use Mocha and RSpec then read this

I like RSpec, but I'm not a huge fan of it's built in mocking framework. So, when I have the choice, I swap it out for Mocha. However, I really miss mock_model. If you haven't used it, mock_model is an RSpec method where you pass in an ActiveRecord object and it stubs out a whole bunch AR magic so you don't have to. This is crazy useful when testing controllers because when you controller has a line like this: redirect_to(@model) I don't want to dig through a ton of Rails code to figure out what I need to stub on this model, I just want it to work. But I just found out that Mislav Marohnić has written a plugin that implements mock_model for Mocha -- so now I can get the best of both worlds. You can find it here: http://github.com/mislav/rspec-rails-mocha Thanks Mislav.

Naked Hashes are Trouble

So I had this form in a Rails view that needed some changes so, amongst other things, I changed this: form_tag(:action => 'search') to this: form_tag(:action => 'show', :method => :get) I had noticed that the show and search methods essentially did the same thing (and had the same views) -- which I why I refactored. However, when I made the change I got a 500. I was using restful routes and this form needed to be a get -- which I thought I had indicated. But I had not -- what should have put in the view was this: form_tag({:action => 'show'}, {:method => :get}) There's this idiom in Ruby where we don't put curly braces around hashes if we don't need to, but I've consistently lost a bunch of time to mistakes like the one above. So I'm thinking about explicitly denoting hashes with curly braces in my future Ruby code just to improve intentionality.

Windy City Rails Part Two

Yesterday , I talked about the first half of the first ever Windy City Rails conference that happened on Saturday at IIT. Tonight, I'll pick up where I left off with my summary of the presentations I attended. Virtualization and Elastic Servers -- Yan Pritzker of CohesiveFT The gist of this talk is that the Rails stack is not well known so why not let CohesiveFT handle deployment for you. It's a virtualization factory, that lets you put together a custom Rails stack and they can deploy and maintain it for you for as little as 10-15 bucks a month. Also, he pointed out that virtualization is a solution for your front end guys getting a working version. Which, having tried to get many a design person's computer up and running, I think is a pretty cool idea. Ten Things I Hate About Web Apps by Micah Martin Short list of things Micah hates about Web Apps: You need to know css, html, js, and the actual programing language just to get started. Html -- only 3 fonts to chose fr...

Windy City Rails Part One

This weekend was the first ever Windy City Rails Conference and I was lucky enough to not only attend but also to speak. I thought I'd sum up the talks I attended in case anyone was interested. After Ray Hightower, lead organizer of the conference, welcomed everyone Ryan Platte gave the first talk of the day: "Outside the sweet spot" Ryan's talk was about getting Ruby and or Rails into a large organization which is committed to things like: Windows, Oracle, etc. I.E. not the typical Rails stack. In Ryan's experience, Rails makes it's way into such an organization the following way: Some guy picks up a Rails book and says "Hey, this is way better than the excel spreadsheet we've been using." Then he talks one boss into the project, and maybe they hire some consultants to help. However the rest of the organization has issues with Rails (it's slow, not compatible with their stack). Ryan's advice is: Do top quality work quickly -- get ...

Metric Fu is Now a Gem

Image
I just released version 0.7.6 of metric_fu and there's all sorts of new stuff: First, MetricFu is now a Ruby gem on GitHub at: http://github.com/jscruggs/metric_fu Also, the Flog task can now flog any set of directories you like. Just put this into your Rakefile: MetricFu::DIRECTORIES_TO_FLOG = ['cms/app', 'cms/lib'] And, the Flog reports are much nicer: The Flog report generator now looks at a MD5 hash of the files its examining to determine if they files need to be re-Flogged -- which saves time. The Saikuro report generator can also look at custom directories like so: MetricFu::SAIKURO_OPTIONS = {"--input_directory" => '"cms/app | cms/lib"'} MetricFu started its life as a Rails Plugin, but I'm trying to turn it into a flexible gem that can be used in any Ruby application (but still have defaults that enable easy use in a Rails project). Eventually I'd like to create something that could tell you about methods that have hig...

Lone Star Ruby Conf Second Day (morning edition)

While checking out at the hotel I ran into Coby from confreaks.com They are recording the conference so you'll be able to see the talks online sometime soon. They're not cheap, but their videos are very good (they capture the output of the presenters computer and display it side by side with video of the presentation -- you seriously need to check out their site), and they offer a discount if the conference is willing to CreativeCommons license the talks. Starting off the morning was "Ruby: A Year of Innovation" with Gregg Pollack & Jason Seifer. They had a lot to cover so it went pretty fast. I tried to keep up as best I could. Jason Seifer was up first: HPricot - awesome for scraping websites Juggernaut - server push with rails -- keeps a connection open Ambition - write your SQL statements in Ruby Prawn - Ruby based PDF generation Capistrano Ruby VMs roo - spreadsheets Excel, google docs Dtrace and Ruby -- memory stack live Skynet - map reduce for Ruby Data...

Lone Star Ruby Conf First Day

(or the second day if you count the tutorials) To start things off Jim Freeze got up and announced that there are 282 attendees and seats for 280 -- So make friends. The the first talk of the day was "The Next Ruby" by Bruce Williams and he (of course) discussed the difference between Ruby 1.8.6 and 1.9 Bruce recommended a good test suite if you're planning to move to 1.9 and I have to agree. Tests are good for lots of reasons (including design) but they pay for themselves 100 times over when you need to do a big tech migration. Because of changes to string, anything string heavy (such as parsers) is gonna need some love. On the plus side: "ruby"[0] => 114 in Ruby 1.8.6 "ruby"[0] => "r" in Ruby 1.9 Yep, I've been burned by that and I'm glad to see sanity carry the day. In other news: Hash.select now returns a hash Hashes will maintain order :this === "this" #true Because of Multinationalization Strings now have each...

I'm Thinking of Putting View Logic into a Model

As I am clearly crazy, but hear me out. Say there's a Car object that uses single table inheritance and all the objects that descended from it are routed through the car method in the CarController. The CarController looks like this: 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 30 31 32 33 class CarController < ApplicationController def car layout = nil @scripts = [] @stylesheets = [] @car = Car .find(params[ :id ]) if @car .is_a?( Toyota ) layout = ' two_column ' @stylesheets << ' two_column_layout.css ' @stylesheets << ' two_column_theme.css ' elsif @car .is_a?( Hyundai ) layout = ' three_column ' @stylesheets << ' three_column_layout.css ' @stylesheets << ' three_column_theme.css ' @stylesheets << ' hyundai_theme.css ' @scripts << ' discount.js ' elsif ...

Disconnecting RSpec from the Database

Recently I spent some time away from RSpec and I did miss it, but one thing especially liked about the testing framework I lived in for 10 months was that it had two ways to test anything you liked: One that hit the database and one that did not. We used UnitRecord to disconnect unit tests from the database (I should mention that we defined unit tests as tests that didn't hit the db and functional tests as those that did). So the vast majority of our tests didn't really need to hit the db and ran very fast. Sometimes we wanted or needed to hit the db and we did (only occasionally in model tests, more often in controller tests, and we kept logic out of the view and only tested views at the Selenium level). It was pretty cool and I'd like to thank the team who set it up before I got there. There's been a lot of talk about whether model, view, or controller tests should hit the db but I've found that there's enough exceptions that it's worth just saying that...