Fun with the simple_form Rails Gem

It’s been a long time since I sat down to write a blog about a programming related topic. I’m really hoping that changes this year, but I’ll do my best not to make promises I can’t be sure I’ll keep. I definitely hope to share more of the things I’m learning about and playing with. With that in mind, I was playing with the simple_form gem earlier today and I thought I would share the experiment that I was working on.

Often when working on a project I’ll have a few basic forms that I need for data entry within the system. Nothing too complex, just a few simple fields. For those of you who haven’t checked out simple_form yet, I highly recommend it. After getting the gem installed I created the form I needed:

<%= simple_form_for @member do |f| %>
  <%= f.input :firstname %>
  <%= f.input :lastname %>
  <%= f.input :email %>
  <%= f.input :bio %>
  <%= f.button :submit %>
<% end %>

Knowing that I would need a few more simple forms I wanted to figure out a way to be able to reuse this code snippet. Although I might not do this same approach in a large project with more complex forms I proceeded to pull out the column symbols within the snippet and came up with this:

<%= simple_form_for @member do |f| %>
  <% @member.class.column_names.each do |field| %>
    <%= f.input field.to_sym unless %w{id created_at updated_at}.include?(field) %>
  <% end %>
  <%= f.submit %>
<% end %>

This takes the @member instance variable and get its class, which in this case is Member. It looks at the column names of Member and loops through them. If the field isn’t “id”, “created_at”, or “updated_at” it creates an input field for it. This was a step closer to what I was going for. I imagine there are many opinions about this approach especially when it comes to thoughts on performance. The focus here was about experimentation. So I keep going and made some additional adjustments, caming up with this:

<%= simple_form_for form do |f| %>
  <% form.class.column_names.each do |field| %>
    <%= f.input field.to_sym unless %w{id created_at updated_at}.include?(field) %>
  <% end %>
  <%= f.submit %>
<% end %>

This allowed me to call the code as a partial like:

<%= render :partial => 'shared/form', :object => @member %>

It’s not perfect, I can’t customize the fields in any way, but it let’s me get some basic forms quickly together for testing concepts with a customer.

Anyway, that’s my recent experiment, feel free to share your thoughts and feedback.

Magic-Ruby 2011 Conference Talks

I was lucky enough to be able to spend the last few days with some amazing Ruby developers. I enjoyed meeting so many new people and getting the chance to learn about some seriously awesome projects that they are working on. In an effort to consolidate some of the slides and information shared by the speakers I’ve created this post. If you have any updates please leave them in the comments and I’ll continue to complete the list.

Friday

  • Cultivating Cucumber: Slides
    Les Hill, Hashrocket
  • Geospacing Your Ruby: Slides and Video of similar talk from July 9, 2010
    Peter Jackson, Intridea
  • Loving your customers, loving your peers
    Alan Johnson, Carsonified
  • Code Isn’t Enough
    Gregg Pollack / Caike Souza
  • Exceptional Ruby: Slides, Code, and Review | Video
    Avdi Grimm
  • What Happened to Desktop Development in Ruby
    Andy Maleh, Obtiva
  • Keynote
    Dave Thomas, Pragmatic Programmers

Saturday

  • Meditation + Code: Slides
    Mike Gehard, Pivotal Labs
  • Crank Up Your Apps with TorqueBox: Slides | Video
    Jim Crossley, Red Hat
  • How I Learned to Stop Worrying and Love the Cloud: Slides
    Wesley Beary, Engine Yard
  • Developing Cocoa Applications with MacRuby: Slides
    Brendan Lim, Intridea
  • Documentation is freaking awesome: Slides and Links
    Kyle Neath, Github
  • Lightning Talks: Videos
  • Keynote – McDonalds, Six Sigma, and Offshore Outsourcing: Notes
    Chad Fowler, InfoEther

A special thanks to all the speakers and Jeremy McAnally for putting it all together. Anyone wishing to stay in touch can find me on Twitter @timknight.

