Archive

Archive for the ‘programming’ Category

Rails Functional Testing of mime types other than HTML (XML, JSON etc)

December 16th, 2008 Dante Regis 2 comments

I noticed something interesting these days. Even though most Rails websites boasts the benefits of Test Driven Development, most examples they give us don’t test the whole app functionality. 

How come? Well, REST was the word of the day, it is now deep inside our brains, right? But have you ever seen an example test of the XML output of Rails? Surely not. Every guide, tutorial and example shows only how to test for the default output, which is HTML. 

So what? That ought to be easy, right? Just add :format after the get, post, put or delete function and you will be fine, you must think. Well, I have been working on a project and had to test if the Atom Feed would work ok. I must say it was one of the most frustrating tests I have ever written. 

Turns out that, even though you can do something like this:


formatted_posts_url(:format => :atom)

you CAN’T do that on testes:

get :index, :format => :atom

 

You know why? Because, even though you CAN use symbols (:atom) on your code, you CAN’T use on tests FOR THE FORMAT. Let’s rephrase it: You can use symbols on tests when specifying the action, the parameters and everything else BUT NOT THE FORMAT of the request. So the code above will give you a mysterious HTTP 406 Error Code.

So, to prevent you from loosing a full day where you felt productive, like I did, the following code will work:

get :index, :format => "atom"

Best!

Categories: programming, rails Tags:

Factory Girl and has_many / has_many :through associations

November 19th, 2008 Dante Regis 7 comments

If you want to create has_many and has_many through associations in factory_girl as I once did, you will need to put it inside square brackets:

Factory.define :user do |user|
  user.name "My Name"
  user.groups {|groups| [groups.association(:group)]}
end

This will ensure that user.groups is not overriden by a single Group object, but by an array with a Group inside.

Categories: programming, rails Tags:

Console input on Ruby: Masking passwords on the command line

November 19th, 2007 Dante Regis 2 comments

I like plain old console applications. They are fast (to program, compile and use) and, of course, add a cool factor, like people saying they have absolutely no idea of what you are doing!

Ruby, of course, can do console apps, but it’s not trivial to read user input. And what if you want the user to type a password? You would like, of course, to show them asteriks ‘*’ or nothing at all instead of the plain text being typed.

Well, “highline” gem covers this and much more. What about writing:

ask("Enter your password:  " ) { |q| q.echo = "x" }

And that’s all! That’s quite cool, man. It can even check if the answer matches some regex, or split it by commas, or anything you want
Take a look at it: http://highline.rubyforge.org/

Categories: programming, ruby Tags:

Formatting dates nicely in Rails

August 16th, 2007 Dante Regis No comments

I always thought those “today”, “yesterday”, “4 days ago” date references on some websites way cool. On a small system I’m working on I did some code to do something alike, though in portuguese. But if you want it in english, there’s a small plugin called relative_time_helpers that you can install by typing:

 script/plugin install http://ar-code.svn.engineyard.com/plugins/relative_time_helpers/

Then, you can do things like

<%= relative_time(Time.now) %>
# today

<%= relative_time(1.day.ago) %><
# yesterday

<%= relative_time(1.day.from_now) %>
# tomorrow

<%= relative_time_span([Time.now, 5.days.from_now]) %># May 17th - 22nd

This tip was taken from Active Reload Blog!

Categories: programming, rails, ruby Tags:

ruby on rails reference cheat sheet

March 7th, 2007 Dante Regis No comments

This website has an excelent Ruby on Rails resource for those in need (like most of us). It features lot’s of command line options, code snippets, and stuff like that. Very handfull! This is the link to the blog entry, and this one points to the text itself. Take a look!

Categories: programming, rails, ruby Tags:

ruby on rails ide comparison

March 7th, 2007 Dante Regis No comments

Sébastien from The Nameless One, did a very impressive job comparing three of the main Ruby on Rails IDE avaiable. His extensive post includes a feature chart with each IDE’s most important features side-by-side, using colors to show how good or bad implemented that feature is on a particular IDE. It’s more than worth looking, and the link is here.

BTW, I’ll stay with Netbeans Development Edition. Even being unstable (I believe it’s not even Alpha yet) it’s quite usable, and I’m doing most of my work on it. Just missing a few test features. But that should come on the next milestones!

Thanks Sébastien, for the great job!

Categories: netbeans, programming, rails, ruby Tags:

making webpages with great colors

February 27th, 2007 Dante Regis No comments

I’m just a programmer. Period. I work alone, so I have to build both code and web page layout for my software, but I’m no designer. To top it all, I’m color blind. So, I choose the colors, they look great to me, but everyone else with a normal vision will protect their eyes from such a terrible color scheme!

Maybe you’re not a color blind person, but you may have faced trouble choosing colors for your web site. You try two, three, ten times and it just can’t get right. Your designer friend though, can do that in 35 seconds, of which 30 are spent opening Dreamweaver.

There’s a tool on the web that intents to help people like us. It’s called Color Scheme Online v 2.0.  It helped me big time. You go to their website, and click on a color (or type the color code on one of the inputs on the left). The site will then show you 12 colors that match with the one you provided. Along with their codes in RGB and Hex.

Great value tool! Try it out.  There’s also a desktop software download there, but I haven’t tried that out yet.

Categories: programming, web Tags:

netbeans and UTF8 encoding

February 26th, 2007 Dante Regis 11 comments

[digg=http://www.digg.com/programming/Netbeans_and_UTF8_encoding]
When I started using Netbeans as my Ruby on Rails IDE, I noticed that, at least on my build, it did not use UTF 8 on files by default. If you speak english that usually is not a problem, but for me, who write software in portuguese, it is very important.
But, Mr. Google gave me a tip, and I’ll place it here so you don’t have to bother him again with this same question.
Theres a file, inside the /etc folder of netbeans directory, called netbeans.conf. I don’t know if it stays elsewhere in Linux, I’ll only know later today, but anyway, you should place this option inside the quotes of netbeans_default_options:
-J-Dfile.encoding=UTF-8
That’s it. Worked perfectly for me.
Best

Categories: netbeans, programming, rails, ruby Tags:

netbeans and ruby on rails

February 26th, 2007 Dante Regis 3 comments

Tor Norbye is doing a great job integrating a Ruby module on the development version of Netbeans. Since I’m still on the look for my definitive Ruby IDE, I decided to take a look at it, and all I can say is that I liked it very, very much. Never had any experience with Netbeans before (I don’t speak Java), but it’s a great software. The Ruby integration is, as far as I can tell, quite good, only a few bugs showed up on 3 days of work.

You can download it from the Netbeans CVS, the standard module. This post from Tor shows how to get and compile it. It took me long 36 hours to checkout the whole source. Don’t know if it was my Internet connection (yes, Telemar, it’s your fault, not mine, DSL provider from hell), or a busy server, but it was long, and I had to restart the process lot’s of times (around 7 times. But you don’t loose your job, it continues from where it stopped).

Give it a try! And, Tor, thank you very much for the great job!

Best!

Categories: programming, rails Tags:

publishing code online

February 25th, 2007 Dante Regis No comments

If you ever needed to publish a few lines of code online, you probably noticed that it is quite difficult to do on the major blogging platforms. I found a good way to workaround this functionality lack, using a third site to host the code, called Pastie. It allows you to post code there, and even split the same post on different sections, meaning whater you want: separate parts of the same file, different files, you name it.

Categories: programming Tags: