Archive for the 'ruby' Category

Capistrano and Solaris

Recently I’ve been using Capistrano to deploy projects to Solaris. One client uses a Joyent Accelerator (OpenSolaris) and another (University of Virginia) uses Solaris 5.8. The Accelerator is great as it has ZFS, the new Solaris Services, speed, etc. But Solaris is not the same as Linux or FreeBSD when it comes to certain common utilities and this has caused me problems on both servers.

ln

The main culprit is ln. Capistrano use ln -nfs all over the place to make soft links. Unfortunately, the version in Solaris’s /usr/bin does not remove existing soft links with this command. Oy. My first response was to rewrite the tasks like :symlink and :on_rollback to add rm -rf in front of the link command. After forgetting to do this once or twice in new deploy scripts, I posted to the TextDrive forums. Someone mentioned that the Blastwave version of ln (gnu’s version) worked. Well I’m using Blastwave (and you should too), but I don’t remember seeing that. It turns out that gnu’s ln is in /opt/csw/gnu/, not /opt/csw/bin/.

So I just needed that in front of my path, but when Capistrano logs in through ssh, it doesn’t pick up either /etc/profile or ~/.profile. (I don’t have links handy, but apparently this depends on how bash was compiled.) There are two ways of dealing with this. You can add a path to your ssh config (here’s some info), but I prefer making the change in /etc/default/login. I want the Blastwave stuff to take precedence all the time.

Advertisements

More on Should

I was reading a post to Rails Studio mailing list today when I came across this:

If you’re using mocha/stubba, you can say:
@user_notifier.expects(:deliver_activation).never

If you’re using flexmock, you can say:
flexmock(@user_observer).should_receive(:deliver_activation).never

Which would I rather use? Disregarding the frameworks, if you read my previous post, you know the answer: use the active verb! expects is far preferable to should_receive.

BDD: We shouldn’t use "Should"

I like behavior driven development. Even though I still using Ruby and Rails built-in testing framework, I write test names descriptive of behavior.

But I have stopped using should.

Should has become a bunch of noise in BDD that needs to be expunged. What’s wrong with test_should_move_resource_lower_and_return_to_edit_exhibit? One alone is fine, but to read a bunch of should phrases over and over is mind-numbing:

test_should_require_email_on_signup
test_should_require_password_confirmation_on_signup
test_should_require_password_on_signup
test_should_remember_me

What a lot of noise! Who wants to keep hearing the word “should” in their head over and over? Or writing it over and over? Not me!

Use the Active Verb!

So how do I write my test names? Using the active verb:

test_moves_resource_lower_and_returns_to_edit_exhibit
test_requires_email_on_signup
test_requires_password_confirmation_on_signup
test_requires_password_on_signup
test_remembers_me

Much easier to read, the ugly repetition of should is gone, and the method names are shorter. Recently, at Rails Edge in Reston, VA, Jim Weirich gave a test-first presentation where he used active verbs instead of should. I went up after and thanked him profusely.

Form Test Helper Plugin

This week I found the FormTestHelper plugin by Jason Garber. It both enhances and simplifies form testing in Rails functional and integration tests. The great thing is the FormTestHelper tests the form, not just the submission of the form data. For example, if you try to set a form element that doesn’t exist, an error is thrown.

Here’s my first pass at using FormTestHelper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
assertions = proc do |response|
  assert_response(response)
  assert(exhibit = assigns(:exhibit), 
   "Should have assigned :exhibit")
  assert(exhibit.errors.empty?, 
   "@exhibit should not have errors: #{exhibit.errors.inspect}")
end

exhibit_count = Exhibit.count
get(:new)
assertions.call(:success)

submit_form('new_exhibit') do |f|
  f.exhibit.title = "New Exhibit"
  f.exhibit.exhibit_type_id = 1
  f.exhibit.license_id = 1
  f.exhibit.annotation = "Exhibit notes."
end

assert_equal(exhibit_count += 1, Exhibit.count )
assertions.call(:redirect)
assert_redirected_to(edit_exhibit_path(assigns(:exhibit)))
assert(flash[:notice])

The Rails Edge

Last week I attended The Rails Edge Reston. I can honestly say that it was the best workshop or conference I have attended. I’ve been to 3 or 4 No Fluff, Just Stuff conferences, Rails Conf, and Ruby Conf. It’s true that I’ve seen many of the same speakers at these other events. Perhaps the presenting team has gelled into some sort of uber-team, but the pacing of the event and the little touches by Mike and Nicole put it over the edge for me.

