Module: UtilityScopes::Except::ClassMethods

Defined in:
lib/utility_scopes/except.rb

Instance Method Summary collapse

Instance Method Details

#attach_except_utility_scopeObject

Must be a class method called directly on AR::Base subclasses so named_scope knows who its base class, and thus, table_name and primary_key are.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/utility_scopes/except.rb', line 27

def attach_except_utility_scope

  # Allow easy rejection of items. 
  # Can take an a single id or ActiveRecord object, or an array of them
  # Example: 
  #   before   Article.all.reject{|a| a == @bad_article }
  #   after    Article.except(@bad_article)
  named_scope :except, lambda { |item_or_list|
    # nil or empty array causes issues here with mysql
    item_or_list.blank? ? {} : {:conditions => ["#{quoted_table_name}.#{primary_key} NOT IN (?)", item_or_list]}
  }
end