Class: MultiTenant::ArelTenantVisitor

Inherits:
Object
  • 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.


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

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.


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

def contexts
  @contexts
end

Instance Method Details

#visit_Arel_Attributes_Attribute(*args) ⇒ Object


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

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

#visit_Arel_Nodes_Equality(o, *args) ⇒ Object


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

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


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

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


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

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


93
94
95
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 93

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

#visit_MultiTenant_TenantEnforcementClause(o) ⇒ Object


85
86
87
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 85

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

#visit_MultiTenant_TenantJoinEnforcementClause(o) ⇒ Object


89
90
91
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 89

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