card-mod-search 0.14.2 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -0
  3. data/assets/script/autocomplete.js.coffee +36 -0
  4. data/assets/script/search_box.js.coffee +25 -0
  5. data/assets/style/search.scss +69 -0
  6. data/config/locales/de.yml +4 -0
  7. data/config/locales/en.yml +4 -0
  8. data/data/files/mod_search_script_asset_output/file.js +4 -0
  9. data/data/real.yml +112 -0
  10. data/data/test.yml +18 -0
  11. data/lib/card/mod/search.rb +4 -0
  12. data/set/abstract/0_search/checkbox_item.haml +6 -0
  13. data/set/abstract/{02_search_params.rb → 0_search/search_params.rb} +2 -4
  14. data/set/abstract/{05_search → 0_search}/views.rb +23 -46
  15. data/set/abstract/{05_search.rb → 0_search.rb} +9 -9
  16. data/set/abstract/{06_cql_search.rb → cql_search.rb} +3 -6
  17. data/set/abstract/search_views.rb +1 -2
  18. data/set/abstract/virtual_search.rb +3 -0
  19. data/set/all/autocomplete/autocomplete_input.haml +3 -0
  20. data/set/all/autocomplete/autocomplete_item.haml +5 -0
  21. data/set/all/autocomplete.rb +48 -0
  22. data/set/right/by_create.rb +7 -0
  23. data/set/right/by_name.rb +7 -0
  24. data/set/right/by_update.rb +7 -0
  25. data/set/right/children.rb +8 -0
  26. data/set/right/count.rb +9 -0
  27. data/set/right/created.rb +8 -0
  28. data/set/right/creator.rb +7 -0
  29. data/set/right/edited.rb +8 -0
  30. data/set/right/editors.rb +8 -0
  31. data/set/right/last_edited.rb +7 -0
  32. data/set/right/linked_to_by.rb +8 -0
  33. data/set/right/links_to.rb +7 -1
  34. data/set/right/mates.rb +9 -1
  35. data/set/right/nested_by.rb +9 -1
  36. data/set/right/nests.rb +8 -0
  37. data/set/right/referred_to_by.rb +8 -0
  38. data/set/right/refers_to.rb +8 -0
  39. data/set/self/recent.rb +38 -4
  40. data/set/self/search/results_for_keyword.haml +5 -0
  41. data/set/self/search/search_box.haml +2 -0
  42. data/set/self/search.rb +88 -40
  43. data/set/type/cardtype.rb +12 -1
  44. metadata +63 -28
  45. data/lib/card/filter_query.rb +0 -78
  46. data/set/abstract/00_filter_helper.rb +0 -46
  47. data/set/abstract/03_filter/_filter_input.haml +0 -8
  48. data/set/abstract/03_filter/filter_form.haml +0 -56
  49. data/set/abstract/03_filter/filter_form.rb +0 -101
  50. data/set/abstract/03_filter/filtered_content.haml +0 -5
  51. data/set/abstract/03_filter/form_helper.rb +0 -105
  52. data/set/abstract/03_filter/query_construction.rb +0 -38
  53. data/set/abstract/03_filter/quick_filters.haml +0 -11
  54. data/set/abstract/03_filter/selectable_filtered_content.haml +0 -2
  55. data/set/abstract/03_filter.rb +0 -30
  56. data/set/abstract/05_search/checkbox_item.haml +0 -5
  57. data/set/abstract/05_search/select_item.haml +0 -15
  58. data/set/abstract/filter_list.rb +0 -11
  59. data/set/abstract/filterable.rb +0 -13
  60. data/set/abstract/filterable_bar.rb +0 -11
  61. data/set/right/follow.rb +0 -3
