Class: MultiTenant::ArelTenantVisitor

Inherits:
Arel::Visitors::DepthFirst
  • Object
show all
Defined in:
lib/activerecord-multi-tenant/query_rewriter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arel) ⇒ ArelTenantVisitor

Returns a new instance of ArelTenantVisitor.



58
59
60
61
62
63
64
65
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 58

def initialize(arel)
  super(Proc.new {})
  @statement_node_id = nil

  @contexts = []
  @current_context = nil
  accept(arel.ast)
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



67
68
69
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 67

def contexts
  @contexts
end

Instance Method Details

#visit_Arel_Attributes_Attribute(*args) ⇒ Object



69
70
71
72
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 69

def visit_Arel_Attributes_Attribute(*args)
  return if @current_context.nil?
  super(*args)
end

#visit_Arel_Nodes_Equality(o, *args) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 74

def visit_Arel_Nodes_Equality(o, *args)
  if o.left.is_a?(Arel::Attributes::Attribute)
    table_name = o.left.relation.table_name
    model = MultiTenant.multi_tenant_model_for_table(table_name)
    @current_context.visited_handled_relation(o.left.relation) if model.present? && o.left.name.to_s == model.partition_key.to_s
  end
  super(o, *args)
end

#visit_Arel_Nodes_OuterJoin(o, collector = nil) ⇒ Object Also known as: visit_Arel_Nodes_FullOuterJoin, visit_Arel_Nodes_RightOuterJoin



112
113
114
115
116
117
118
119
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 112

def visit_Arel_Nodes_OuterJoin(o, collector = nil)
  nest_context(o) do
    @current_context.discover_relations do
      visit o.left
      visit o.right
    end
  end
end

#visit_Arel_Nodes_SelectCore(o, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 96

def visit_Arel_Nodes_SelectCore(o, *args)
  nest_context(o) do
    @current_context.discover_relations do
      visit o.source
    end
    visit o.wheres
    visit o.groups
    visit o.windows
    if defined?(o.having)
      visit o.having
    else
      visit o.havings
    end
  end
end

#visit_Arel_Table(o, _collector = nil) ⇒ Object Also known as: visit_Arel_Nodes_TableAlias



91
92
93
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 91

def visit_Arel_Table(o, _collector = nil)
  @current_context.visited_relation(o) if tenant_relation?(o.table_name)
end

#visit_MultiTenant_TenantEnforcementClause(o) ⇒ Object



83
84
85
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 83

def visit_MultiTenant_TenantEnforcementClause(o, *)
  @current_context.visited_handled_relation(o.tenant_attribute.relation)
end

#visit_MultiTenant_TenantJoinEnforcementClause(o) ⇒ Object



87
88
89
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 87

def visit_MultiTenant_TenantJoinEnforcementClause(o, *)
  @current_context.visited_handled_relation(o.tenant_attribute.relation)
end