Home > Instruction > Creating a base project in rails

Creating a base project in rails

I spent some time over the last few days working on a base application for rails. The idea is straight forward, reduce the start up requirements of a new project – the authentication mechanism, multiple framework inclusions, css layouts, routing, and seo additions are all setup initially. With these pieces of the application already configured, the focus can be placed on development instead of setup.

** note – I am considering a rails template, but that will be a future addition. For now, the project will consist of several gems and plugins.

Here is a link to the github repository where the code resides. If you are not familiar with github, feel free to reach out to me via my User Voice forum and I will help however I can.

Now for the breakdown…

Testing Frameworks/Tools

RSpec RSpec external link / RSpec-Rails RSpec-Rails external link

Both testing frameworks have been vendored so there is no reason to not test (double negative == positive you will test)

Authentication

Clearance Clearance external link

Opinions: Cucumber, Factory Girl, Webrat
Dependencies: Nokogiri (Webrat)

I have spent too many days considering what authentication framework to use. Restful authentication was a previous choice, which I found to be unmanageable and always requiring another tweak. Another opinion was AuthLogic, which seems to be attracting a following. However, I chose clearance… for now. It was created by the folks at thoughtbot, who have plenty of other tasty projects for inclusion.

So to begin with, all authentication requires a means of sign-up, sign-in, sign-out, and password retrieval. This core functionality is essential in making sure users will be able to use your website. They are embedded into the clearance project. It did not unfortunately include clean SEO routes. Not that a login/logout page needs SEO, but for good measure I changed the routes in clearance to practice between url friendliness. An couple examples are dashes instead of underscores and content text rich urls (think /sign-in versus /sessions/new). All of these items should help make clearance a useful auth mechanism within your project.

Layouts

There is only one layout represented in the project, “public.” It cleans up the sign-xxx page layouts, adding fieldsets into forms, auto margins, padding, font details, and a few other CSS tweaks.

Stylesheets

There are multiple styles within stylesheets directory. The naming is standardized on the layout, so all public layout styles will reside under the stylesheets/public directory The stylesheets tag is added into the public layout using the cache key so all stylesheets will be joined into one file in production

taken from app/views/layouts/public.html.erb

stylesheet_link_tag 'public/layout.css', 'public/default.css',
                    'public/forms.css',
                    :media => 'screen', :cache => 'cache/cache'

Javascript
Prototype, jQuery, and an application js has been added to the project. Prototype is added first, with jQuery.noConflict() afterward to keep jQuery from stepping on prototype. The javascript tag is added into the public layout as well, using cache key for one combined javascript file on your production servers.

taken from app/views/layouts/public.html.erb

javascript_include_tag 'prototype', 'jquery-1.3.2.min', 'application',
                       :cache => 'cache/cache'

As it is setup, all jQuery calls will rely on using the reference jQuery instead of the $ function. jQuery can be configured to work with other libraries too. An example is below.

taken from app/views/layouts/public.html.erb

jQuery(document).ready(function() {
...
});

Seo

Rubaidh Google Analytics Rubaidh Google Analytics external link

What public facing site is finished without adding google analytics integration? Without tracking, we don’t know how effectively the message is at targeting our customer. That said, most websites are using some type of analytics toolset behind the scenes to determine demographic information and find ways of segmenting it. I chose google analytics as it is familiar to most. The only part still required for you is setting up the google analytics account and configuring the key in the file below:

taken from config/environment.rb

Rubaidh::GoogleAnalytics.tracker_id   = 'UA-XXXXX-XX'
Rubaidh::GoogleAnalytics.domain_name  = ''
Rubaidh::GoogleAnalytics.environments = ['production']

Additional SEO

I started an SEO addition to this project that creates a new folder config/seo for localized files of static SEO mappings and uses code in the application.rb file and an initializer config/initializers to pull them into the project. An example configuration has been created as en-us.yml is the project. The idea is to define title, description, keyword, and other meta information in a key/value yml file so it can be used in multiple locales. Dynamic mappings can be made with the function set_meta inside any controller. While I know this idea has some downsides, this is a trial / learning period to see what ideas come to light.

taken from config/initializers/meta_mappings.rb

@@META_MAPPINGS = YAML.load_file(
                  File.join(RAILS_ROOT, "config", "seo", "en-us.yml") )

Geo Mapping

Geokit Geokit external link

Included in the project is the geokit gem. It makes use of multiple geocoders on the web. What is a geocoder? It is an API that allows a person to search for latitude and longitude values for a particular point based on address, ip, or other related information. Google is known by most people trying to learn, but more geocoders can be found at the bottom of the geokit rubyforge website readme.html.

taken from config/environments/development.rb

Geokit::Geocoders::google =
'ABQIAAAA03NbuyzEzQFenBHfN8J8TBQP1sBEw9yUgsSPPDa33Q5sgDohShTabokZ0czZTUODwnLWVkXv-lLvtw'

Pagination

Will Paginate Will Paginate external link

What dynamic website does not have pagination? Unless you have found a perfect searching algorithm, you are likely to need this plugin in your views.

Image Upload

Paperclip Paperclip external link

This is another thoughtbot open source project. It works in combination with ImageMagicK to upload, manipulate, and persist images on the filesystem. A few minor filesystem link tweaks are suggested when using capistrano. That will be covered when capistrano is added to this document. For now, the thoughtbot paperclip url is a good reference for information on use.

Logging

Exception Notification Exception Notification external link

Having a mechanism in the application that brings light to any problems is essential. Exception Notifier is a configuration plugin that automates an email based on controller exceptions. It is setup by adding a line to the config/environment.rb file as seem below. Make sure to add your email address to this line.

taken from config/environment.rb

ExceptionNotifier.exception_recipients = %w(my_email@a.domain.com)

Full-text indexing

ThinkingSphinx ThinkingSphinx external link

Full-text indexing is the goods. It provides an abstract way to say, “search my model.. or models, for all the data inside.” It also makes searching for data less code intensive and faster. ThinkingSphinx is a plugin that can tie directly into the sphinx full-text search engine. For those who are unfamiliar with sphinx, further information will be provided on how to use this plugin and setup the sphinx search engine in a later post.

Deployments

Capistrano

Coming soon!

Suggestions?

Comments and suggestions for improvements, bug fixes, or thrashings are welcome and requested! Please use the User Voice forum that is setup.


  1. No comments yet.
  1. No trackbacks yet.
Google Analytics Alternative