Class: Polymorph::Relation
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Polymorph::Relation
- Defined in:
- lib/polymorph/relation.rb
Instance Method Summary collapse
- #count ⇒ Object
-
#initialize(query, source_types:, fields:) ⇒ Relation
constructor
A new instance of Relation.
- #pluck(field) ⇒ Object
- #where(hash = {}) ⇒ Object
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
#count ⇒ Object
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 |