Class: PartialKs::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/partial_ks/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Table

Returns a new instance of Table.



6
7
8
# File 'lib/partial_ks/table.rb', line 6

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/partial_ks/table.rb', line 3

def model
  @model
end

Instance Method Details

#candidate_parent_classesObject

NB: can’t do polymorphic for now, rails errors on reflection#klass see, e.g. github.com/rails/rails/issues/15833



22
23
24
# File 'lib/partial_ks/table.rb', line 22

def candidate_parent_classes
  non_nullable_reflections.reject(&:polymorphic?).map(&:klass)
end

#inferred_parent_classObject



10
11
12
13
14
15
16
17
18
# File 'lib/partial_ks/table.rb', line 10

def inferred_parent_class
  if candidate_parent_classes.empty?
    nil
  elsif candidate_parent_classes.size == 1
    candidate_parent_classes.first
  else
    MultiParent.new(candidate_parent_classes)
  end
end

#relation_for_associated_model(klass) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/partial_ks/table.rb', line 26

def relation_for_associated_model(klass)
  association = model.reflect_on_all_associations.find {|assoc| assoc.class_name == klass.name}
  raise "#{filter_condition.name} not found in #{model.name} associations" if association.nil?

  case association.macro
  when :belongs_to
    model.where(association.foreign_key => [0, *klass.pluck(:id)])
  when :has_many
    model.where(model.primary_key => [0, *klass.pluck(association.foreign_key)])
  else
    raise "Unknown macro"
  end
end