Yeah, this post has the WORST title ever, but i tried to litter it with keywords so people struggling can find it easily.

When messing with my Rails views I find that pretty commonly I’m dealing with lists of things i need to use link_to on AND split them with a comma.  Here’s a super simple trick that will help you keep your view cleaner (this is for Haml).  For the sake of this tiny code snippet pretend you are trying to create a list of your recent posts separated by commas:

= raw @recent_posts.collect { |post| link_to post.title, post }.join(",")

By using collect you iterate over your array of recent posts and build each one into a link using the ‘link_to’ helper — THEN just join with a comma and BAM you have a nice html string of links.  Prefix the whole thing with ‘raw’ because if you don’t Rails will emit escaped HTML in your face.

I talked to a gentleman in #rubyonrails on IRC who goes by the name of codepoet_ about Rails3 and Arel for a while today and he linked me to what is somewhat of an Arel cheat sheet he created.

With his permission I am reposting this as a reference tool for myself.  Hopefully someone else can benefit!
[sourcecode language=’ror’]
# In a User model:

scope :men, where(“sex = ?”, ‘M’)
or
scope :men, where(:sex => ‘M’)

scope :in_tights, where(“clothing_preference = ?”, ‘Tights’)
or
scope :in_tights, where(“clothing_preference, ‘Tights’)

scope :created_in_first_week_july_this_year, where(“DATE(created_at) BETWEEN ? AND ?”, Date.parse(“July”), Date.parse(“July”) + 7.days)

scope :with_dark_hair, where(“(hair_color = ? OR ?)”, “black”, “brown”)

scope :oldest_first, order(“age DESC”)

scope :oldest, oldest_first.limit(1)

scope :not_cancelled, where(“status != ‘Cancelled'”)

scope :old_dark_men_in_tights_for_july, men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# get the men

User.men

#get the men in tights (these two do the same thing)
User.men.in_tights
User.in_tights.men

# get the men, order by the oldest first
User.men.oldest_first

# get the oldest man
User.men.oldest

# get the oldest man who has not been cancelled
User.men.oldest.not_cancelled

# get the users created in the first week of july
User.created_in_first_week_july_this_year

# get the dark hair colored men in tights created in the first week of july
User.men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# Or you can use the last scope which references other scopes, to cut down on the tedium of chaining so many things:
User.old_dark_men_in_tights_for_july
[/sourcecode]

I wanted to play around with creating my own gems so today I decided to try the jeweler gem.

Install went fine:

io@love:~$ sudo gem install jeweler
Successfully installed jeweler-1.4.0
1 gem installed
Installing ri documentation for jeweler-1.4.0...
Installing RDoc documentation for jeweler-1.4.0...

But when i attempted to create “the perfect gem” I ran into a bit of a problem:

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
/usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:700:in `command': git config "--global" "--list"  2>&1:fatal: unable to read config file /home/io/.gitconfig: No such file or directory (Git::GitExecuteError)
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:672:in `command_lines'
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:352:in `global_config_list'
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git.rb:148:in `global_config'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/options.rb:15:in `initialize'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/application.rb:9:in `new'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/application.rb:9:in `run!'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/jeweler:5
	from /usr/bin/jeweler:19:in `load'
	from /usr/bin/jeweler:19

And after some research… looks like i had not done some mandatory git setup steps… as i SHOULD have.

The solution…

io@love:~/ruby/gem_test$ git config --global user.name "iouser"
io@love:~/ruby/gem_test$ git config --global user.email "[email protected]"
io@love:~/ruby/gem_test$ git config --global github.user iouser

And finally the glory:

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
	create	.gitignore
	create	Rakefile
	create	LICENSE
	create	README.rdoc
	create	.document
	create	lib
	create	lib/the-perfect-gem.rb
	create	test
	create	test/helper.rb
	create	test/test_the-perfect-gem.rb
Jeweler has prepared your gem in the-perfect-gem

Note: If you see the this error

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
No github.user found in ~/.gitconfig. Please tell git about your GitHub account (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt

it means you forgot to do this part:

io@love:~/ruby/gem_test$ git config --global github.user iouser

I ran into an issue today.  I have a Blog model that has_many feed_entries.

In plain english, I needed a list of blogs ordered by most recently updated feed.

My models look like this:

blog.rb
has_many :feed_entries

feed_entry.rb
belongs_to :blog

The query that makes it possible:

Blog.find(:all, :include => :feed_entries, :order => 'feed_entries.published_at DESC')

:include makes this possible!

This is how i got Google Maps working in Radiant CMS via the google_maps extension.

This one is pretty easy, but you should know that you HAVE to be using MySQL or PostgreSQL because google_maps uses the spatial adapter plugin which needs one of those 2.

First install the GeoRuby gem.  Careful this is case sensitive:
sudo gem install GeoRuby

Next, from within your Radiant project’s root folder, clone the extension from github:
git clone git://github.com/rurounijones/radiant-google-maps-extension.git vendor/extensions/google_maps

Time to run the Rake tasks:
rake radiant:extensions:google_maps:api
rake production radiant:extensions:google_maps:migrate
rake production radiant:extensions:google_maps:update

Edit config/gmaps_api_key.yml and add your own Google Maps API key.

Add this R tag inside your layout’s head tag:
<r:google_map:header />

Goto the Maps tab in the Radiant Admin and create a map

Create a new page and name it something.  In the body of that page add:
<r:google_map:generate div=”my_gmap_div_name” id=”1″ />
<div id=”my_gmap_div_name” style=”height: 600px; width: 868px;”></div>

That provides a div for the map to go in, and calls the google_map generate tag with Map id 1.  Look on the github page for more stuff.

This post is more of a personal note to myself. 99% is copy/paste from either the Radiant wiki, or the Mailer extension’s github page. I’m notating this because both the Radiant wiki and Mailer’s github page have instructions that confused the hell out of me so I’m consolidating it here after getting it working.

This tutorial assumes you have a freshly bootstrapped Radiant installation. Let’s Go!Continue reading

WYMeditor is a really cool wysiwyg-ish editor — well technically it’s a WYSIWYM  (What You See Is What You Mean).

It just so happens that a gentleman named Benny Degezelle has created an extension for Radiant CMS.

At the time of this writing I’ve installed this bad boy on Radiant CMS 0.7.1.

FIRST

Assuming that you have an already bootstrapped (aka ‘rake production db:bootstrap’) installation of Radiant CMS you should first clone the extension from GitHub while in your application’s home directory:

me@world:/www/radiant.domain.net$ git clone git://github.com/jomz/radiant-wym-editor-filter-extension.git vendor/extensions/wym_editor_filter

The command above will clone it to /vendor/extensions/wym_editor_filter

SECOND

Run the Install rake task!

me@world:/www/radiant.domain.net$ rake radiant:extensions:wym_editor_filter:install

THIRD

Run the Update rake task just to be safe.  You can run this at a later point in time to update, obviously.

me@world-www:/www/radiant.domain.net$ rake radiant:extensions:wym_editor_filter:update