views:

40

answers:

2

Working on a request to add a pretty tooltip to a page (using jQuery tooltip plugin, as recommended by others on my team). Pretty tooltip is working fine, but two of the existing specs now fail, an example below:

describe 'with a discussion post containing html' do

  before(:each) do
    @post.update_attributes(:body => "some <strong>bold and <i>italic</i></strong> text")
    assigns[:posts] = @discussion.posts.paginate(:page => 1)
    render
  end

  it 'should strip the html tags from the post body' do
    response.should say('some bold and italic text')
  end    
end

For completness, here is the javascript I have added to the discussions show page and the resulting rspec.

<script type="text/javascript" charset="utf-8">
  $(function(){
    $('#useful_item_submit').tooltip();
  });
</script>

<div id="useful_item_form" >
  <% form_for [flaggable, flaggable.useful_items.new] do |f| -%>
    <div>
      <%= f.submit "I find this useful", :title => 'Click to let others know you found this useful' %>
    </div>
  <% end -%>
</div>

Should I change the test to ignore the extra javascript or should I not have the javascript in the show.html.erb file?

Output from spec ============

'discussions/show.html.erb with a discussion post containing html should strip the html tags from the post body' FAILED
<"some bold and italic text"> expected but was
<"some bold and italic text\n\t\t\n  $(function(){\n    $('#useful_item_submit').tooltip();\n  });">.
<false> is not true.
A: 

My plugin supports full HTML. Then you wouldn't have to strip out the HTML markup.

http://plugins.jquery.com/project/hovertiphtml

a432511
A: 

I would definitely put you javascript in a javascript file.

I do this to make including javascripts really easy:

in app/views/layouts/application.html.erb somewhere:

<%= yield :javascripts %>

in app/helpers/layout_helper.rb:

def javascript(file)
    content_for(:javascripts) { javascript_include_tag file }
end

then, in public/javascripts/useful_item.js:

$(function(){
    $('#useful_item_submit').tooltip();
    // among other useful javascripts that useful_items might use
});

and in your view:

<% javascript 'useful_item' %>
<div id="useful_item_form" >
    <% form_for [flaggable, flaggable.useful_items.new] do |f| -%>
        <div>
            <%= f.submit "I find this useful", :title => 'Click to let others know you found this useful' %>
        </div>
    <% end -%>
</div>
Amiel Martin