It’s often said that Ruby has a very strong and positive community. The Rails Edge bore this out. One small example of this was during the MacBook Pro give-away. When the winner was announced, I heard nothing but wild, enthusiastic cheering for him.

As I told Mike Clark and a few others, this is the first conference I have attended where I was not disappointed by a single presentation. Highlights for me were all of Dave Thomas’s and Jim Weirich’s presentations. I also liked the presentation team of Chad Fowler and Bruce Williams. But as I said before, I was not disappointed in any of the presentations. The lightning and VGA presentations were fantastic, too. Quick exposure to what the some of the participants are doing with Ruby and Rails was an invaluable part of the conference.

Thanks to all the presenters and attendees, and especially Mike and Nicole for organizing the event.

Introducing Elemental

HTML Element Names as Helper Methods

Summary

repository: http://svn.dangosaur.us/svn/elemental/

Moved to: http://github.com/jamieorc/elemental/tree/master

Introduces builder-like syntax to rhtml:

<%= p @item.content %>
<%= p span @person.first_name, :id => dom_id(@person, "name_") %>

or even

<% table do @list.each do |item| tr do %>
  <%= td item.name %>
  <%= td item.content %>
<% end end end %>

Elemental allows you to use XHTML Transitional tags as helper methods in your rhtml. With traditional ERb, the code above would be written:

<p><%= @item.content %></p>
<p><span id="<%= dom_id(@person, "name_") %>"><%= @person.first_name %></span></p>

and

<table>
  <% @list.each do |item| -%>
  <tr>
    <td><%= item.name %></td>
    <td><%= item.content %></td>
  </tr>
  <% end %>
</table>

That’s more code, more noise as angle-brackets (especially embedded inside the html tag), and more lines. Elemental’s syntax is also cleaner and terser than when using content_tag:

1
2
<%= content_tag "p", @item.content %>
<%= content_tag "p", content_tag "span", @person.first_name, :id => dom_id(@person, "name_") %>

and you can’t send a block to content_tag

<table>
  <% @list.each do |item| -%>
  <tr>
    <%= content_tag "td", item.name %>
    <%= content_tag "td", item.content %>
  </tr>
  <% end %>
</table>
<table>

Usage

Elemental has three basic usages:

1. Self-closing tags: no argument, or hash only for argument:

<% br %>
<% br :class => "someClass" %>

2. Content tags: first argument is the value of the content:

<%= p "some content" %>
<%= p "some content", :id => dom_id(@object) %>

3. Content tags with a block argument:

1
2
3
<% div :class => "some-class" do %>
      ...
<% end %>

You can nest Elemental methods:

<%= p span @object.value %>
  or
<%= p(span(@object.value)) %>
  generates:
<p><span>the object's value</span></p>

The same thing with attributes (pay attention to your parentheses):

<%= p span @object.value, :id => "some_id", :class => "some_class" %>
  generates:
<p><span id="some_id" class="some_class">the object's value</span></p>
  while
<%= p span(@object.value, :id => "some_id"), :class => "some_class" %>
  generates:
<p class="some_class"><span id="some_id">the object's value</span></p>

You can nest the methods in blocks:

<% p do %>
  <% span :class => "someClass" do %>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  <% end %>
<% end %>

or

<% p do span :class => "someClass" do %>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<% end end %>

which both generate:

<p>
  <span class="someClass">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </span>
</p>

This is useful for loops:

<% ul do ['one', 'two', 'three'].each do |item| %>
  <%= li item %>
<% end end %>

which generates:

<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Options/Attributes Hash

Options are converted to regular html attributes. None are filtered, so you can certainly insert invalid attributes.

<%= span :id => "some_id", :bogus_attribute => "some_value"%>
  generates:
<span id="some_id" bogus_attribute="some_value"></span>

Omitted Tags

Rails’ ActionView::Helpers already defines form, select, and input, so these are omitted from Elemental.

Motivation

Afer using Markaby a bit, I decided there were situations where I wanted a Markaby or Builder-type syntax within rhtml’s context. I had been using content_tag quite a bit for convenience, but wanted more legible and concise code, espcially for loops.

Acknowledgments

_why for Markaby and its list of XHTML and XHTML Transitional tag lists, which I used.


Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved
Advertisements