Posts Tagged 'ruby'

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.

Dumping a Table to Yaml

I decided I wanted all 356 rows of a table dumped to a fixture, but
CSV was a disaster. So:

require 'yaml'
js = Journal.find_all
File.open ('dump.yml', "w") do |f|
  js.each do |j|
    f.print YAML.dump ({"j_#{j.id}" => j.attributes})
  end
end

(I actually did it from script/console.) The only caveat is I got a
line of “” between each entry that I had to delete.

Using Active Record to Test a SQL Server Legacy DB Connection

I recently came across Sam Ruby’s Rails Confidence Builder post and used it as a model to test out my connection to a legacy SQL Server database. I’m undertaking a new project with Mel at Upstate and wanted to verify connectivity before diving into the Rails application. I’m using Actual Technologies SQL Server Driver because it’s easy to set up and use, making it worth the 30 bucks. One thing I noticed right away was that the attribute names come back in the same case as they are in the table definitions, so I’ll have to translate those to lower case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'rubygems'
require_gem 'activerecord'

ActiveRecord::Base.establish_connection(
  :adapter  => "sqlserver",
  :mode =>  "odbc",
  :dsn => "the_dsn",
  :database =>  "db_name",
  :username =>  "userName",
  :password =>  "some_password"
)  

class Customer < ActiveRecord::Base
  set_primary_key "customer_id"
  set_table_name "customer"
end

Customer.find(:all).each do |customer|
  puts customer.NAME
end

Another cool thing is that in TextMate all I have to do is plop the code in a new buffer and press CMD-R to run it.


Pages

Categories

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