github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

thoughtbot / pacecar

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 241
    • 7
  • Source
  • Commits
  • Network (7)
  • Issues (1)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Generated scopes for ActiveRecord classes — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

allow _equals on all columns, not just text/string 
mjankowski (author)
Thu Dec 31 07:54:48 -0800 2009
commit  9ca6eaeedc1830ee9d858e727aac92943f9ed6eb
tree    edaa785a0e0a9aed391acad5f86a4662fa8d51d8
parent  cf2af211b1605b0747224de1a620664bb7206228
pacecar /
name age
history
message
file .gitignore Thu Aug 13 15:21:21 -0700 2009 ignore test.db [mjankowski]
file MIT-LICENSE Tue Oct 07 12:30:54 -0700 2008 add the MIT LICENSE [mjankowski]
file README.rdoc Mon Dec 21 10:58:57 -0800 2009 List MySQL and SQLite as supported databases. C... [tristandunn]
file Rakefile Sun Oct 05 20:01:50 -0700 2008 add first pass at boolean and datetime columns ... [mjankowski]
file init.rb Sun Oct 05 20:01:50 -0700 2008 add first pass at boolean and datetime columns ... [mjankowski]
directory lib/ Thu Dec 31 07:54:48 -0800 2009 allow _equals on all columns, not just text/string [mjankowski]
file pacecar.gemspec Thu Dec 31 07:13:54 -0800 2009 update gemspec [mjankowski]
directory test/ Thu Dec 31 07:03:55 -0800 2009 merge in changes to honor time zone if present [mjankowski]
README.rdoc

Pacecar

Pacecar adds named_scope methods and other common functionality to ActiveRecord classes via database column introspection.

Pacecar automatically includes the Pacecar::Helpers module into all ActiveRecord::Base classes.

To get all Pacecar functionality, you need to "include Pacecar" in your class.

To get some subset (for example, only the state functionality), you can do something like "include Pacecar::State" to get only the module(s) you want.

Usage

Assuming a database schema…

  class CreateSchema < ActiveRecord::Migration
    def self.up
      create_table :users, :force => true do |t|
        t.boolean :admin, :default => false, :null => false
        t.datetime :approved_at
        t.datetime :rejected_at
        t.string :first_name
        t.string :last_name
        t.text :description
        t.timestamps
      end
      create_table :posts, :force => true do |t|
        t.string :owner_type
        t.integer :owner_id
        t.string :publication_state
        t.string :post_type
        t.timestamps
      end
      create_table :comments, :force => true do |t|
        t.integer :user_id
        t.text :description
        t.timestamps
      end
    end
  end

And some basic model declarations…

  class User < ActiveRecord::Base
    include Pacecar
    has_many :posts, :as => :owner
    has_many :comments
    has_many :articles
    has_ranking :comments
    has_recent_records :comments
    has_recent_records :articles, :comments
  end

  class Post < ActiveRecord::Base
    include Pacecar
    PUBLICATION_STATES = %w(Draft Submitted Rejected Accepted)
    TYPES = %w(Free Open Private Anonymous PostModern)
    belongs_to :owner, :polymorphic => true
    has_state :publication_state
    has_state :post_type, :with => TYPES
    has_polymorph :owner
  end

  class Comment < ActiveRecord::Base
    include Pacecar
    belongs_to :user
  end

  class Article < ActiveRecord::Base
    belongs_to :user
  end

All columns

Records where approved_at is not null, or where it is null…

  User.approved_at_present
  User.approved_at_missing

Records where first_name is not null, or where it is null…

  User.first_name_present
  User.first_name_missing

Records ordered by first_name (default to ‘asc’, can specify to override)…

  User.by_first_name
  User.by_first_name(:asc)
  User.by_first_name(:desc)

Records where an attribute matches a search term (column LIKE "%term%")…

  User.first_name_matches('John')

Records where an attribute starts or ends with a search term…

  User.first_name_starts_with('A')
  User.first_name_ends_with('a')

Records where any non-state text or string column matches term…

  User.search_for('test')

Records where any of a list of columns match the term…

  User.search_for 'test', :on => [:first_name, :last_name]

Records where all of a list of columns match the term…

  User.search_for 'test', :on => [:first_name, :last_name], :require => :all

Boolean columns

Records that are all admins or non-admins…

  User.admin
  User.not_admin

The "balance" (count of true minus false for column in question)…

  User.admin_balance

Datetime columns

Records approved before or after certain times…

  User.approved_at_before(5.days.ago)
  User.approved_at_after(4.weeks.ago)

Records with approved_at in the past or future…

  User.approved_at_in_past
  User.approved_at_in_future

Records with approved_at inside or outside of two times…

  User.approved_at_inside(10.days.ago, 1.day.ago)
  User.approved_at_outside(2.days.ago, 1.day.ago)

Records with certain year, month or day…

  User.approved_at_in_year(2000)
  User.approved_at_in_month(01)
  User.approved_at_in_day(01)

Records with a duration (time delta between two columns) of, over or under a certain number of days…

  User.with_duration_of(14, :approved_at, :rejected_at)
  User.with_duration_over(14, :approved_at, :rejected_at)
  User.with_duration_under(14, :approved_at, :rejected_at)

Polymorphic relationships

Records which have an owner_type of User…

  Post.for_owner_type(User)

Associations

Records with the most and least associated records…

  User.maximum_comments
  User.minimum_comments

Records with associated records since a certain time…

  User.recent_comments_since(2.days.ago)
  User.recent_comments_and_posts_since(3.days.ago)
  User.recent_comments_or_posts_since(4.days.ago)

State columns

Records which are in a particular state, or not in a state…

  Post.publication_state_draft
  Post.post_type_not_open

Query methods on instances to check state…

  Post.first.publication_state_draft?
  Post.last.post_type_not_open?

Limits

First x records…

  User.limited(10)

Named scopes

Because these are all named_scope, you can combine them.

To get all users that have a first_name set, who are admins and approved more than 2 weeks ago, ordered by their first name…

  User.first_name_present.admin.approved_at_before(2.weeks.ago).by_first_name

To get the top 10 commenters…

  User.maximim_comments.limited(10)

Supported Databases

  • MySQL
  • SQLite

License

Pacecar is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server