Module: Placet::Rails::ActiveRecordScope

Defined in:
lib/placet/rails/active_record_scope.rb

Overview

ScopePlan → ActiveRecord::Relation の写像(concept.md 8.4 の集合演算を SQL に落とす)。 relation の scope 同士の構造差(joins の有無など)に依存しないよう、 和集合・差集合は主キーのサブクエリとして合成する

Class Method Summary collapse

Class Method Details

.compose(plan, relations, user, model) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/placet/rails/active_record_scope.rb', line 11

def compose(plan, relations, user, model)
  return model.none if plan.kind == "empty"

  pk = model.primary_key
  base =
    if plan.kind == "all"
      model.all
    else
      Placet.relation_scopes(relations, plan.include_relations, user)
            .map { |scope| model.where(pk => scope.select(pk)) }
            .reduce { |a, b| a.or(b) }
    end
  Placet.relation_scopes(relations, plan.exclude_relations, user)
        .reduce(base) { |rel, scope| rel.where.not(pk => scope.select(pk)) }
end