Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5552 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Super Cool RJS If, elsif, else and unless blocks (with tests)

Reported by: martin.emde@gmail.com Assigned to: sam@conio.net
Priority: normal Milestone:
Component: ActionPack Version:
Severity: normal Keywords: rjs if else elsif
Cc:

Description

If you're like me, you tried to do this:

if(Element.visible('free_beer'))
  page.hide('free_beer')
end

because we (of course) want to hide our free beer as quickly as possible if it's visible. You may not have gone as far as I did (actually running the request and being sad) but I'm sure you realized there's no really good way to put if statements cleanly into RJS.

Finally, here come the RJS-if patch (which means more free beer)

Simply add a page. in front of that if and put quotes around the conditions and you got yourself some real, outputting, javascript ifs.

page.if("Element.visible('free_beer')") {
  page.hide('free_beer')
}.elsif("Element.visible('free_accordians')") {
  page.visual_effect(:highlight, 'free_accordians')
}.else {
  page['header'].replace_html "sorry, nothing free today"
}

Now you can keep your beer, get rid of your accordians, and do it all cleanly. The syntax is similar to javascript ifs, else if, and else. The only change is the . after the closing '}' to call the next method (elsif or else).

This patch comes complete with tests that proove it will protect your beer.

Ok, I like beer, but how's it work?

It works by adding an if and unless method to the JavaScriptGenerator::GeneratorMethods module.

I also added a class called ConditionChain to JavaScriptGenerator::GeneratorMethods that is explicitly for return from an if, unless, or elsif block. This way we can control what methods are available to chain onto an if. Only elsif and else may be added on to an if, elsif, or unless statement which makes sure that we're writing good code (and get a reasonable error instead of just outputting weird javascript if we happen to be dumb)

Attachments

rjs_if_elsif_and_else_blocks.diff (5.1 kB) - added by martin on 06/29/06 18:21:51.
RJS if, elsif, else, and unless (with tests)

Change History

06/29/06 18:21:51 changed by martin

  • attachment rjs_if_elsif_and_else_blocks.diff added.

RJS if, elsif, else, and unless (with tests)

06/29/06 19:51:20 changed by anonymous

  • summary changed from [PATCH] Super Cool RJS If, elsif, else and unless blocks (that actually work) to [PATCH] Super Cool RJS If, elsif, else and unless blocks (with tests).

07/05/06 01:16:11 changed by david

  • owner changed from David to sam@conio.net.

09/04/06 18:02:01 changed by david

  • status changed from new to closed.
  • resolution set to wontfix.

This is definitely an interesting idea, but I think it crosses the border of what feels like a good and natural fit for RJS. We're not trying to completely reimplement JavaScript in Ruby. Just enough to make it feel not-painful. You're of course more than welcome to turn this into a plugin for those who do like the idea of full emulation.