Module: Sequel::Plugins::Polymorphic::DatasetMethods

Defined in:
lib/sequel/plugins/polymorphic.rb

Instance Method Summary collapse

Instance Method Details

#where(*cond, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sequel/plugins/polymorphic.rb', line 14

def where(*cond, &block)
  if cond.first.is_a? Hash
    cond.first.clone.each do |key, value|
      # Does it respnd to #pk ? Then it's a model
      next unless value.respond_to?("pk")
      next unless self.model.association_reflections.key?(key.to_sym)
      next unless self.model.association_reflections[key.to_sym].key?(:polymorphic)
      next unless self.model.association_reflections[key.to_sym][:polymorphic]

      cond.first.delete(key)
      cond.first["#{key}_id".to_sym] = value.pk
      cond.first["#{key}_type".to_sym] = value.class.to_s
    end
  end

  super
end