7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 7
def self.included(base)
base.class_eval do
include ClassMethods
alias_method_chain :order, :dynamic_columns
alias_method_chain :where, :dynamic_columns
alias_method_chain :build_arel, :dynamic_columns
def dynamic_column_process_arel_nodes(rel, scope, index, joins)
case rel
when Arel::Nodes::Grouping
dynamic_column_process_arel_nodes(rel.expr, scope, index+1, joins)
when Arel::Nodes::Or
dynamic_column_process_arel_nodes(rel.left, scope, index+1, joins)
dynamic_column_process_arel_nodes(rel.right, scope, index+10000, joins) when Arel::Nodes::And
dynamic_column_process_arel_nodes(rel.left, scope, index+1, joins)
dynamic_column_process_arel_nodes(rel.right, scope, index+10000, joins) when Arel::Nodes::Descending
col_name = rel.expr.name
dynamic_type = rel.expr.relation.engine
rel.expr.relation = dynamic_column_build_arel_joins(col_name, dynamic_type, scope, index+1, joins)[:table]
rel.expr.name = :value
when Arel::Nodes::Ascending
col_name = rel.expr.name
dynamic_type = rel.expr.relation.engine
rel.expr.relation = dynamic_column_build_arel_joins(col_name, dynamic_type, scope, index+1, joins)[:table]
rel.expr.name = :value
else
if rel.left.relation.respond_to?(:engine)
col_name = rel.left.name
dynamic_type = rel.left.relation.engine.to_s
res = dynamic_column_build_arel_joins(col_name, dynamic_type, scope, index+1, joins)
rel.left.relation = res[:table]
rel.left.name = res[:column] || :value end
end
end
def dynamic_column_build_arel_joins(col_name, dynamic_type, scope, index, joins)
col_name = col_name.to_s
field_scope = scope
field_scope_id = (!field_scope.nil?) ? field_scope.id : nil
field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil
joins_scope_key = "#{field_scope_type}_#{field_scope_id}"
joins[joins_scope_key] ||= {}
column_datum_store_table_type = "HasDynamicColumns::DynamicColumnStringDatum"
if !field_scope.nil? && a = field_scope.activerecord_dynamic_columns.where(key: col_name).first
column_datum_store_table_type = "HasDynamicColumns::DynamicColumn#{a.data_type.to_s.capitalize}Datum"
end
column_table = HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_where_#{index}_#{col_name}")
column_datum_table = HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_where_data_#{index}_#{col_name}")
column_datum_store_table = column_datum_store_table_type.constantize.arel_table.alias("dynamic_where_data_store_#{index}_#{col_name}")
if !joins[joins_scope_key][col_name].nil?
return joins[joins_scope_key][col_name]
end
on_query = column_table[:key].eq(col_name)
on_query = on_query.and(
column_table[:field_scope_type].eq(field_scope_type)
) unless field_scope_type.nil?
on_query = on_query.and(
column_table[:field_scope_id].eq(field_scope_id)
) unless field_scope_id.nil?
column_table_join_on = column_table
.create_on(
on_query
)
join_scope_type = (field_scope_id.nil?)? Arel::Nodes::OuterJoin : Arel::Nodes::InnerJoin
column_table_join = table.create_join(column_table, column_table_join_on)
self.joins_values += [column_table_join]
column_table_datum_join_on = column_datum_table
.create_on(
column_datum_table[:owner_id].eq(table[:id]).and(
column_datum_table[:owner_type].eq(dynamic_type.to_s)
).and(
column_datum_table[:dynamic_column_id].eq(column_table[:id])
)
)
column_table_datum_join = table.create_join(column_datum_table, column_table_datum_join_on, join_scope_type)
self.joins_values += [column_table_datum_join]
column_table_datum_store_join_on = column_datum_store_table
.create_on(
column_datum_table[:datum_id].eq(column_datum_store_table[:id]).and(
column_datum_table[:datum_type].eq(column_datum_store_table_type)
)
)
column_table_datum_store_join = table.create_join(column_datum_store_table, column_table_datum_store_join_on, join_scope_type)
self.joins_values += [column_table_datum_store_join]
column_name = :value
if !field_scope.nil? && assoc = field_scope.activerecord_dynamic_columns.where(data_type: "model", key: col_name).first
assoc_table = assoc.class_name.constantize.arel_table.alias("dynamic_where_associated_data_#{index}_#{col_name}")
join_on = assoc_table.create_on(
column_datum_store_table[:value_id].eq(assoc_table[:id]).and(
column_datum_store_table[:value_type].eq(assoc.class_name)
)
)
join = table.create_join(assoc_table, join_on)
self.joins_values += [join]
column_table_datum_store_join = join
column_datum_store_table = assoc_table
column_name = assoc.column_name.to_sym
end
joins[joins_scope_key][col_name] = {
:join => column_table_datum_store_join,
:table => column_datum_store_table,
:column => column_name
}
joins[joins_scope_key][col_name]
end
def build_dynamic_column_joins
joins = self.joins_dynamic_columns
self.where_dynamic_columns_values.each_with_index { |dynamic_scope, index_outer|
dynamic_scope[:where].each_with_index { |rel, index_inner|
dynamic_column_process_arel_nodes(rel, dynamic_scope[:scope], (index_outer*1000)+(index_inner*10000), joins)
self.where_values += [rel]
}
}
self.order_dynamic_columns_values.each_with_index { |dynamic_scope, index_outer|
dynamic_scope[:order].each_with_index { |rel, index_inner|
dynamic_column_process_arel_nodes(rel, dynamic_scope[:scope], (index_outer*1000)+(index_inner*10000), joins)
}
}
true
end
end
end
|