Module: Scopiform::AssociationScopes::ClassMethods

Defined in:
lib/scopiform/association_scopes.rb

Instance Method Summary collapse

Instance Method Details

#reflection_added(_name, reflection) ⇒ Object



32
33
34
# File 'lib/scopiform/association_scopes.rb', line 32

def reflection_added(_name, reflection)
  setup_association_auto_scopes(reflection)
end

#scopiform_association_scope(association, method, value, ctx:) ⇒ Object



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

def scopiform_association_scope(association, method, value, ctx:)
  is_root = ctx.nil?
  ctx = ScopeContext.from(ctx)
  ctx.set(arel_table) if is_root || ctx.arel_table.blank?

  ctx.association = association
  ctx.build_joins

  applied = ctx.association.klass.send(method, value, ctx: ScopeContext.from(ctx).set(ctx.association_arel_table))

  if is_root
    ctx.scopes.reduce(joins(ctx.joins).merge(applied)) { |chain, scope| chain.merge(scope) }
  else
    ctx.scopes.reduce(all.merge(applied)) { |chain, scope| chain.merge(scope) }
  end
end