Close and Go BackBack to Viget

Tips and Tricks

I Have a Pull Request on GitHub, Now What?

Patrick Reagan
Patrick Reagan, Development Director, April 24, 2008 0

I'm a n00b when it comes to Git, so I solicited Matt's help to get the changes in my fork up to our main GitHub account. As awesome as GitHub is, there's still no "automatically-pull-and-merge" functionality when someone wants to send you their changes (and for good reason). Here's how I made it happen:

The Scenario

I wanted to work on my constant_cache gem, so I forked the project into my own GitHub account. I made the changes I wanted, ensured that all specs were passing, committed, and then pushed the changes up to my repository. Now it was time to sync up those changes with the main repository.

Continue reading "I Have a Pull Request on GitHub, Now What?"

Maintaining Lookup Data in Your Rails Application

Patrick Reagan
Patrick Reagan, Development Director, April 22, 2008 0

Early last year, Dave Thomas gave a great talk on Metaprogramming in Ruby which he reprised last week at April's NovaRUG meeting. One of the quick examples he gave during the talk inspired me to work on an idea that began as a submission to Advanced Rails Recipes, turned into a Rails plugin, and which I just re-released as a gem. Basically, it allows you to cache the lookup data for your Rails models when your application loads.

Installation

Since we're hosting our code on RubyForge, a simple gem install will fetch the latest version:

sudo gem install constant_cache

Usage

Assuming that your application has a Status model with data that looks like this:

mysql> select * from statuses;
+----+----------+
| id | name     |
+----+----------+
|  1 | Pending  | 
|  2 | Active   | 
|  3 | Disabled | 
+----+----------+
3 rows in set (0.00 sec)

All that's needed is to include the gem in your model class and instruct it to cache its data:

require 'constant_cache'
class Status
  caches_constants
end

Status data is now available as constants on the class:

$ ./script/console 
Loading development environment (Rails 2.0.2)
>> Status.find(:all).map {|s| [s.id, s.name] }
=> [[1, "Pending"], [2, "Active"], [3, "Disabled"]]
>> Status::PENDING
=> #Status id: 1, name: "Pending"
>> Status::ACTIVE
=> #Status id: 2, name: "Active"

Since the constants point to ActiveRecord instances, the real value comes when you want to use them as part of an association:

user = User.create!(:username => 'preagan', :status => Status::PENDING)

The gem relies on a name column in your database, but can be configured to use something else as needed:

class Status
  caches_constants :key => :slug
end

Other configuration options are available, see the README file for more information.

Future Additions

Right now, the gem only handles caching data that exists in the database when the application is loaded, it isn't able to handle cases when the data changes (e.g. a record is deleted, or a name is changed). I plan to handle these cases in a future release. Feedback and patches are always welcome

Effectively Using Git With Subversion

Clinton R. Nixon
Clinton R. Nixon, Senior Developer, April 18, 2008 9

Like many organizations using Rails, we have caught the git wave, and are in a state of transition between git and Subversion. Our open-source work is stored in git repositories, but our client work is still stored in Subversion repositories, and probably will be for some time. While git is amazing, Subversion still has its good qualities, and makes an excellent centralized repository, especially with its ecosystem of user-friendly tools.

The integration between git and Subversion (git-svn) is so well done that several of us have been using git as our interface to all our Subversion repositories. Doing this is fairly simple, but there are some interesting tricks, and so I thought I would share a day in the Viget life with git-svn.

Continue reading "Effectively Using Git With Subversion"

Installing Sphinx on OS X Leopard

Clinton R. Nixon
Clinton R. Nixon, Senior Developer, April 17, 2008 8

I am working on a project where we are using Ultrasphinx as the search solution for the site. I had to install Sphinx to make this work, and I soon found out that the version of Sphinx in MacPorts is one release too old to work with Ultrasphinx. (You need to use the latest release of version 0.9.8.) I very much like using package management, but sometimes you have to bite the bullet and compile something yourself.

When I tried to compile Sphinx with the tried-and-true ./configure; make; make install, I ran into some serious problems. It wouldn’t compile, and gave me this error:

