Module: Bsm::Constrainable::Relation

Defined in:
lib/bsm/constrainable/relation.rb

Overview

Extension for ActiveRecord::Relation

Instance Method Summary collapse

Instance Method Details

#constrain(*args) ⇒ Object

Apply constraints. Example:

Post.constrain("created_at__lt" => "2011-01-01")

# Use "custom" constraints
Post.constrain(:custom, "created_at__lt" => "2011-01-01")

# Combine it with relations & scopes
Post.archived.includes(:author).constrain(params[:where]).paginate :page => 1


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bsm/constrainable/relation.rb', line 14

def constrain(*args)
  scope    = args.first.is_a?(Symbol) ? args.shift : nil
  filters  = args.last

  case filters
  when Bsm::Constrainable::FilterSet
    filters.merge(self)
  when Hash
    klass.constrainable(scope).filter(filters).merge(self)
  else
    self
  end
end