Module: HasDynamicColumns::ActiveRecordRelation
Defined Under Namespace
Modules: WhereChainCompatibility
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/has_dynamic_columns/active_record_relation.rb', line 3
def self.included(base)
base.class_eval do
end
base.instance_eval do
end
end
|
Instance Method Details
#where(opts = :chain, *rest) ⇒ Object
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
|
# File 'lib/has_dynamic_columns/active_record_relation.rb', line 20
def where(opts = :chain, *rest)
if opts == :chain
scope = spawn
scope.extend(WhereChainCompatibility)
chain = ::ActiveRecord::QueryMethods::WhereChain.new(scope)
chain.instance_eval do
def dynamic(opts, *rest)
options = rest.
field_scope = opts.respond_to?(:class) && opts.class.respond_to?(:ancestors) && (opts.class.ancestors.include?(::ActiveRecord::Base)) ? opts : nil
if !field_scope.nil?
opts = options
rest = []
end
field_scope_id = (!field_scope.nil?) ? field_scope.id : nil
field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil
@scope.joins_values += @scope.send(:build_where, opts, rest).map do |rel|
dynamic_type = rel.left.relation.engine.to_s
col_name = rel.left.name
value = rel.right
table = dynamic_type.constantize.arel_table
column_table = ::HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_where_#{col_name}")
on_query = column_table[:key].eq(col_name)
if !field_scope_type.nil?
on_query = on_query.and(
column_table[:field_scope_type].eq(field_scope_type)
)
end
if !field_scope_id.nil?
on_query = on_query.and(
column_table[:field_scope_id].eq(field_scope_id)
)
end
column_table_join_on = column_table.create_on(on_query)
table.create_join(column_table, column_table_join_on)
end unless field_scope.nil?
join_value = @scope.send(:build_where, opts, rest).map do |rel|
dynamic_type = rel.left.relation.engine.to_s
col_name = rel.left.name
value = rel.right
table = dynamic_type.constantize.arel_table
column_table = ::HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_where_#{col_name}")
column_datum_table = ::HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_where_data_#{col_name}")
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)
).and(
column_datum_table[:dynamic_column_id].eq(column_table[:id])
).and(
column_datum_table[:value].matches("%"+value+"%")
)
)
table.create_join(column_datum_table, column_table_datum_join_on)
end
@scope.joins_values += join_value
@scope
end
end
chain
else
super
end
end
|