Module: EdgeRider::Scoped

Extended by:
Scoped
Included in:
Scoped
Defined in:
lib/edge_rider/scoped.rb

Constant Summary collapse

VALID_FIND_OPTIONS =
[ :conditions, :include, :joins, :limit, :offset,
:order, :select, :readonly, :group, :having, :from, :lock ]

Instance Method Summary collapse

Instance Method Details

#scoped(options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/edge_rider/scoped.rb', line 7

def scoped(options = nil)
  options ||= {}
  relation = all
  
  options.assert_valid_keys(VALID_FIND_OPTIONS)
  finders = options.dup
  finders.delete_if { |key, value| value.nil? && key != :limit }
  
  ((VALID_FIND_OPTIONS - [:conditions, :include]) & finders.keys).each do |finder|
    relation = relation.send(finder, finders[finder])
  end
  
  relation = relation.where(finders[:conditions]) if options.has_key?(:conditions)
  relation = relation.includes(finders[:include]) if options.has_key?(:include)
  
  relation
end