Posted by Jim Morris
Sat, 14 Jul 2007 23:40:00 GMT
UPDATED for HAML 2.0 and RSpec 1.1.5 - Changed open to haml_tag, prefix helper. to all rspec calls...
The most recent release of HAML
introduced a neat feature that allows you to use HAML-like syntax in
your helpers to generate HTML
HAML#haml_tag.
A question on the HAML news group asked how to test a helper that uses
HAML#haml_tag (used to be open/puts) and thanks to Nathan on that
list
I finally got RSpec to do it. As shown below.
However a really good point was made that really in RSpec the way to
test anything is to use mocks to mock any call to an outside method
thus focusing the test on the specific module under test.
Generally I agree with that philosophy. But this is way cool so I
thought I'd do it anyway, and also as it is a new feature in HAML one
may not want to simply trust HAML to generate the correct HTML.
So in my application_helper.rb I have a simple helper...
module ApplicationHelper
...
def display_flash
for name in [:notice, :warning, :error]
if flash[name]
haml_tag :div, flash[name], {:class => name.to_s}
end
end
nil
end
...
end
This is called in my views as...
- display_flash
Notice the - instead of =, this is because the open (and puts) write
output directly to the HAML buffer, and so this routine should return
nothing. (This is also a very simply case and does not show off the
utility of the open/puts methods, I'll show one of those later on).
The RSpec helper test that tests this is as follows...
require File.dirname(__FILE__) + '/../spec_helper'
describe ApplicationHelper do
before :each do
helper.extend Haml
helper.extend Haml::Helpers
helper.send :init_haml_helpers
end
it "should display flash" do
for name in [:notice, :warning, :error]
flash[name]= "flash #{name.to_s} message"
helper.capture_haml{
helper.display_flash
}.should =~ /<div class='#{name.to_s}'>\s*#{flash[name]}\s*<\/div>/
flash[name]= nil
end
end
end
Excellent, a simple test for the HTML generated by my haml helper.
NOTE the setup required in the before :each, this sets up the haml helpers in the helpers context
Why would I want to use HAML#open you ask?
Well it makes the helpers look so much tidier IMHO, take this example
from my previous post on
tag clouds,
the re-factored helper now looks like this...
def tag_cloud(model, title= nil)
m= model.to_s.camelcase.constantize
plural= model.to_s.capitalize.pluralize
title ||= plural
tags= m.tag_counts(:order => 'tags.name')
return false if tags.empty?
urlmeth= "tagged_#{model.to_s.pluralize}_path".to_sym
haml_tag :div, {:class => "tagcloud"} do
haml_tag :h3, title
tags.each do |t|
next if t.name == 'FAQ'
haml_tag :span, {:style => "font-size:#{calc_size(t.count)}%"} do
puts link_to(h(t.name), self.send(urlmeth, :tag => t.name))
end
end
end
return true
end
So much cleaner, plus I can return a boolean to indicate if there was
anything output or not, which tells me if I need to output an <hr/> or
not.
Posted in RSpec, Rails, HAML | Tags haml, helpers, rspec, test | 8 comments | no trackbacks
Posted by Jim Morris
Sat, 14 Jul 2007 23:40:00 GMT
UPDATED for HAML 2.0 and RSpec 1.1.5 - Changed open to haml_tag, prefix helper. to all rspec calls...
The most recent release of HAML
introduced a neat feature that allows you to use HAML-like syntax in
your helpers to generate HTML
HAML#haml_tag.
A question on the HAML news group asked how to test a helper that uses
HAML#haml_tag (used to be open/puts) and thanks to Nathan on that
list
I finally got RSpec to do it. As shown below.
However a really good point was made that really in RSpec the way to
test anything is to use mocks to mock any call to an outside method
thus focusing the test on the specific module under test.
Generally I agree with that philosophy. But this is way cool so I
thought I'd do it anyway, and also as it is a new feature in HAML one
may not want to simply trust HAML to generate the correct HTML.
So in my application_helper.rb I have a simple helper...
module ApplicationHelper
...
def display_flash
for name in [:notice, :warning, :error]
if flash[name]
haml_tag :div, flash[name], {:class => name.to_s}
end
end
nil
end
...
end
This is called in my views as...
- display_flash
Notice the - instead of =, this is because the open (and puts) write
output directly to the HAML buffer, and so this routine should return
nothing. (This is also a very simply case and does not show off the
utility of the open/puts methods, I'll show one of those later on).
The RSpec helper test that tests this is as follows...
require File.dirname(__FILE__) + '/../spec_helper'
describe ApplicationHelper do
before :each do
helper.extend Haml
helper.extend Haml::Helpers
helper.send :init_haml_helpers
end
it "should display flash" do
for name in [:notice, :warning, :error]
flash[name]= "flash #{name.to_s} message"
helper.capture_haml{
helper.display_flash
}.should =~ /<div class='#{name.to_s}'>\s*#{flash[name]}\s*<\/div>/
flash[name]= nil
end
end
end
Excellent, a simple test for the HTML generated by my haml helper.
NOTE the setup required in the before :each, this sets up the haml helpers in the helpers context
Why would I want to use HAML#open you ask?
Well it makes the helpers look so much tidier IMHO, take this example
from my previous post on
tag clouds,
the re-factored helper now looks like this...
def tag_cloud(model, title= nil)
m= model.to_s.camelcase.constantize
plural= model.to_s.capitalize.pluralize
title ||= plural
tags= m.tag_counts(:order => 'tags.name')
return false if tags.empty?
urlmeth= "tagged_#{model.to_s.pluralize}_path".to_sym
haml_tag :div, {:class => "tagcloud"} do
haml_tag :h3, title
tags.each do |t|
next if t.name == 'FAQ'
haml_tag :span, {:style => "font-size:#{calc_size(t.count)}%"} do
puts link_to(h(t.name), self.send(urlmeth, :tag => t.name))
end
end
end
return true
end
So much cleaner, plus I can return a boolean to indicate if there was
anything output or not, which tells me if I need to output an <hr/> or
not.
Posted in RSpec, Rails, HAML | Tags haml, helpers, rspec, test | 8 comments | no trackbacks
Posted by Jim Morris
Sat, 02 Dec 2006 02:09:00 GMT
I've just started a new RoR project and thought I'd try out
HAML for the views instead of rhtml.
Seeing as I just switched to JEdit, I looked for a HAML language mode,
and to my surprise no one had done one yet.
So here is my first attempt at a language mode for JEdit and version
1.0 of the HAML language mode. It needs more work, and I hope someone
with a better understanding of JEdit language modes will help out.
This will set the indent to 2 spaces and auto-indent after a %tag that
has nothing else on the line...
%p
stuff
It will not auto indent if there is more stuff on the line like in the
following example the line after %p gets indented but the line
after %label does not.
%p
%label{ :for => "login" }Login
%br/
= f.text_field :login
I have setup a basic syntax coloring scheme that should color %tags
and .classes and #ids differently. It will color anything
following an = or ~ and defer the syntax highlighting to the ruby
mode for anything following them, ditto for anything inside { }.
Feedback and better ideas are welcome.
Download from here
To install copy the file to ~/.jedit/modes, and edit the catalog file
in that directory and add this line:
<MODE NAME="haml" FILE="haml.xml" FILE_NAME_GLOB="*.haml" />
Then restart jedit, and your haml files will be syntax highlighted.
I like HAML but there simply is not enough example code out there
yet, so here are two simple conversions from well know rhtml files
into HAML...
First the standard login form from acts_as_authenticated
- form_tag sessions_path do
%p
%label{ :for => "login"} Login
%br/
= text_field_tag 'login'
%p
%label{ :for => "password" }Password
%br/
= password_field_tag 'password'
%p
%label{ :for => "remember_me" }Remember me:
= check_box_tag 'remember_me'
%p
= submit_tag 'Log in'
Next the new user form from the same place...
= error_messages_for :user
- form_for :user, :url => users_path do |f|
%p
%label{ :for => "login" }Login
%br/
= f.text_field :login
%p
%label{ :for => "email" }Email
%br/
= f.text_field :email
%p
%label{ :for => "password" }Password
%br/
= f.password_field :password
%p
%label{ :for => "password_confirmation" } Confirm Password
%br/
= f.password_field :password_confirmation
%p
= submit_tag 'Sign up'
If this is dumb HAML coding please let me know how to improve it :)
Posted in Rails, HAML, JEdit | Tags haml, jedit, rails | 4 comments | no trackbacks