Module: HasDynamicColumns::ActiveRecord::QueryMethods

Defined in:
lib/has_dynamic_columns/active_record/query_methods.rb,
lib/has_dynamic_columns/active_record/v3/query_methods.rb,
lib/has_dynamic_columns/active_record/v4/query_methods.rb

Defined Under Namespace

Modules: ClassMethods, WhereChainCompatibility Classes: OrderChain, WhereChain

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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) # Hack - queries with over 10,000 dynamic where conditions may break
			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) # Hack - queries with over 10,000 dynamic where conditions may break
			# We can work with this
			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
			# We can work with this
			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
			# We can work with this
			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) # modify the where to use the aliased table

					rel.left.relation = res[:table]
					rel.left.name = res[:column] || :value # value is the data storage column searchable on dynamic_column_data table
				end
			end
		end

		# Builds the joins required for this dynamic column
		# Modifies the where to use the dynamic_column_data table alias
		#
		# rel - an arel node
		# scope - scope to run the conditions in
		# index - unique table identifier
		# joins - list of joins processed (to prevent duplicates)
		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}")

			# Join for this scope/col already added - continue
			if !joins[joins_scope_key][col_name].nil?
				return joins[joins_scope_key][col_name]
			end

			# Join on the column with the key
			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]

			# Join on all the data with the provided key
			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]

			# Join on the actual data
			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 this dynamic column points to another model we need to join on that table
			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

			# Track the joins
			# - So they can be referenced later
			# - So we don't make more joins than we have to
			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

		# Builds all the joins required for the dynamic columns in the where/order clauses
		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|
					# Process each where
					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|
					# Process each order
					dynamic_column_process_arel_nodes(rel, dynamic_scope[:scope], (index_outer*1000)+(index_inner*10000), joins)
				}
			}

			true
		end
	end
end

Instance Method Details

#build_arel_with_dynamic_columnsObject

When arel starts building - filter



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 183

def build_arel_with_dynamic_columns
	self.build_dynamic_column_joins

	if self.where_dynamic_columns_values.length > 0 || self.order_dynamic_columns_values.length > 0
		self.group_values += [Arel::Nodes::Group.new(table[:id])]
	end

	self.where_dynamic_columns_values_reset
	self.order_dynamic_columns_values_reset

	build_arel_without_dynamic_columns
end

#order_with_dynamic_columns(*args) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/has_dynamic_columns/active_record/v3/query_methods.rb', line 83

def order_with_dynamic_columns(*args)
	# Chain - by_dynamic_columns
	if args.empty?
		::ActiveRecord::QueryMethods::OrderChain.new(clone)
	else
		order_without_dynamic_columns(*args)
	end
end

#where_with_dynamic_columns(opts = :chain, *rest) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/has_dynamic_columns/active_record/v3/query_methods.rb', line 75

def where_with_dynamic_columns(opts = :chain, *rest)
	if opts == :chain
		::ActiveRecord::QueryMethods::WhereChain.new(clone)
	else
		where_without_dynamic_columns(opts, *rest)
	end
end