Navigate/Search

Archive for the 'Ruby On Rails' Category

Rails 2.3 & Using Mongrel to deploy to a subdirectory (via –prefix)

Wednesday, March 25th, 2009

As many in the Rails community, our team has begin using the latest and greatest version of Ruby on Rails (2.3). We’ve been using v2.3 to develop our new Rails apps and everything is great. Great, that is, until we try to deploy to our application server.

You see, Our team runs multiple applications on the same server: apps.domain.com/app_1 apps.domain.com/app_2 etc..
We launch mongrel with something like:

  mongrel_rails start --prefix /app_1

This has always worked for us, but when we tried to deploy an application with Rails 2.3 we ran into a problem. A big one. Buried down in Mongrel’s code, the following code lies:

  ActionController::AbstractRequest.relative_url_root = ops[:prefix] if ops[:prefix]

This code hooks into Rails, telling it to add the subdirectory that you’re deploying to (app_1, app_2, etc) to the paths that Rails generates for you (root_url and the like). The problem is ActionController::AbstractRequest has been removed from Rails 2.3.

Seeing as the last update to Mongrel was over a year ago, we’re not very optimistic of the Mongrel team getting a fix for this out anytime soon. We also, can’t simply patch our version of mongrel, because it is also used by Rails apps around from before v2.3. Instead, we decided to temporarily hack around the problem.

I present abstract_request.rb

module ActionController
  class AbstractRequest
    def relative_url_root=(path)
      ActionController::Base.relative_url_root = path
    end
    def relative_url_root
      ActionController::Base.relative_url_root
    end
  end
end

Placing this little guy the lib/action_controller/ directory of your rails app will gracefully allow Mongrel to still make its call to AbstractRequest, hopefully refraining from breaking anything else.
So if you’re like us and are deploying multiple rails apps to subdirectories, or even if you just want to deploy a single rails app to a subdirectory, you’re likely going to run into this problem.

ActsAsCSVable v1.0

Monday, September 15th, 2008

It’s finally here: ActsAsCSVable!

Important Update! on June 25th, 2009
ActsAsCSVable is now on GitHub.com and all development has moved to git.
http://github.com/pjleonhardt/ActsAsCSVable/tree/master
Git Repository: git://github.com/pjleonhardt/ActsAsCSVable.git

History
I developed a plugin while working as a Rails Developer, CSVExportable, which was originally based off of some code from Bryan Helmkamp.

CSVExportable was good, but it was an evolutionary design. As such, it was hodgepodged together with no since of organization. I decided to rewrite the plugin from scratch, and design it a little bit better this time around (since I knew where I would end up this time around). It also needed a name change, since it does both CSV exporting and importing. I’ve seen a few Rails plugins that will do CSV exporting, but I haven’t found one that does both, or even importing. Importing is definitely tricky, but I think that if you follow a few guidelines, it can be useful in the right situations.

Installation
Subversion: Trunk | Stable v1.0 | Development Application
Project Management: Redmine

Usage

   #in your model
    acts_as_csv_exportable :fancy_naming, [{:first => "first_name"}, {:last => "last_name"}, {:email => "email_address"}, {:address => "mailing_address"}]
    acts_as_csv_exportable :detailed, [:first_name, :last_name, :email_address, :mailing_address, :formatted_date]
    acts_as_csv_exportable :default, [:id, :first_name, :last_name]

    acts_as_csv_importable :default, [:id, :first_name, :last_name]
    acts_as_csv_importable :new_projects, [:name, :details, :\owner_username]

    def formatted_date
      self.date.strftime("%Y/%M/%D")
    end

    def owner_username=(username)
      self.owner = Users.find_by_username(username)
    end

  # your contrller
  def index
    @people = Person.find(:all)

    respond_to do |wants|
      wants.csv { render :text => @people.to_csv(:columns => [:first_name, :last_name, :date_of_birth]) }
      # or
      wants.csv { render :text => @people.to_csv(:template => :fancy) }
      # or
      wants.csv { render :text => @peope.to_csv } #renders the :default template
    end
  end

  #/people/import
  def import
    file = params[:csv_uplodad]
    template = params[:upload_template]
    projects = Project.from_csv(file, template)

    if projects.all?(&:valid?)
      projects.each(&:save)
    else
      # Options options options...
      # 1) Save valid rows, and re-export invalid rows
      # 2) Save nothing, and tell user which rows were invalid
      # 3) Save nothing and tell them rows are invalid
      # 4) Up to you!
    end
  end

I only ask that if you use the plugin, let me know! I love to keep track of how its being used. The more response, the more likely I am to improve it and write more plugins! Leave a message below, or send me an email. I’d love to hear your thoughts for improvement, as well.

Introducing CSVExportable

Wednesday, August 20th, 2008

This plugin has been replaced by ActsAsCSVExportable, check it out!

I am finally releasing my first Rails Plugin: CSVExportable !

As many great things in the Rails community (and Rails itself) this plugin was born out of necessity. The need of the plugin in my projects defined its functionality and the design decisions taken to make it. Of course the project is open source, and free to modify and redistribute with credits intact.

Current version: 0.9
Download (SVN): Stable 0.9
Follow: (more…)

Web Applications with RoR

Tuesday, October 2nd, 2007

For the next 60 days, sitepoint.com is giving away their 464 page e-book (also available in print for $10 off in the next 60 days) called “Build Your Own Ruby on Rails Web Applications.” I’ve heard a lot of good things about the book. I just downloaded my copy and look forward to reading it.