Class: ActiveRecordEx::NillableFind::NillableArel

Inherits:
Object
  • Object
show all
Defined in:
lib/active-record-ex/nillable_find.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, ids, parent_scope) ⇒ NillableArel

Returns a new instance of NillableArel.



4
5
6
7
8
# File 'lib/active-record-ex/nillable_find.rb', line 4

def initialize(base, ids, parent_scope)
  @base = base
  @ids = ids
  @parent_scope = parent_scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/active-record-ex/nillable_find.rb', line 10

def method_missing(method_name, *args, &block)
  return super unless  @base.respond_to? method_name

  normals = @base.where(id: @ids).send(method_name, *args)
  return normals unless @ids.include? nil

  used = @base.send(method_name, *args)
  # return those in normals AND those in parent scope not belonging to another
  normals.disjunct(used.relative_complement(@parent_scope))
end