tag:blogger.com,1999:blog-15027610931964315122009-06-26T09:24:03.205+01:00Ruby-coloured glassesTech blog of a rails contractor.Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.comBlogger126125tag:blogger.com,1999:blog-1502761093196431512.post-47268151331757366222009-06-11T10:37:00.003+01:002009-06-11T10:48:18.638+01:00On the premature death of Array.to_ahmm... after finally discovering that Array.to_a doesn't double-array now I'm getting warnings that Array.to_a is being obsoleted (with the unhelpful message: warning: default `to_a' will be obsolete). Aunty google tells me I should now use Array(thing)! Can I just say (from a purely aesthetic POV): yuk! When you use to_a you can just read your code left-to-right eg: Thing.do_stuff(args).Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com5tag:blogger.com,1999:blog-1502761093196431512.post-72168243401161490862009-06-09T10:16:00.001+01:002009-06-10T10:17:53.982+01:00acts_as_good_styleOriginally published in 2007, this article is updated on a semi-regular basis and republished when I add something new... Rails Idiom There are a number of small pieces of rails idiom that contribute to better rails code. I've been collecting them at work as they come to me (often by reading other people's code). I know that a lot of this can be gleaned simply by reading the "Agile rails" book..Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com5tag:blogger.com,1999:blog-1502761093196431512.post-71994681393130889872009-06-06T10:18:00.002+01:002009-06-06T10:18:00.747+01:00dynamic_parametersThe params hash in Rails is special. It contains not only the query/post parameters that the user asked for, but also a set of special parameters such as controller and action... but there are times when we want to know *just* what the user has *actually* asked for (without Railsy interference, however helpful). I was quite surprised to find that there isn't a function in Rails that actually Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-82510432380170315212009-06-05T22:33:00.000+01:002009-06-05T10:33:52.646+01:00arrayify!Following from my post: Array.acts_like_array?, I can now implement arrayify without using is_a? Array or respond_to? :[] && !is_a? String class Object def arrayify self.acts_like?(:array) ? self : [self] end end So we can now do: thing = thing.arrayify or: do_something(thing.arrayify) Now, what I really want to do is thing.arrayify!, but I'm not sure how to implement that. Any Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com6tag:blogger.com,1999:blog-1502761093196431512.post-21390116957130185122009-06-03T12:09:00.003+01:002009-06-03T12:12:35.315+01:00Array.acts_like_array?I've recently discovered the joys of: acts_like?, and was astounded that there hadn't been a simple extension to Array. So here's the monkeypatch: # extension to allow us to check if an array is an array... # This feeds the Object.acts_like? method class Array def acts_like_array? true end end Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-15336828533560750462009-06-01T10:01:00.005+01:002009-06-01T10:21:05.425+01:00Help? ActiveResource common configSo we have a system that communicates with a "local" API - ie all the objects in our system are on another database, which we access via an API[1]. A very large number of our objects are accessed using HyperactiveResource[2]. Because so many of our resources are hosted on the same system - it makes sense to put the common access-information (eg "site", "user" and "password") into a Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-81210228608041830462009-05-27T10:38:00.001+01:002009-05-27T10:39:54.227+01:00Link: Designing For Sign UpThe sign-up process is much more than just the form that gets a potential customer's details. This presentation explains how you can design your signup process to get customers motivated and rearing to go on your site.Designing For Sign UpView more Microsoft Word documents from Joshua Porter.Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-85118369550406546012009-05-21T11:25:00.006+01:002009-05-22T10:06:39.913+01:00Testing gotchas: nested logins don't work*sigh* after a couple of hours futile hacking about I finally realised that my controller tests were failing because login_as cannot be nested. ie. if you have a context with a login_as @user1 in the setup... then put a context inside it where you login_as @user2... it won't actually do the second login. So just move that nested context outside. Simples neh?Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com2tag:blogger.com,1999:blog-1502761093196431512.post-75542847504220001592009-04-30T13:21:00.005+01:002009-04-30T13:30:40.725+01:00Should have form fields (macro)Quickie snippet form-macro to make sure that the page you're on has the given set of form fields. You can optionally pass in a model name for "form_for" style forms (because these are pretty common), but it also lets you just pass in straight field names (for when you check your submits and hand-crafted tags). Stick this at the bottom of your test_helper.rb and use as below: Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-22115770561668926932009-04-30T13:08:00.004+01:002009-04-30T13:14:36.212+01:00Dehumanizing RailsSo we have a string that has been "humanized" - and all the underscores have been stripped out and replaced by spaces, and it's been nicely capitalised (sorry, make that "capitalize"d). So how do we go back the other way? Plonk the following into config/initializers/inflections.rb (or if you're using a legacy Rails, drop it onto the end of config/environments.rb). module ActiveSupport::Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com2tag:blogger.com,1999:blog-1502761093196431512.post-65394483769072724242009-04-28T10:41:00.014+01:002009-04-28T15:56:10.051+01:00Playing nice with XML and HTTPSo we've got a setup that has a remote API that we're accessing using HyperactiveResource (an extended version of ActiveResource). Now, I'm using Rails to simulate the remote API (for the purposes of testing), and I've come across some annoying behaviour. One issue is that standard rails routing for a RESTful interface will direct a badly-constructed (or non-existent) URL to a real action... letTarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-33906820136469702752009-04-27T12:13:00.001+01:002009-04-27T12:54:30.398+01:00Testing ActiveResourceI've been banging my head against the wall that is ActiveResource for a while now. One big problem is actually getting the testing correct. Take 1: HTTPMock The supposedly sanctioned expectation is that you test it against the provided HTTPmock... unfortunately, this is great for testing that ActiveResource makes the correct remote calls eg, when you update a user that ARes POSTs to /users/123Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com2tag:blogger.com,1999:blog-1502761093196431512.post-2496793732980798122009-04-22T01:33:00.000+01:002009-04-22T10:33:46.660+01:00should_set_the_flash bettershould_set_the_flash_to doesn't do quite what I'd like. I want to: a) specify which level of flash should be set (ie make sure it's a notice and not an error) and b) not care exactly what notice has been set. IMO tying your tests down too hard to ephemeral strings is annoying... what is somebody changed the wording from "user created" to "thanks for signing up"? You have to change your test Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-90176794189254791082009-04-16T10:45:00.002+01:002009-04-16T11:38:03.148+01:00rails gotchas: nested transactionsSo I've been adding some new plugins for testing that I've never tried before, in this case: shoulda and stump, and found that stump just isn't playing nice with SQLite :P The problem only occurred when I tried to declare a proxy! function on an existing model. ok, I didn't get around to trying everything, so maybe it occurs on other things too, but it seemed to be happy with stub and mock. TheTarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-61317235848818516722009-04-09T09:54:00.006+01:002009-04-10T20:54:21.881+01:00Rails gotchas: shoulda not_allow_values_forIf you're using should_not_allow_values_for and getting a failing test something in the lines of: Failure: test: User should not allow email to be set to "b lah". (UserTest) [/usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/assertions.rb:56:in `assert_rejects' /usr/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/active_record/macros.rb:174:in `__bind_Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-69025686985580472202009-04-08T16:48:00.007+01:002009-04-09T09:38:16.418+01:00You shoulda test your plugin!So, lets assume you have a plugin "acts_as_teapot" that you want to include in some of your model classes. You've written a number of useful methods for teapot-like models to use, and now want to check that the models that are implementing acts_as_teapot actually are able to make use of the full spectrum of teapotly functionality. You could write a set of asserts that you can tell your plugin's Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-67565991982027042342009-04-08T16:23:00.004+01:002009-04-08T16:30:42.713+01:00rails gotchas: assert_raises a syntax errorI found that assert_raise was causing a syntax error of the form: syntax error, unexpected '{', expecting kEND (SyntaxError) For a fairly simple test: should "remove it from the database" do assert_raise ActiveRecord::RecordNotFound { User.find(@uid)} end Looks like it's getting confused about nesting. the solution? parenthesise your Error thus: should "remove it from the database" do Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-40652191694543007322009-04-06T08:45:00.000+01:002009-04-07T17:40:03.110+01:00rails gotchas: restful_authentication not 2.0 compliant?restful_authentication[1] is a great plugin, but the standard svn version is showing it's age. It's a whole month or so out of date... which is, of course, an eternity in the fast-paced world of Rails. Luckily it's actually just moved home. You can find the up-to-date version on github [1] Formerly known as acts_as_authenticatedTarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-88602290483705938282009-04-06T06:30:00.000+01:002009-04-07T17:40:26.122+01:00rails gotchas: HttpMock not enough variablesA quickie for my own remembrance. I'm currently setting up HttpMock to test ActiveResource. I'd set up a few extra "routes" for it to mock out and kept getting the error below: NoMethodError: undefined method `size' for :not_found: Symbol For the code line: mock.get "/users/#{uid}.xml", {}, :not_found The fix was pretty simple - I'd just accidentally missed out the "nil" for the body - ie it Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-69102286271183938912009-04-02T13:40:00.010+01:002009-04-03T16:11:33.042+01:00ActiveRecord::Validations in ActiveResourceThe holy grail for ActiveResource users is for ARes to actually behave like ActiveRecord. In theory, ARes is just like AR, but in practice it's only kinda, sorta like AR... but missing a few bits that really seem to make all the difference. ARes is still missing fundamental functionality that we have all grown to know and love... It all looks alright on the surface, but you can't help but noticeTarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com2tag:blogger.com,1999:blog-1502761093196431512.post-87028171673063785922009-04-02T11:06:00.006+01:002009-04-02T11:17:35.037+01:00git gotchas: the wrong forking repositorySo I'm pretty new to git, really, and am learning all the places that I can stumble and fall head-over-arse. Today's escapade is entitled "how to recover from forking the wrong repository in github". It's quite a simple recovery, and you'd think it'd be obvious how to recover from it... you'd also think that github would have a "how to" in the main help page... This solution is based on having Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-55896047659427595462009-03-31T10:08:00.009+01:002009-03-31T10:59:08.919+01:00State of authenticationWe're using restful_authentication for login - because it's a good standard set of tools. However, we didn't want activation for our system, as it's just a slow-down on the path to user kick-ass-ness, and we wanted out users up and running with a minimum of fuss. However what we *did* want, was statefulness (so we could suspend users). When I ran the usual generator without the --Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com2tag:blogger.com,1999:blog-1502761093196431512.post-7095122670020255152009-03-30T12:25:00.006+01:002009-03-30T15:44:53.902+01:00acts_as_state_machine 2.0I spun up a new rails project and downloaded my favourite authentication plugin restful_authentication, only to find that all out-of-the-box tests break (oh noez!) Clearly, I thought, restful_authentication is now just a millisecond behind edge rails... and therefore doesn't work properly anymore. I quickly realised that no, it's just that the old-standard install (via svn) is no longer Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-11077674834394340082009-03-29T19:46:00.000+01:002009-03-30T09:53:51.164+01:00I just want a toaster...I was buying all my basic housey stuff at Tesco today (it's *that* kind of supermarket/superstore), but feeling overwhelmed... Being in a foreign country means all the brands mean nothing to you, so you don't know which ones do what, and you find there are many brands that do things you've never even heard of. So you have two options. You can either pick things at random and hope you aren't Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com0tag:blogger.com,1999:blog-1502761093196431512.post-87988409095452747942009-02-07T09:15:00.005Z2009-02-07T09:55:21.077ZSome day, my visa will come...So in case you happen not to have actually spoken to me any time in the past two months, I'm in the process of moving to the UK. Of which, the major step is to get myself a UK work visa. I'm (frankly) too old for a holiday working visa (and I don't want to go for that short a time anyway), so what I need is the Tier 1 general migrant visa. This will let me work in the UK for any company for up Tarynhttp://www.blogger.com/profile/00647732421144825421noreply@blogger.com1