Posted on June 24, 2007
I’ve just been learning how incredibly powerful Test First Development is in Rails – being able to test for elements on the dom using assert_select is fantastico!
However I note that there’s no opposite – a deny_select if you will. Something that will test to see if a given element does NOT exist.
Here’s one I cooked up earlier:
def deny_select(element, message)
assert_select element, false, message
end
Use it by passing in the css element id and the message if the assertion fails like this:
deny_select 'table.calendar', "Calendar should not be on page"
Filed under: Ruby on Rails |
Tagged with: testing |
Posted on June 20, 2007
Something you can’t seem to do in RJS is to do conditional checking with the client side DOM.
For example, I want to be able to investigate an element to see if it has the CSS display tag set to none. The solution is to blend javascript and RJS together.
To do this you append the javascript as a string to the page object like this:
# Javascript check to see if the element is visible
page << "if ($('div_element').style.display == 'none') {"
# Wow - this is like being in a PROPER if statement!
# RJS stuff to make the element visible
page.replace_html 'div_element', :partial => 'some_partial'
page['div_element'].visual_effect :blind_down
# back to javascript
page << '}'
Thanks to Rails Casts for that particular little tidbit!
Filed under: Ruby on Rails |
Posted on June 13, 2007
The Elements of Style is a fantastic little book detailing best practices for writing.
It's written as a series of rules about the best way of writing clear, concise elegant writing.
Subsequently, there's a series of programming language books that do a similar thing. One on my bookshelf is Baldwin, Gray and Misfeldts
The Elements of C# Style that provides style guidelines for C#.
For example:
Element 73: Choose Simplicity over Elegance
...there is nothing particularly elegant about a 50 line sequence of if-else statements, but if it's the simplest most straightforward approach to the problem, there is no reason to turn it into something it does not need to be.
And other tasty tid-bits.
If there's ever a language that's just CRYING out for a definitive style guide, it's Ruby. Rubyists are such purists about style, simplicity, DRY etc - but there doesn't seem to be (at least not to my eye) a list from on high as to best practices for style issues.
That was until I found Ian Macdonalds
Unoffical Ruby Usage Guide that seems to be a pretty exhaustive study into Ruby best practices.
For example:
We recommend two blank lines between each class and module definition, and a single blank line between each method definition
Hooray!
oo - I also see Jeremy McAnally has created a project to do just this. Find it at:
Elements of Ruby Style
Filed under: Ruby on Rails |