Fake_arel: Rails 3 Query Interface for Rails 2

github.com/gammons/fake_arel

DESCRIPTION:

  • Tired of waiting for Rails 3 and its new super-sweet query interface? Try fake_arel!

  • Fake_arel is a gem that will simulate the new Rails 3 query interface, using named_scopes and a couple small patches to ActiveRecord.

  • This should serve as a nice bridge between Rails 2 and Rails 3 apps, and can be removed once upgrading your app to rails 3, and everything (hopefully) should still work.

SYNOPSIS:

  • All the finders described on Pratik’s blog have been implemented. (m.onkey.org/2010/1/22/active-record-query-interface)

    Reply.where(:id => 1)
    Reply.select("content,id").where("id > 1").order("id desc").limit(1)
    Topic.joins(:replies).limit(1)
    
  • Additionally, named_scopes are very similar to Rails 3 relations, in that they are lazily loaded.

    >> Reply.where(:name => "John").class
    ActiveRecord::NamedScope::Scope
    
  • Also implemented was to_sql. to_sql will work on any chained query.

    >> Topic.joins(:replies).limit(1).to_sql
    "SELECT \"topics\".* FROM \"topics\"   INNER JOIN \"replies\" ON replies.topic_id = topics.id   LIMIT 1"
    
  • named_scope was modified to include other named_scopes, so you can chain them together.

    class Reply < ActiveRecord::Base
      named_scope :by_john, where(:name => "John")
      named_scope :recent, lambda {|t| where("created_at > ? ", t.minutes.ago) }
      named_scope :recent_by_john, recent(15).by_john
    end
    

REQUIREMENTS:

  • >= ActiveRecord 2.3.5

INSTALL:

  • gem install fake_arel

AUTHORS:

  • Grant Ammons

  • Sokolov Yura

LICENSE:

(The MIT License)

Copyright © 2010 Grant Ammons ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.