Context Aware Scope

An extension to ActiveRecord’s named scope

Usage

1. Add a context to a named_scope:


  named_scope :luxurious, :conditions => ['price > ?', 100], :context => {:price => 'luxurious'}
  named_scope :recent, :conditions => ['created_at > ?', 1.week.ago], :context => {:created_at => 'brand new'}

2. Execute the scopes:


  @products = Product.new.luxurious

3. And get the context:


  @products.context
  # => {:price => 'luxurious', :created_at => 'brand new'}
  "You are looking at #{@products.context.values.to_sentence} products"
  # => "You are looking at brand new and luxurious products"

Installation

Install the ContextAwareScope gem:


  gem install context_aware_scope

Compatibility Only tested with ActiveRecord 2.3.8. Rails 3 support planned.