class Pickel::Condition

Attributes

column[R]
join_tables[R]
klass[R]
predicate[R]
target[R]

Public Class Methods

for(klass, key) click to toggle source
# File lib/pickel/condition.rb, line 6
def for(klass, key)
  s_key = key.to_s
  predicate = Predicate.find(s_key)

  cond = new(klass: klass, predicate: predicate)
  new_key = s_key.delete_suffix("_#{predicate.id}")
  cond.assign_column(new_key)
end
new(klass:, predicate:, column: nil, target: nil, join_tables: []) click to toggle source
# File lib/pickel/condition.rb, line 16
def initialize(klass:, predicate:, column: nil, target: nil, join_tables: [])
  @klass = klass
  @predicate = predicate
  @column = column
  @target = target
  @join_tables = join_tables
end

Public Instance Methods

assign_column(key, target: klass, join_tables: []) click to toggle source
# File lib/pickel/condition.rb, line 25
def assign_column(key, target: klass, join_tables: [])
  base = { klass: klass, predicate: predicate, target: target, join_tables: join_tables }
  return Condition.new(base.merge(column: key)) if target.attribute_names.include?(key)

  association = target.reflect_on_all_associations.find { |a| key.start_with?(a.name.to_s) }
  if association
    new_key = key.delete_prefix("#{association.name}_")
    join_tables << association.name
    new_target = association.klass

    assign_column(new_key, target: new_target, join_tables: join_tables)
  else
    Condition.new(base.merge(predicate: nil))
  end
end
build(value) click to toggle source
# File lib/pickel/condition.rb, line 41
def build(value)
  return klass.all if predicate.nil?

  rel = predicate.build(target, column, value)
  join_tables.empty? ? rel : join_relation.merge(rel)
end

Private Instance Methods

join_relation() click to toggle source
# File lib/pickel/condition.rb, line 50
def join_relation
  args = join_tables.reverse_each.inject { |a, b| { b => a } }
  klass.joins(args)
end