Blog - Microsoft .NET, ASP.NET, AJAX and more

Ruby Beauty – Rendering a Rails Partial for a Collection

by Damien White 4/27/2010 10:48:16 PM

Continuing with my exploration of Ruby on Rails as a .NET guy, here’s an awesome example of Convention over Configuration at work.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Today’s post illustrates how elegantly the render partial helper method in Rails works.  Assuming you’ve done some MVC development, you should be very familiar with a partial view.  Partials are used to keep your view nice and clean.  When things start getting complex within a view, you may want to think about adding a partial. 

Take for example a library or bookstore application.  One of the models in the application is, of course, a book.  The books are displayed in a list as well as individually on a details page.  During the course of development, you notice that you have quite a bit of repeated code between the index and show pages.  This is a good opportunity to use a partial to DRY up your code.  You take the repeated book view code (which I won’t post here since it doesn’t matter what it is), and put it into a partial (_book.html.erb).  Within the partial we are referencing a local object simply called book

Single Partial

On your detail page (a single book), it’s a simple replacement like so:

<%= render :partial => "book", :object => @book %> 

But even better, since our partial is named the same as our model, we can shorten up the syntax:

<%= render :partial => @book %> 

Now that’s some convention goodness right there.  

More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Rails and Time Zones

by Damien White 4/21/2010 7:52:05 PM

Continuing with my exploration of Ruby on Rails as a .NET guy, here’s an awesome example of Convention over Configuration at work.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in others, see my other Ruby posts.

Time zone handling has always been one of those things that I’ve tried to avoid.  A recent ASP.NET app I was working on needed to support time zones and it was quite a pain to implement.  Well, within the context of Rails, I was astonished at how easy it was to handle time zones. 

By default, ActiveRecord stores datetimes in the database as UTC (although you can change this behavior, however, I’m not sure where this would actually be helpful).  This makes it very easy to convert a datetime to another time zone.

Conversion Magic

At the application level, you can set a default time zone.  Within your environment.rb file (sort of like an ASP.NET web.config file), you can just simply set:

config.time_zone = "Eastern Time (US & Canada)"

Now, when you display a date within a view it will be converted to EST –5:00 (or EDT –4:00 like it is now).  Also, when a user enters a date, it will be entered as Eastern Time, and it will be converted and saved to the DB as UTC. 

More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Method Naming Conventions

by Damien White 4/12/2010 9:58:26 PM

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Naming conventions can be a good thing providing there is actually some rhyme and reason to them.  We’ve seen a lot of changes with conventions, like Hungarian Notation for example, who’s still using that?  Within the context of .NET, there are defined conventions by Microsoft to which I would say I mostly follow (I still usually prefix private global member variables with an underscore).  The guidelines for .NET are really good, much better than something very arbitrary like Hungarian Notation where no two developers use the same syntax on things. 

There are two method naming conventions in Ruby that I really like.  Within Ruby a method name can end in either a question mark (?) or an exclamation point (!).  Having these conventions makes for nicer syntax.  Let’s look at them in a little more depth More...

Shout it kick it on DotNetKicks.com Bookmark and Share

ActiveSupport Singularize Problem

by Damien White 4/9/2010 9:28:27 PM

I just happened to encounter a problem with the ActiveSupport inflector in my Rails app.  Here’s the problem: words that end in ess seem to be an issue.  For example:

'dress'.singularize # => 'dres'
'business'.singularize # => 'busines'
'address'.singularize # => 'addres'

D'oh, where did that ending 's' go?

Turns out that “Rails has a longstanding policy of not taking further inflector patches. Use an initializer in your application instead,” a quote from Mike Gunderloy.   What exactly does this mean?  Simple…

Within your Rails application, open the config/initializers/inflections.rb file and just add the following:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.singular(/ess$/i, 'ess')
end

All better…

'dress'.singularize # => 'dress'
'business'.singularize # => 'business'
'address'.singularize # => 'address'
Shout it kick it on DotNetKicks.com Bookmark and Share

Ruby Beauty – Optional Parentheses on Method Calls

by Damien White 4/8/2010 8:52:45 PM

Continuing with my exploration of Ruby as a .NET guy, here’s another example of Ruby’s expressive syntax in action.  I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails.  If you are interested in more, see my other Ruby posts.

Optional parentheses on method calls are such simple things, but they render some pretty code.  Ruby allows you to omit parentheses on method calls in a lot of cases.  Take the following code:

puts "Hello World"
puts("Hello World")

The two lines are equivalent, yet the first is just cleaner, don’t you think?  Being a diehard C# guy, I do like parentheses, curly braces, and semi-colons, but I’ve got to say Ruby does yield some beautiful code.

In a recent Rails app I used Ryan Bates’ CanCan gem for authorization.  It’s a really nice gem and allows you to define abilities for a user elegantly.  Using CanCan, you can see how nice and clean the code looks.  For example:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

The code flows nicely; what can an admin do? “can manage all.”

If we were required to add parentheses it’s just is a bunch of extra, unneeded noise, and it breaks up the flow:

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?()
      can(:manage, :all)
    else
      can(:read, :all)
    end
  end
end

user.admin?() - <gag> right?() :)

Shout it kick it on DotNetKicks.com Bookmark and Share