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.



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

def initialize(arel)
  super(proc {})
  @statement_node_id = nil

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

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



75
76
77
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 75

def contexts
  @contexts
end

Instance Method Details

#visit_Arel_Attributes_Attribute(*args) ⇒ Object

rubocop:disable Naming/MethodName



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

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

  super(*args)
end

#visit_Arel_Nodes_Equality(obj, *args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 84

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

#visit_Arel_Nodes_OuterJoin(obj, _collector = nil) ⇒ Object Also known as: visit_Arel_Nodes_FullOuterJoin, visit_Arel_Nodes_RightOuterJoin

rubocop:disable Naming/MethodName



128
129
130
131
132
133
134
135
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 128

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

#visit_Arel_Nodes_SelectCore(obj, *_args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 109

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

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



103
104
105
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 103

def visit_Arel_Table(obj, _collector = nil)
  @current_context.visited_relation(obj) if tenant_relation?(MultiTenant::TableNode.table_name(obj))
end

#visit_MultiTenant_TenantEnforcementClause(obj) ⇒ Object



95
96
97
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 95

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

#visit_MultiTenant_TenantJoinEnforcementClause(obj) ⇒ Object



99
100
101
# File 'lib/activerecord-multi-tenant/query_rewriter.rb', line 99

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