Class: Polymorph::Relation

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/polymorph/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(query, source_types:, fields:) ⇒ Relation

Returns a new instance of Relation.



4
5
6
7
# File 'lib/polymorph/relation.rb', line 4

def initialize(query, source_types:, fields:)
  @source_types, @fields = source_types, fields
  super(query)
end

Instance Method Details

#countObject



9
10
11
# File 'lib/polymorph/relation.rb', line 9

def count
  self.to_a.length
end

#pluck(field) ⇒ Object



13
14
15
# File 'lib/polymorph/relation.rb', line 13

def pluck(field)
  self.to_a.map(&field)
end

#where(hash = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/polymorph/relation.rb', line 17

def where(hash = {})
  fields = hash.slice(*@fields)
  clause = fields.map do |field|
    %{(
      #{@source_types.map { |type| "#{type}.#{field[0]} = :#{field[0]}" }.join(" OR ")}
    )}
  end.join(" AND ")
  rebuild(super(clause, fields))
end