g++  -Wall -g -D_FILE_OFFSET_BITS=64 -O3 -DNDEBUG   
-o indexer  indexer.o libsphinx.a  -L/opt/local/lib 
-L/opt/local/lib/mysql5/mysql -lmysqlclient -L/opt/local/lib 
-lz -lm  -L/opt/local/lib -lssl -lcrypto  -liconv -lexpat  
-L/usr/local/lib
Undefined symbols:
  "_iconv_close", referenced from:
      xmlUnknownEncoding(void*, char const*, XML_Encoding*)in libsphinx.a(sphinx.o)
  "_iconv", referenced from:
      xmlUnknownEncoding(void*, char const*, XML_Encoding*)in libsphinx.a(sphinx.o)
  "_iconv_open", referenced from:
      xmlUnknownEncoding(void*, char const*, XML_Encoding*)in libsphinx.a(sphinx.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [indexer] Error 1
make[1]: *** [all] Error 2
make: *** [all-recursive] Error 1

It turns out that I needed to compile the latest versions of the expat XML parser and iconv. Here’s a quick guide to how I got everything working:

Download iconv from the GNU project and compile it.

curl -O http://ftp.gnu.org/gnu/libiconv/libiconv-1.12.tar.gz
tar zxvf libiconv-1.12.tar.gz
cd libiconv-1.12
./configure --prefix=/usr/local
make
sudo make install
cd ..

I’m installing everything in /usr/local, and I suggest you do, too. I don’t want to mix up the programs I compile with the OS X system libraries and binaries (/usr) or MacPorts’ libraries and binaries (/opt/local).

Download expat and compile it.

curl -O http://internap.dl.sourceforge.net/sourceforge/expat/expat-2.0.1.tar.gz
tar zxvf expat-2.0.1.tar.gz
cd expat-2.0.1
./configure --prefix=/usr/local
make
sudo make install
cd ..

Download Sphinx and compile it. There’s a new flag given to configure this time, specifying the location of my MySQL installation. I installed it via MacPorts; if you installed it differently, you will have to change this flag. Also, you may want to go to the Sphinx download page to see if there’s a new version of Sphinx out. The version current while I’m writing this is a release candidate, and a finished release of 0.9.8 may be out by the time you read this.

curl -O http://www.sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz
tar zxvf sphinx-0.9.8-rc2.tar.gz
cd sphinx-0.9.8-rc2
./configure --prefix=/usr/local --with-mysql=/opt/local/lib/mysql5
make
sudo make install
cd ..

The install is quick and easy, but certainly wasn’t when I was trying to figure out why Sphinx wouldn’t compile.


Git with remotes and SVN

Ben Scofield
Ben Scofield, Development Director, April 01, 2008 4

The laziness plugin I’ve been working on is hosted in two different SCM systems: Subversion, at Viget’s SVN repository, and git, at GitHub. I use git in development, which means that pushing out to GitHub is easy - but maintaining a git-svn connection at the same time is a little tricky.

Starting from this post, I finally got things moving. Basically, I created a local branch to interact with SVN independently of the main development; when new features (like the specific exception handling I added recently) are done, I push ‘em out to GitHub normally. Then I switch into the aforementioned local branch, merge from master, and dcommit them up.

The only problem I’ve run into is the occasional conflict on merging from master - and that’s easily resolvable by editing the conflicted file and git adding it before committing and dcommitting.

I long for the day when I can get by on git alone, but until then this appears to be a workable solution.

A Development Community for Viget Labs and Beyond

Every team member here at Viget Labs strives to be an innovator. We members of the development team are no different - that's why we're constantly engaging in community discussions and exploring the unknown that is the next generation of open-source web applications.

Viget Is Hiring!

Viget has job openings for Ruby Developers, Interns, and Front-End Developers. Learn More »

Recent Comments

I think that polymorphic_url(@commentable, :anchor => “comment_#{@comment.id}") should work. You can also refactor the “comment_#{@comment.id}” to a separated method, like dom_id, which returns the dom identifier of the comment.