Making Configuration Files with YAML: Revised

So back in July 2007 I posted a blog on making configuration files with YAML, and I’ve been noticing a lot of readership on the old article. Because it seems that a lot of people are reading it I felt it was important to show how I apply this nowadays.

First I put my config.yml file within the /config/ directory within my Rails application. It looks something like this:

development: &non_production_settings
  :google_analytics:
    :api_key: "[Enter Google ID]"
  :site:
    :title: "[Title]"
    :address: "http://localhost:3000/"

test:
  <<: *non_production_settings

production:
  :google_analytics:
    :api_key: "[Enter Google ID]"
  :site:
    :title: "[Title]"
    :address: "[Address]"

Then, I create a new file called load_config.rb within the /config/initializers directory. You can name the file whatever you want – that’s just what I call it. This is where the actually YAML loading is going to happen – and this is what it looks like:

raw_config = File.read(RAILS_ROOT + "/config/config.yml")
APP_CONFIG = YAML.load(raw_config)[RAILS_ENV]

Now any time I want to all one of these variables I just call it like:

<%= APP_CONFIG[:site][:title] %>

Optimizing the RedCloth Helper

In my previous post I posted several small Rails tips, one of which was a cleaner RedCloth helper. Unfortunately, this helper requires that the textile is parsed each time the page is loaded, and that can get nasty. So it our item is a text field called details, just add a details_html field to your model… then create a private conversion method that you call using a before_filter.

Something like:

before_filter :convert_details

def convert_details
  return if self.details.nil?
  self.details_html = RedCloth.new(self.details).to_html
end 

Then just display details_html in your view instead. This way the textile only gets converted when you save and make changes to it.

Rails Tip Roundup

I’ve been working on a few new projects lately and wanted to share a few little tips I’ve started doing.

Conditional Buttons for Shared Forms

Something that I like to do is use a shared form for both my edit and new views. Doing this however means I need to be friendly to the user interface and make sure the submit button is properly instructional. So for example if I have a Customer model with an instance variable @customer my button would look like this:

<%= f.submit((@customer.new_record? ? "Create" : "Update") + " Customer") %>

With this I’m checking to see if the @customer instance variable belongs to a new record and if so it’s outputs Create, otherwise it’s an Update button.

Cleaner RedCloth Helper

There is the built-in textile helper that comes with Rails, it’s carage return rendering is a bit lame so most people typically upgrade their RedCloth gem and using something like:

<%= RedCloth.new("My copy that requires formatting").to_html =>

I prefer to instead create an application level helper for redcloth (some people might instead overwrite the textile helper here, but I find that can be confusing to some people looking at your code for the first time). My helper looks like this:

def redcloth(str)
   RedCloth.new(str).to_html
end

So now when I want to redcloth something I just call:

<%= redcloth("My copy that requires formatting") %>

Simple Little Permalink

When I have a simple object that I want to create more user-friendly URLs for, I’ll create a basic permalink. In the instance of the same Customer model from tip 1 above, I like to use the customer name as the permalink. To do this of course the name has to be unique so make sure you are validating it’s uniquiness above all. Then I create a permalink column in the database table and write something like this in my model.

def name=(value)
  write_attribute :name, value
  write_attribute :permalink, value.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
end

def to_param
  permalink
end

This uses the value of the name that is entered, clears it of puncuation, replaces spaces with hyphens and drops the casing. The first line makes sure it still remembers it needs to write the value itself to the name column in the model.

Now it’s still important that you confirm that the permalink is unique too, but I’ll let you do that on your own.

So that’s it – I hope you guys find it useful.

Making Configuration Files with YAML

Update: A newer version of how I load YAML into configuration files in my projects can be found here.

