Tag: rails


Congrats to the Whole Rails Team

Posted By Ryan Stout on August 30, 2010

Rails 3 has officially dropped. I've been following the progress since it branched and I think the core team (and all the contributors) have done a great job. Not only does rails 3 have some great new features, but the refactoring under the hood means that plugin development will continue to grow. Bundler has already made my day to day rails development much easier and the UJS helpers are a great way to approach javascript support. Having the libraries use delegation is a simple and elegant solution to a complex problem. All in all, very well done. It might have taken a little longer than expected, but the upgrade process is fairly easy, and the learning curve isn't bad.

Again, from us here at agile productions,

Congratulations and Thank You to all involved.


Tags: rails, rails3



Page Caching by Subdomain in Rails and Nginx

Posted By Ryan Stout on July 30, 2010

I'm internationalizing an app, and am splitting the locales by subdomain (es.bustaname.com, fr.bustaname.com, etc...) While other people have solved the page_caching issue with subdomains before, I couldn't find a clear explanation. All of the examples were missing one piece of information.

Controller

Add the following into your controller.

class ApplicationController < ActionController::Base
  # Cache pages with the subdomain
  def cache_page(content = nil, options = nil)
    path = "/#{request.host}/"
    path << case options
    when Hash
      url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
    when String
      options
    else
      if request.path.empty? || request.path == '/'
        '/index'
      else
        request.path
      end
    end
    super(content, path)
  end
end

This will cache each subdomain in its own directory.

To keep things clean I also move my cache directory into public/cache Just add the following to your environment.rb inside of the run block.

Rails::Initializer.run do |config|
	config.action_controller.page_cache_directory = RAILS_ROOT + '/public/cache/'
end

Then the part everyone else seemed to miss is that this will make something like caches_page :index (note the pluralization) work, even though we defined a cache_page method. ActionController::Base has both a class and an instance version of cache_page and a class method called cache_pages. You want to use the class method cache_pages.

Nginx

Next place the following in your nginx.conf file:

# If the file exists in the public folder, send it
if (-f $request_filename) {
  break;
}

# Check / files with index.html
if (-f $document_root/cache/$host/$uri/index.html) {
  rewrite (.*) /cache/$host/$1/index.html break;
}

# Check the path + .html
if (-f $document_root/cache/$host/$uri.html) {
  rewrite (.*) /cache/$host/$1.html break;
}

# Check directly
if (-f $document_root/cache/$host/$uri) {
  rewrite (.*) /cache/$host/$1 break;
}

This will look up cached files in /cache/$host/ and should send the files correctly. Be sure to add the nginx config before you send it to mongrel/thin/etc..


Tags: rails, caching, subdomain, cache_page, nginx



BlogKit, a Plugin to Add a Blog to your Rails App

Posted By Ryan Stout on June 05, 2010

It seems like every project I do needs a blog integrated into it. There are a few ways to setup a blog within an existing site, but I felt like they all were lacking in some large way. Here is what I or others have done to get a blog integrated into an existing site.

Use Twitter

This seems like a popular option for people to update users on their applications, while it works, there are some serious limitations.

Upsides:

  • Super easy
  • fairly well understood

Downsides:

  • 144 character limit
  • different domain, so little value on search engine ranking
  • different look
  • no real comments

Use WordPress.com or other hosted service

This also seems common because it is for the most part easy.

Upsides:

  • Still fairly easy
  • Shared authentication system with other blogs

Downsides:

  • different domain
  • different look
  • different authentication system for comments/posting

Use WordPress Locally

While there are a few Rails blog apps, most of them are lacking and don't have much real support in the rails community, which leaves someone looking to other languages if they want to install a blog app locally.

Again, the common blog app people reach for is WordPress, love it or hate it, its the standard. However, installing locally still leaves us with some issues.

Upsides:

  • Good community support
  • Can be integrated with same look and authentication

Downsides:

  • Requires duplicating layout code/resources to get same look
  • Sharing authentication must be done at the database level
  • Sharing authentication is difficult
  • Requires installing Php

Can't we have it all?

From my point of view, every solution to integrate a blog into an existing rails app is either severely limited, or requires way too much time to setup. Having written a few rails plugins before I thought there has to be a rails plugin that lets me add a blog to my existing rails app. After searching, I was only able to find one called Bloggity, but it seemed to have some limitations:

  • requires attachment_fu
  • not being actively developed
  • doesn't use existing layout or authentication (by default at least)
  • not easy to integrate (imho)

What I think we need

So after looking for a while and using WordPress.com on a few projects, I finally decided I needed to take the time to build a rails blog plugin that meets my requirements.

My goal was to limit the dependencies, and have it degrade gracefully if you don't have a required dependency. So for example, I handle image uploading with paperclip, however if you don't have or want to install paperclip, it disables image uploading. I want it to be easy to install, integrate with your existing layouts and authentication if they are setup in a common way (via authlogic or restful_authenticaiton for example)

I figure to make it easy and useful, it should:

  • Use existing user model and before filters (based on conventions - requre_user, current_user)
  • Use existing application layout (with option to customize)
  • Have easily customizable CSS
  • Have option for markdown (default) or html

Thinking some more I decided that there were a few other features that I thought would be good and that most blogs have:

  • Search engine friendly urls
  • Comments via existing user model
  • Highlights code (with ultraviolet - optional)
  • Akismet (spam filtering) Support (optional)
  • Image and Gravatar support
  • Anonymous Comments (optional)
  • Atom Feeds
  • Tags
  • S3 Image Upload support

After a few weeks of work, I'm happy to present BlogKit

BlogKit

BlogKit supports rails 2 and 3 (via rails 3 branch), ruby 1.8.x and ruby 1.9.

Installing BlogKit is as easy as:

./script/plugin install http://github.com/ryanstout/blog_kit.git
rake db:migrate

or for rails3

./script/plugin install http://github.com/ryanstout/blog_kit.git -r rails3
rake db:migrate

For authentication, having a require_user before filter and a current_user method have become somewhat convention. If you are using something else, you can alias your existing methods and BlogKit will work.

The only dependency is will_paginate, however I'll be removing this requirement shortly.

When you install the plugin, it runs the blog_assets generator and copies over css files and a config file. Everything else stays in the plugin and can be over-ridden by copying it into the /app dir.

A settings file is created at config/blog_kit.yml that lets you customize how the plugin should work.

Once you've set it up, you can see the blog at /blog_posts

If your user model returned by current_user responds true to #admin? then you will see the links to create manage blog posts.

Once thats done you should have a fully functioning blog. You can see public/stylesheets/blog_kit.css for ways to customize the layout of the blog. You can specify a layout just for the blog pages in config/blog_kit.yml, if you want to add a side panel, you can do that there and use the <%= blog_tags_list %> tag to add the list of tags to the layout.

Status

While this project is still early, it should be ready for anyone to drop a blog into their app quickly. Please post any issues to the github page and fork the project and add features as needed.

See the github page for more info


Tags: plugins, rails, blogkit, blog