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: b54d204cd2d065b23eb81feaffc7e44b7939caf8
4
- data.tar.gz: 20cd3b0ecfa3d99190d86188405c385e0b20c1fa
3
+ metadata.gz: d0157fd29f68efb12148913e676a698b97cb7893
4
+ data.tar.gz: b93043b78dd717e0c0d42315931d287a06e16670
5
5
  SHA512:
6
- metadata.gz: 8766e626bcc171d0d1ca27f8e138b2cf98ea5852dc6fcbca58349060aa27941caa2888c343ed102dbc797da664a2e7bfbf86dc92c96dcc1534a4e0401880c7c6
7
- data.tar.gz: 4b9220bd040166462ba4ad4ae684908b71a6b8af46c0b9eec6d5a916b2781cd87be12a709294e7ad1bde506a85f983dd08248d704056879c2dce58511604bc6f
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
- query = query.order(order.definition.clause) if order
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
- result = base.joins(definition.joins).where(definition.qualified_column_name => value)
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.reorder('').joins(definition.joins).select(query).group(my_column)
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
- result = base.joins(definition.joins).where(definition.qualified_column_name => values)
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.reorder('').joins(definition.joins).select(query).group(my_column)
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).distinct
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
- .reorder('')
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)
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module FortyFacets
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forty_facets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Axel Tetzlaff