Since being introduced to YAML I’ve loved using it for configuration files in both Ruby and Ruby on Rails. YAML means “YAML Ain’t Markup Language”. Yes, there is an infinite loop in the title – it’s programmer humor. Those of you familiar with Ruby on Rails are somewhat familiar with YAML because that is the format of the database.yml configuration file. So this is a sample of what a YAML file typically looks like:

development:
	adapter: mysql
	database: project_development
	username: root
	password:
	socket: /tmp/mysql.sock

This is a snippet out of the database.yml file. But one of the great things about Ruby is that writing code to read YAML is extremely simple. Let’s say I want to make a website configuration file, this is what that might look like:

config:
	title: My Rails Website
	author: Santa Claus
	email: email@company.com
	css_file: default.css

The hardest part is thinking about what type of information you want to store in your configuration, the Ruby is extremely easy. Check it out…

First we require the YAML library:

require 'yaml'

Ok, now we can make a read_config method:

def read_config
	config = YAML.load_file("config.yaml")
	@title = config["config"]["title"]
	@author = config["config"]["author"]
	@email = config["config"]["email"]
	@css_file = config["config"]["css_file"]
end

Seriously, that’s it (of course you need to execute the method). We just load the YAML into a local variable. Then we spider down the YAML document using the local variable that we assigned the loaded YAML document. So where you see “config” in quotes that is referring to the “config:” within the YAML document. Of course, were I say “config.yaml” you would actually put the path to your specific YAML document. One the information is loaded, I pull the data I want into a few instance variables making them accessible to my views (in Rails).

Update:

A more efficient way to do this would be to loop through the hash that is created by the read_config method and just set the key to an instance variable, like so:

config["config"].each { |key, value| instance_variable_set("@#{key}", value) }

Update: A newer version of how I load YAML into configuration files in my projects can be found here.

Formatting Names Using Ruby

Getting information from users is a common task that we have to deal with in building an application. This typically happens either in creating a user account, commerce orders, or any other time when the application has to ask “who are you?”. Often times we’ll create a field for both a firstname and a lastname allowing the user to separate their names to make it easier for us (the application developer) to display either/or both. In this post, I’m going to talk about how we can format full names given to the application by the user without necessarily worrying about creating two textfields. Of course, you would use this code in the methods within a Rails app.

So let’s say I have a variable for creative purposes, I’ll call it ‘name’ and ‘name’ has a value.

name = “James Earl Jones”

And I want to only display the first and last name of my ‘name’ value I could do this:

names = name.split
firstname = names.first
lastname = names.last

Now this splits the three words in ‘name’ into an array. I then set firstname as the first item from the array, and set lastname as the last item from the array.

Running:
p “#{firstname} #{lastname}”

Would then output:
“James Jones”

How about we do another common formatting task we can to show the last name first separated with a comma the rest of the name, we could do this:

names = name.split
lastname = names.pop
firstname_remaining = names.join(‘ ‘)

Here we split the name up again, put then ran the pop method which pulls the last time of the array out of the array and then returns it. This would leave only the remaining name left to work with as the firstname_remaining variable. I them join the rest of the array with spaces for the firstname_remaining (which would include the first name and any remain name information like a middle name).

Running:
p “#{lastname}, #{firstname_remaining}”

Would then output:
“Jones, James Earl”

Lastly, a common thing I’ve been seeing lately is the usage of the last initial when showing a user’s name. We could do something like this:

names = name.split
firstname = names.first
lastinitial = names.last[0,1]

Here I’m introducing some trimming for the lastinitial variable. You see this with a bracket and two numbers. The first number is the offset in the string that I’m trimming (zero is the first letter is a string), the next number is how many characters I’m showing from that offset.

Running:
p “#{firstname} #{lastinitial}.”

Would then output:
“James J.”

So that’s it for now. Of course you might think about adapting this to users that will have items at the end of their name like “Jr.” – this can sometimes just be solved using a drop-down.  I’m hoping that perhaps I have introduced you to some things in Ruby that you might have been previously unaware of. Until next time.