forty_facets 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0157fd29f68efb12148913e676a698b97cb7893
|
4
|
+
data.tar.gz: b93043b78dd717e0c0d42315931d287a06e16670
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b683cffff5b2154881af6d062bebb88e75f7f7c7d21dd07119645bb8a91067e37de4b20ad499f55132cf01a5a2fe785c73ab4b4f47795f1ed6d65c9a9b471848
|
7
|
+
data.tar.gz: 6c47c65896b8920c463e23438a335cc43d2c662718a613825987e261f57302ea3dd7767e0f36eb0f318c08f2b6ff7d903f093c55f417a780f56cc14b1cc033ca
|
@@ -115,11 +115,16 @@ module FortyFacets
|
|
115
115
|
@orders.find(&:active)
|
116
116
|
end
|
117
117
|
|
118
|
-
def result
|
119
|
-
query = @filters.inject(root) do |previous, filter|
|
118
|
+
def result(skip_ordering: false)
|
119
|
+
query = @filters.reject(&:empty?).inject(root) do |previous, filter|
|
120
120
|
filter.build_scope.call(previous)
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
|
+
if order && !skip_ordering
|
124
|
+
query = order.apply(query)
|
125
|
+
else
|
126
|
+
query = query.distinct
|
127
|
+
end
|
123
128
|
query
|
124
129
|
end
|
125
130
|
|
@@ -27,7 +27,7 @@ module FortyFacets
|
|
27
27
|
|
28
28
|
class AssociationFacetFilter < FacetFilter
|
29
29
|
def selected
|
30
|
-
@selected ||= definition.association.klass.find(values)
|
30
|
+
@selected ||= definition.association.klass.find(Array.wrap(values).reject(&:blank?))
|
31
31
|
end
|
32
32
|
|
33
33
|
def remove(entity)
|
@@ -57,19 +57,14 @@ module FortyFacets
|
|
57
57
|
def build_scope
|
58
58
|
return Proc.new { |base| base } if empty?
|
59
59
|
Proc.new do |base|
|
60
|
-
|
61
|
-
if definition.joins
|
62
|
-
result = result.distinct
|
63
|
-
end
|
64
|
-
|
65
|
-
result
|
60
|
+
base.joins(definition.joins).where(definition.qualified_column_name => value)
|
66
61
|
end
|
67
62
|
end
|
68
63
|
|
69
64
|
def facet
|
70
65
|
my_column = definition.qualified_column_name
|
71
66
|
query = "#{my_column} AS facet_value, count(#{my_column}) AS occurrences"
|
72
|
-
counts = without.result
|
67
|
+
counts = without.result(skip_ordering: true).distinct.joins(definition.joins).select(query).group(my_column)
|
73
68
|
counts.includes_values = []
|
74
69
|
facet = counts.map do |c|
|
75
70
|
is_selected = selected.include?(c.facet_value)
|
@@ -100,19 +95,14 @@ module FortyFacets
|
|
100
95
|
def build_scope
|
101
96
|
return Proc.new { |base| base } if empty?
|
102
97
|
Proc.new do |base|
|
103
|
-
|
104
|
-
if definition.joins
|
105
|
-
result = result.distinct
|
106
|
-
end
|
107
|
-
|
108
|
-
result
|
98
|
+
base.joins(definition.joins).where(definition.qualified_column_name => values)
|
109
99
|
end
|
110
100
|
end
|
111
101
|
|
112
102
|
def facet
|
113
103
|
my_column = definition.qualified_column_name
|
114
104
|
query = "#{my_column} AS foreign_id, count(#{my_column}) AS occurrences"
|
115
|
-
counts = without.result
|
105
|
+
counts = without.result(skip_ordering: true).distinct.joins(definition.joins).select(query).group(my_column)
|
116
106
|
counts.includes_values = []
|
117
107
|
entities_by_id = definition.association.klass.find(counts.map(&:foreign_id)).group_by(&:id)
|
118
108
|
|
@@ -136,7 +126,7 @@ module FortyFacets
|
|
136
126
|
|
137
127
|
matches_from_facet = base.joins(definition.joins).where("#{definition.association.klass.table_name}.#{definition.association.klass.primary_key}" => values).select(primary_key_column)
|
138
128
|
|
139
|
-
base.joins(definition.joins).where(primary_key_column => matches_from_facet)
|
129
|
+
base.joins(definition.joins).where(primary_key_column => matches_from_facet)
|
140
130
|
end
|
141
131
|
end
|
142
132
|
|
@@ -145,8 +135,8 @@ module FortyFacets
|
|
145
135
|
join_name = [definition.association.name.to_s, base_table.to_s].sort.join('_')
|
146
136
|
foreign_id_col = definition.association.name.to_s.singularize + '_id'
|
147
137
|
my_column = join_name + '.' + foreign_id_col
|
148
|
-
counts = without.result
|
149
|
-
.
|
138
|
+
counts = without.result(skip_ordering: true)
|
139
|
+
.distinct
|
150
140
|
.joins(definition.joins)
|
151
141
|
.select("#{my_column} as foreign_id, count(#{my_column}) as occurrences")
|
152
142
|
.group(my_column)
|
data/lib/forty_facets/order.rb
CHANGED
@@ -11,6 +11,38 @@ module FortyFacets
|
|
11
11
|
new_params[:order] = definition.request_value
|
12
12
|
search.class.new_unwrapped(new_params, search.root)
|
13
13
|
end
|
14
|
+
|
15
|
+
def apply(query)
|
16
|
+
if [Symbol, String, Hash].include? definition.clause.class
|
17
|
+
query.order(definition.clause)
|
18
|
+
elsif definition.clause.is_a? Array # [:person, :first_name]
|
19
|
+
# new and unelegant handling of ordering by columns from joined tables
|
20
|
+
root_table = search.class.root_class.table_name
|
21
|
+
primary_key = search.class.root_class.primary_key
|
22
|
+
just_ids = query.select("#{root_table}.#{primary_key}")
|
23
|
+
path_to_order_property = definition.clause
|
24
|
+
order_class = path_to_order_property
|
25
|
+
.take(path_to_order_property.length - 1)
|
26
|
+
.inject(search.class.root_class) do |prev_class, assoc_name|
|
27
|
+
prev_class.reflect_on_association(assoc_name).klass
|
28
|
+
end
|
29
|
+
joins = path_to_order_property
|
30
|
+
.reverse
|
31
|
+
.drop(1)
|
32
|
+
.inject(nil) do |sum, elem|
|
33
|
+
if sum
|
34
|
+
{elem => sum}
|
35
|
+
else
|
36
|
+
elem
|
37
|
+
end
|
38
|
+
end
|
39
|
+
search.root
|
40
|
+
.joins(joins)
|
41
|
+
.where(search.class.root_class.primary_key => just_ids)
|
42
|
+
.order("#{order_class.table_name}.#{path_to_order_property.last}")
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
14
46
|
end
|
15
47
|
end
|
16
48
|
|
data/lib/forty_facets/version.rb
CHANGED