wish I could get hold of a chain of named_scopes without performing any query (which means it can’t be dynamically composed) #activerecord

My team has currently been performing some refactorings on our Rails app and one of our goals is to move complex find queries into more composable/meaningful/maintainable named scopes. Contrary to my expectations, when you call a named scope, Active Record performs a find(:all) query implicitly as you finish your statement:

In our particular case, we didn’t even need the proxy object, but just a hash representation of the composed query parameters. We’re using ActiveRecord::Extensions (ar-extensions) to perform some UNION queries to aggregate results, and it expects a list of hashes that represent each query. It would be nice if I could grab the hash from my named scopes without performing the query…

The documentation points to a proxy_options method that you can use to test your named scope, but it didn’t solve my problem, since it only returns the hash for the last scope in your chain:

This is not a perfect solution, but saved me the trouble of performing the merge myself. I hope the Rails 3 solution addresses these issues and that this post saves me some time digging through ActiveRecord source code if I need this before then :)

The new version of Synthesis 0.2.1 was just released and the great news is that my experimental work with visualizations was integrated into the master branch! Just sudo gem update synthesis to grab the latest version.

Why Visualizations?

“…reducing the volume and increasing the value of test code by connecting the dots.”

As George pointed out in his first post about Synthesis, it helps you to connect the dots of your tests, to increase your confidence in the system. I like to think of it as a mock coverage tool. When you use a mock in your test, you’re explicitly stating what interaction you expect to happen. How do you really know that the actual object will behave as you expect? You probably need a unit test around it to guarantee it does what it should. Synthesis helps you connect those dots, breaking your build when a mocked expectation is not met in the “real world”.

By using it in a real project, I noticed that the textual output was getting quite verbose. While coding, I was usually only interested in the failure expectations, but when I stopped to look at the whole output, I noticed there were a lot of interesting information hidden in there. If only I could have a better representation of that information…. that’s exactly what software visualization is all about! I had to dig into Synthesis internals to understand how to hook a different report formatter, but the end result ended up being quite good:

Generating Visualizations

To generate such a visualization, first make sure you have sexp_processor installed (sudo gem install sexp_processor) you need to configure your Synthesis rake task to use the dot formatter:

This will create a synthesis.dot file that can be used to generate a Graphviz image. You can download and install Graphviz for your prefered platform (there’s even a Mac OS X version), but all you need is the command line dot tool to generate your visualization. Just execute:

dot -Tpng-o synthesis.png synthesis.dot

This will create a synthesis.png image file that will show your tested expectations in green, and untested expectations in red. You can generate other output files by changing the -T option (try ps, pdf for a higher quality). There are still a lot of things to improve in the formatter and any feedback is welcome, but it proved to show some interesting information in my last project, as you can depict in the following picture (notice that the app is mainly composed of 2 subsystems that don’t share dependencies):

Tip: Running Synthesis with Rails

Recently, when trying to use Synthesis in a Rails project, I had some problems that took me a while to figure out. As always, the solution was quite small, so I thought it would be nice to share it with everyone. To run Synthesis on a Rails project, make sure you add the following lines to your Synthesis rake task:

Synthesis::Task.newdo|t| RAILS_ENV = "test"Rake::Task['environment'].invoke# This may make your build slower. Use only if needed t.pattern = 'test/**/*_test.rb'end

Give Synthesis a try in your project and let us know how it goes. You might be disappointed in the beginning about how many expectations you’re not testing, but even if not all of them really require a test, it is still a very valuable information to have. Let me know if you have any issues with the dot formatter as well, as I’m sure there are a lot of kinks to be sorted out. Feedback is always welcome!

I’ve finally settled down in London and took some time to start the conference reports from the last couple of weeks. The first one I attended in São Paulo was Rails Summit Latin America. It was very well attended (500+ participants) and organized. I should give a special thanks to Fabio Akita and Locaweb for putting such a great conference together!