@@ -0,0 +1,7 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { creator_of: "_left" }
7
+ end
data/set/right/edited.rb CHANGED
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { edited_by: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Cards edited by {{_left|name}}."
3
11
  end
data/set/right/editors.rb CHANGED
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { editor_of: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Users who have edited {{_left|name}}."
3
11
  end
@@ -0,0 +1,7 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { last_edited_by: "_left" }
7
+ end
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { link_to: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Cards that link to {{_left|name}}."
3
11
  end
@@ -1,4 +1,10 @@
1
- # -*- encoding : utf-8 -*-
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { linked_to_by: "_left", sort_by: "name" }
7
+ end
2
8
 
3
9
  def raw_help_text
4
10
  "Cards that <em>{{_left|name}}</em> links to."
data/set/right/mates.rb CHANGED
@@ -1,3 +1,11 @@
1
- def raw_helo_text
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { plus: "_left", sort_by: "name" }
7
+ end
8
+
9
+ def raw_help_text
2
10
  "If there is a card named \"X+{{_left|name}}\", then X is a mate of {{_left|name}}."
3
11
  end
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
1
5
  def raw_help_text
2
- "Cards that include {{_left|name}}."
6
+ "Cards that nest {{_left|name}}."
7
+ end
8
+
9
+ def cql_content
10
+ { nest: "_left", sort_by: "name" }
3
11
  end
data/set/right/nests.rb CHANGED
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { nested_by: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Cards that {{_left|name}} includes."
3
11
  end
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { refer_to: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Cards that refer to {{_left|name}}."
3
11
  end
@@ -1,3 +1,11 @@
1
+ include_set Abstract::VirtualSearch
2
+
3
+ assign_type :search_type
4
+
5
+ def cql_content
6
+ { referred_to_by: "_left", sort_by: "name" }
7
+ end
8
+
1
9
  def raw_help_text
2
10
  "Cards that {{_left|name}} refers to."
3
11
  end
data/set/self/recent.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  ACTS_PER_PAGE = 25
2
+ MAX_ACTS_TO_SCAN = 10_000
2
3
 
3
4
  view :title do
4
5
  voo.title ||= "Recent Changes"
@@ -6,12 +7,17 @@ view :title do
6
7
  end
7
8
 
8
9
  def recent_acts
9
- action_relation = qualifying_actions.where "card_acts.id = card_act_id"
10
- Act.where("EXISTS (#{action_relation.to_sql})").order id: :desc
10
+ limiting_scan do
11
+ Act.joins(ar_actions: :ar_card).distinct
12
+ .where(Query::CardQuery.viewable_sql)
13
+ .where("draft is not true")
14
+ .order id: :desc
15
+ end
11
16
  end
12
17
 
13
- def qualifying_actions
14
- Action.all_viewable.where "draft is not true"
18
+ def limiting_scan
19
+ min_id = Card::Act.maximum(:id) - MAX_ACTS_TO_SCAN
20
+ min_id.positive? ? yield.where("card_acts.id > #{min_id}") : yield
15
21
  end
16
22
 
17
23
  format :html do
@@ -27,3 +33,31 @@ format :rss do
27
33
  render_blank
28
34
  end
29
35
  end
36
+
37
+ # *Strat 1: WHERE EXISTS*
38
+ # ~~~~~~~~~~~~~~~~~~~~~~~
39
+ # SELECT `card_acts`.* FROM `card_acts` WHERE EXISTS (
40
+ # SELECT `card_actions`.*
41
+ # FROM `card_actions`
42
+ # INNER JOIN `cards` ON `cards`.`id` = `card_actions`.`card_id`
43
+ # WHERE (draft is not true)
44
+ # AND (card_acts.id = card_act_id)
45
+ # ) ORDER BY `card_acts`.`id` DESC LIMIT 20;
46
+ #
47
+ #
48
+ # *Strat 2: INNER JOIN*
49
+ # ~~~~~~~~~~~~~~~~~~~~~
50
+ # SELECT DISTINCT `card_acts`.* FROM `card_acts`
51
+ # JOIN `card_actions` ON card_acts.id = card_act_id
52
+ # JOIN `cards` ON `cards`.`id` = `card_actions`.`card_id`
53
+ # WHERE (draft is not true)
54
+ # ORDER BY `card_acts`.`id` DESC LIMIT 20;
55
+
56
+ # 11:33
57
+ # first run times:
58
+ # mysql 5.7:
59
+ # - strat 1: 0.22 sec (possibly in cache already?)
60
+ # - strat 2: 3min 17 sec
61
+ # mysql 8.0
62
+ # - strat 1: 2min 1 sec
63
+ # - strat 2: 0.01 sec
@@ -0,0 +1,5 @@
1
+ - if search_keyword.present?
2
+ .my-3.p-2.bg-light
3
+ = t :search_results_for
4
+ %span.search-keyword
5
+ = h search_keyword
@@ -0,0 +1,2 @@
1
+ .d0-search-box
2
+ = search_form { search_box_contents }
data/set/self/search.rb CHANGED
@@ -1,9 +1,13 @@
1
+ def match_start_only?
2
+ Card.config.search_box_match_start_only
3
+ end
4
+
1
5
  format do
2
- view :search_error, cache: :never do
3
- sr_class = search_with_params.class.to_s
6
+ delegate :match_start_only?, to: :card
4
7
 
8
+ view :search_error, cache: :never do
5
9
  # don't show card content; not very helpful in this case
6
- %(#{sr_class} :: #{search_with_params.message})
10
+ %(#{search_with_params.class} :: #{search_with_params.message})
7
11
  end
8
12
 
9
13
  def search_with_params
@@ -12,13 +16,11 @@ format do
12
16
 
13
17
  def cql_search
14
18
  query = card.parse_json_cql search_keyword
15
- rescuing_bad_query query do
16
- Card.search query
17
- end
19
+ rescuing_bad_query(query) { Card.search query }
18
20
  end
19
21
 
20
22
  def search_keyword
21
- @search_keyword ||= search_vars&.dig :keyword
23
+ @search_keyword ||= term_param || search_vars&.dig(:keyword)
22
24
  end
23
25
 
24
26
  def search_vars
@@ -28,65 +30,111 @@ format do
28
30
  def cql_keyword?
29
31
  search_keyword&.match?(/^\{.+\}$/)
30
32
  end
33
+
34
+ def complete_path
35
+ path mark: :search, view: :search_box_complete, format: :json
36
+ # path mark: :search, view: :test, format: :json
37
+ end
38
+
39
+ def results_path keyword
40
+ path mark: :search, query: { keyword: keyword }
41
+ end
31
42
  end
32
43
 
33
44
  format :html do
34
- view :title, cache: :never do
35
- return super() unless (title = keyword_search_title)
45
+ view :search_box, template: :haml, cache: :never
46
+
47
+ view :title do
48
+ voo.title ||= t(:search_results_title)
49
+ super()
50
+ end
36
51
 
37
- voo.title = title
52
+ view :core, cache: :never do
53
+ [render_results_for_keyword, super()]
38
54
  end
39
55
 
40
- def keyword_search_title
41
- search_keyword &&
42
- %(Search results for: <span class="search-keyword">#{h search_keyword}</span>)
56
+ view :results_for_keyword, cache: :never, template: :haml
57
+
58
+ def search_item term
59
+ autocomplete_item :search, icon_tag(:search), term
60
+ end
61
+
62
+ private
63
+
64
+ def search_form &block
65
+ form_tag path, method: "get", role: "search", class: classy("search-box-form"), &block
66
+ end
67
+
68
+ def search_box_contents
69
+ text_field_tag "query[keyword]", search_keyword,
70
+ class: "_search-box #{classy 'search-box'} form-control w-100",
71
+ autocomplete: :off,
72
+ data: { completepath: complete_path },
73
+ placeholder: t(:search_search_box_placeholder)
43
74
  end
44
75
  end
45
76
 
46
77
  format :json do
78
+ view :search_box_complete, cache: :never do
79
+ search_box_items :search_item, :add_item, :goto_items
80
+ end
81
+
47
82
  view :complete, cache: :never do
48
- term = term_param
49
- exact = Card.fetch term, new: {}
83
+ complete_or_match_search(start_only: match_start_only?).map do |name|
84
+ goto_item_label name
85
+ end
86
+ end
50
87
 
51
- {
52
- search: true,
53
- term: term,
54
- add: add_item(exact),
55
- new: new_item_of_type(exact),
56
- goto: goto_items(term, exact)
57
- }
88
+ private
89
+
90
+ def search_box_items *methods
91
+ [].tap do |items|
92
+ each_search_box_item methods do |action, value, label, data|
93
+ items << data.merge(action: action, value: value, label: label)
94
+ end
95
+ end
58
96
  end
59
97
 
60
- def add_item exact
61
- return unless exact.new_card? &&
62
- exact.name.valid? &&
63
- !exact.virtual? &&
64
- exact.ok?(:create)
98
+ def each_search_box_item methods, &block
99
+ term = search_keyword
100
+ exact = Card.fetch term, new: {}
101
+ methods.map { |method| send method, term, exact, &block }
102
+ end
65
103
 
66
- [h(exact.name), ERB::Util.url_encode(exact.name)]
104
+ def search_item term, _exact
105
+ yield :search, term, card.format.search_item(term), url: card_url(results_path(term))
67
106
  end
68
107
 
69
- def new_item_of_type exact
70
- return unless (exact.type_id == CardtypeID) &&
71
- Card.new(type_id: exact.id).ok?(:create)
108
+ def add_item term, exact
109
+ return unless exact.add_autocomplete_ok?
72
110
 
73
- [exact.name, "new/#{exact.name.url_key}"]
111
+ fmt = exact.format
112
+ yield :add, term, fmt.render_add_autocomplete_item,
113
+ url: card_url(fmt.add_autocomplete_item_path)
74
114
  end
75
115
 
76
- def goto_items term, exact
77
- goto_names = complete_or_match_search start_only: Card.config.navbox_match_start_only
78
- goto_names.unshift exact.name if add_exact_to_goto_names? exact, goto_names
79
- goto_names.map do |name|
80
- [name, name.to_name.url_key, h(highlight(name, term, sanitize: false))]
116
+ def goto_items _term, exact
117
+ map_goto_items exact do |item|
118
+ yield :goto, item, goto_item_label(item), url: path(mark: item)
81
119
  end
82
120
  end
83
121
 
84
- def add_exact_to_goto_names? exact, goto_names
85
- exact.known? && !goto_names.find { |n| n.to_name.key == exact.key }
122
+ def goto_item_label item
123
+ item.card.format.render_goto_autocomplete_item
124
+ end
125
+
126
+ def map_goto_items exact, &block
127
+ goto_names = complete_or_match_search start_only: match_start_only?
128
+ goto_names.unshift exact.name if exact.known?
129
+ goto_names.uniq.map(&block)
86
130
  end
87
131
 
88
132
  def term_param
89
- term = query_params[:keyword]
133
+ # return nil unless query_params.present?
134
+ #
135
+ # term = query_params[:keyword]
136
+ return unless (term = super)&.present?
137
+
90
138
  if (term =~ /^\+/) && (main = params["main"])
91
139
  term = main + term
92
140
  end
data/set/type/cardtype.rb CHANGED
@@ -1,5 +1,16 @@
1
1
  include_set Abstract::CqlSearch
2
2
 
3
+ # should name sorting be hard coded here??
3
4
  def cql_content
4
- { type_id: id, sort: :name }
5
+ { type_id: id, sort_by: :name }
6
+ end
7
+
8
+ def add_autocomplete_ok?
9
+ card.create_ok?
10
+ end
11
+
12
+ format :html do
13
+ def add_autocomplete_item_path
14
+ path action: :new
15
+ end
5
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: card-mod-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.2
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan McCutchen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-01-08 00:00:00.000000000 Z
13
+ date: 2023-03-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: card
@@ -18,28 +18,56 @@ dependencies:
18
18
  requirements:
19
19
  - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.104.2
21
+ version: 1.105.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - '='
27
27
  - !ruby/object:Gem::Version
28
- version: 1.104.2
28
+ version: 1.105.1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: card-mod-collection
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - '='
34
34
  - !ruby/object:Gem::Version
35
- version: 0.14.2
35
+ version: 0.15.1
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - '='
41
41
  - !ruby/object:Gem::Version
42
- version: 0.14.2
42
+ version: 0.15.1
43
+ - !ruby/object:Gem::Dependency
44
+ name: card-mod-format
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '='
48
+ - !ruby/object:Gem::Version
49
+ version: 0.15.1
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '='
55
+ - !ruby/object:Gem::Version
56
+ version: 0.15.1
57
+ - !ruby/object:Gem::Dependency
58
+ name: card-mod-help
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '='
62
+ - !ruby/object:Gem::Version
63
+ version: 0.15.1
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '='
69
+ - !ruby/object:Gem::Version
70
+ version: 0.15.1
43
71
  description: ''
44
72
  email:
45
73
  - info@decko.org
@@ -47,32 +75,36 @@ executables: []
47
75
  extensions: []
48
76
  extra_rdoc_files: []
49
77
  files:
50
- - lib/card/filter_query.rb
51
- - set/abstract/00_filter_helper.rb
52
- - set/abstract/02_search_params.rb
53
- - set/abstract/03_filter.rb
54
- - set/abstract/03_filter/_filter_input.haml
55
- - set/abstract/03_filter/filter_form.haml
56
- - set/abstract/03_filter/filter_form.rb
57
- - set/abstract/03_filter/filtered_content.haml
58
- - set/abstract/03_filter/form_helper.rb
59
- - set/abstract/03_filter/query_construction.rb
60
- - set/abstract/03_filter/quick_filters.haml
61
- - set/abstract/03_filter/selectable_filtered_content.haml
62
- - set/abstract/05_search.rb
63
- - set/abstract/05_search/checkbox_item.haml
64
- - set/abstract/05_search/select_item.haml
65
- - set/abstract/05_search/views.rb
66
- - set/abstract/06_cql_search.rb
67
- - set/abstract/filter_list.rb
68
- - set/abstract/filterable.rb
69
- - set/abstract/filterable_bar.rb
78
+ - README.md
79
+ - assets/script/autocomplete.js.coffee
80
+ - assets/script/search_box.js.coffee
81
+ - assets/style/search.scss
82
+ - config/locales/de.yml
83
+ - config/locales/en.yml
84
+ - data/files/mod_search_script_asset_output/file.js
85
+ - data/real.yml
86
+ - data/test.yml
87
+ - lib/card/mod/search.rb
88
+ - set/abstract/0_search.rb
89
+ - set/abstract/0_search/checkbox_item.haml
90
+ - set/abstract/0_search/search_params.rb
91
+ - set/abstract/0_search/views.rb
92
+ - set/abstract/cql_search.rb
70
93
  - set/abstract/search_views.rb
94
+ - set/abstract/virtual_search.rb
95
+ - set/all/autocomplete.rb
96
+ - set/all/autocomplete/autocomplete_input.haml
97
+ - set/all/autocomplete/autocomplete_item.haml
98
+ - set/right/by_create.rb
99
+ - set/right/by_name.rb
100
+ - set/right/by_update.rb
71
101
  - set/right/children.rb
102
+ - set/right/count.rb
72
103
  - set/right/created.rb
104
+ - set/right/creator.rb
73
105
  - set/right/edited.rb
74
106
  - set/right/editors.rb
75
- - set/right/follow.rb
107
+ - set/right/last_edited.rb
76
108
  - set/right/linked_to_by.rb
77
109
  - set/right/links_to.rb
78
110
  - set/right/mates.rb
@@ -82,6 +114,8 @@ files:
82
114
  - set/right/refers_to.rb
83
115
  - set/self/recent.rb
84
116
  - set/self/search.rb
117
+ - set/self/search/results_for_keyword.haml
118
+ - set/self/search/search_box.haml
85
119
  - set/type/cardtype.rb
86
120
  - set/type/search_type.rb
87
121
  homepage: https://decko.org
@@ -94,6 +128,7 @@ metadata:
94
128
  wiki_uri: https://decko.org
95
129
  documentation_url: http://docs.decko.org/
96
130
  card-mod: search
131
+ card-mod-group: gem-defaults
97
132
  post_install_message:
98
133
  rdoc_options: []
99
134
  require_paths:
@@ -109,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
144
  - !ruby/object:Gem::Version
110
145
  version: '0'
111
146
  requirements: []
112
- rubygems_version: 3.2.15
147
+ rubygems_version: 3.3.11
113
148
  signing_key:
114
149
  specification_version: 4
115
150
  summary: search
@@ -1,78 +0,0 @@
1
- class Card
2
- # Class for generating CQL based on filter params
3
- class FilterQuery
4
- def initialize filter_keys_with_values, extra_cql={}
5
- @filter_cql = Hash.new { |h, k| h[k] = [] }
6
- @rules = yield if block_given?
7
- @rules ||= {}
8
- @filter_keys_with_values = filter_keys_with_values
9
- @extra_cql = extra_cql
10
- prepare_filter_cql
11
- end
12
-
13
- def add_to_cql key, value
14
- @filter_cql[key] << value
15
- end
16
-
17
- def add_rule key, value
18
- return unless value.present?
19
-
20
- case @rules[key]
21
- when Symbol
22
- send("#{@rules[key]}_rule", key, value)
23
- when Proc
24
- @rules[key].call(key, value).each do |cql_key, val|
25
- @filter_cql[cql_key] << val
26
- end
27
- else
28
- send "#{key}_cql", value
29
- end
30
- end
31
-
32
- def to_cql
33
- @cql = {}
34
- @filter_cql.each do |cql_key, values|
35
- next if values.empty?
36
-
37
- case cql_key
38
- when :right_plus, :left_plus, :type
39
- merge_using_and cql_key, values
40
- else
41
- merge_using_array cql_key, values
42
- end
43
- end
44
- @cql.merge @extra_cql
45
- end
46
-
47
- private
48
-
49
- def prepare_filter_cql
50
- @filter_keys_with_values.each do |key, values|
51
- add_rule key, values
52
- end
53
- end
54
-
55
- def merge_using_array cql_key, values
56
- @cql[cql_key] = values.one? ? values.first : values
57
- end
58
-
59
- def merge_using_and cql_key, values
60
- hash = build_nested_hash cql_key, values
61
- @cql.deep_merge! hash
62
- end
63
-
64
- # nest values with the same key using :and
65
- def build_nested_hash key, values
66
- return { key => values[0] } if values.one?
67
-
68
- val = values.pop
69
- { key => val, and: build_nested_hash(key, values) }
70
- end
71
-
72
- def name_cql name
73
- return unless name.present?
74
-
75
- @filter_cql[:name] = ["match", name]
76
- end
77
- end
78
- end
@@ -1,46 +0,0 @@
1
- format do
2
- def filter_param field
3
- filter_hash[field.to_sym]
4
- end
5
-
6
- def filter_hash
7
- @filter_hash ||= filter_hash_from_params || default_filter_hash
8
- end
9
-
10
- def filter_hash_from_params
11
- return unless Env.params[:filter].present?
12
-
13
- Env.hash(Env.params[:filter]).deep_symbolize_keys
14
- end
15
-
16
- def sort_param
17
- @sort_param ||= safe_sql_param :sort
18
- end
19
-
20
- def safe_sql_param key
21
- param = Env.params[key]
22
- param.blank? ? nil : Card::Query.safe_sql(param)
23
- end
24
-
25
- def filter_keys_with_values
26
- filter_keys.map do |key|
27
- values = filter_param(key)
28
- values.present? ? [key, values] : next
29
- end.compact
30
- end
31
-
32
- # initial values for filtered search
33
- def default_filter_hash
34
- {}
35
- end
36
-
37
- def extra_paging_path_args
38
- super.merge filter_and_sort_hash
39
- end
40
-
41
- def filter_and_sort_hash
42
- { filter: filter_hash }.tap do |hash|
43
- hash[:sort] = sort_param if sort_param
44
- end
45
- end
46
- end