The first day of the conference started with DHH’s remote Q&A session, which was OK but nothing new or exciting. The following talk was really good: Chad Fowler gave an inspiring keynote about “Being Remarkable” which was a good summary of his book, plus his skills as a presenter and a good deck of slides (and videos). Very good!

Me and George skiped lunch to rehearse and give some final touches on our presentation about REST. I thought the presentation flowed very well, and it ended up being the only technical session on the main stage during the first day, even though we didn’t show any code: our goal was to talk about REST as an architectural style, basically summing up the great work of Fielding and the good practices that people on the REST community have been talking about. We also wanted to talk about other alternatives to building services on the internet. The slides are available here (we added some notes to make it easier to follow our train of thought).

Dr. Nic gave a funny and entertaining presentation about contributing to Open Source projects and the “Path to Awesomeness”. The first day finished with Chris’ keynote, which was very similar to the one he presented at Ruby Hoedown. If you’re interested, watch it on Confreaks. After his talk, Fabio moderated the Birds of a Feather session (which was more like Lightning Talks), and I was very surprised to see a lot of people stepping up to talk in their 5 minutes slot. The topics ranged from politics to organizing a study group, with special kudos to the Phusion guys who showed a Brainf*ck interpreter in Ruby. Of course, the day couldn’t finish without a rodízio of Brazilian steak at the churrascaria.

On the second day, I experienced the great joy of São Paulo traffic and didn’t made it to watch the Phusion guys’ presentation. I’ve heard from a lot of people it was great and that they gave a great talk about scalability (as well as a lesson on how to use Keynote properly). I arrived during Charles Nutter and Thomas Enebo’s talk about JRuby. It was held remotely and I know Akita had a lot of work to get the environment working. In the end, we still experienced some latency issues but the demos worked out quite OK. After that, Jay gave an interesting talk about the “Immaturity of Testing”, which was rooted in a last-minute presentation we put together for the UK ThoughtWorks Away Day back in June with George. It was great to see how much the discussions from our internal session enhanced the overall message and how Jay managed to put it all together in a nice format. I think a lot of people learned from the pros and cons that he discussed.

After lunch, the ‘testing’ theme continued with David Chelimsky presenting a double talk on RSpec and Cucumber, the gem that’s being developed by Aslak right now and will eventually replace RSpec’s current Story Runner. I wasn’t present during the second half, because I was preparing to present next about my lessons learned about testing. The slides (in Portuguese) are also available and I received some good feedback about it from the brazilians who prefered to watch me instead of David :-)

I stayed around the Brazilian track to watch my friend Fabio Kung present on JRuby and give a real-life demo of his experiments with JMagLev. After that, we all went to watch the closing keynote, by Obie Fernandez. It was a great talk about the Hashrocket way, which was heavily based on their way of applying Agile. In the end, he managed to get standing ovation from some folks in the audience. The last day finished with a lot of champagne and free beer, closing two days of networking, fun, Ruby, and Rails.

My overall impressions from the conference were very positive. It was great to see how much the Brazilian Rails community has grown over the past year. When Fabio and I organized the first RejectConf, back in 2007, we managed to get 100 people to show up in a Saturday and that triggered a lot of other small local conferences throughout Brazil. Seing everyone together for the first time, was a great chance to do the usual networking, and to share our experiences working with Ruby and Rails. I suspect next year’s conference will be even better. See you there!

Next 15th and 16th of October I will be in São Paulo speaking at Rails Summit Latin America. I will be presenting a session about Developer Testing, showing some interesting tools in the Ruby world such as: RSpec, Mocha, Synthesis, RCov, and Autotest. I will also be co-presenting a session with George about Application Design for REST (or something along those lines).

In the meantime, for the Portuguese speaking audience, I have uploaded the slides and Carlos Eduardo made the recorded video available of my Merb webinar that I presented last month at “Café com Tom”. I will be back to present another webinar about BDD and RSpec in September.