card-mod-lookup 0.2 → 0.4

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
  SHA256:
3
- metadata.gz: d77f0f0886a1501eb9348fe19e42575ddfac1b74a0a8083d133919cc9c74807b
4
- data.tar.gz: 5814cbfda2b15d6c2b4903474940908539121449d83b79528ce048c04bedd688
3
+ metadata.gz: a98f8a016a2cab0a120e24080a6a9412c78df74364ffc335eeebcaed237d381e
4
+ data.tar.gz: ea21cc3f70eae3eabb38fcacd5bd69e6780c7fd9b365230536cb97c7ae211155
5
5
  SHA512:
6
- metadata.gz: bcabc55ee2467ea1bde04d59a735d4d5d86cb240186232ccde7c7ff4a774d6d1f126fb2c3a834211b38e459029f2481ce0745ead932f5b168c32d47e9ac458e5
7
- data.tar.gz: 9e11d30ff675d7e87d2976488cbf45808cf436682210707366f0bf8b25f9884b4d835c9c873be32fbac0dac5a5bd0ac778a5143938e109438d15a59becb7605e
6
+ metadata.gz: 3ea4e3b72a7ff53aedb4045386d9d901c7b9b594f6faa44d534ef7add330c693a1e011ca942b3efa80997d903f5ab03a258d2461c569e61328bee40debff2da5
7
+ data.tar.gz: d10d164d42714b2db57d77101d69af7c74244402ec597018efc59ee06599edfd7b4cd2767fc11ebd1609590d6cf2bcd7bd9fb512d80ea82886502f8ccf6ef1a9
data/README.md CHANGED
@@ -18,10 +18,10 @@ and when a database grows large, it can become quite expensive to execute all
18
18
  the implied self joins.
19
19
 
20
20
  For example, consider the use case that first inspired this code: _answers_ on
21
- WikiRate.org. WikiRate is a site that collects answers to questions about
21
+ wikirate.org. Wikirate is a site that collects answers to questions about
22
22
  companies. A given answer is a response to a given metric for a given year
23
23
  for a given company. When you consider all the kinds of companies and metrics
24
- and metric designers that WikiRate supports, you can imagine the queries
24
+ and metric designers that Wikirate supports, you can imagine the queries
25
25
  becoming quite complex if we have to re-join the cards table to itself every
26
26
  time we want to consider a different variable.
27
27
 
@@ -1,5 +1,5 @@
1
1
  class Card
2
- class LookupFilterQuery
2
+ class LookupQuery
3
3
  # support methods for sort and page
4
4
  module ActiveRecordExtension
5
5
  # @params hash [Hash] key1: dir1, key2: dir2
@@ -36,7 +36,8 @@ class Card
36
36
  end
37
37
 
38
38
  def sort_by_bookmarkers type, rel
39
- [Card::Bookmark.add_sort_join(rel, "#{table.name}.#{type}_id"), "cts.value"]
39
+ [Card::Bookmark.add_sort_join(rel, "#{table.name}.#{type}_id"),
40
+ "bookmarkers"]
40
41
  end
41
42
  end
42
43
  end
@@ -1,6 +1,6 @@
1
1
  class Card
2
- class LookupFilterQuery
3
- # shared filtering methods for FilterQuery classes built on lookup tables
2
+ class LookupQuery
3
+ # shared filtering methods for query classes built on lookup tables
4
4
  module Filtering
5
5
  def process_filters
6
6
  normalize_filter_args
@@ -17,7 +17,7 @@ class Card
17
17
  if (method = filter_method key)
18
18
  send method, key, value
19
19
  else
20
- try "#{key}_query", value
20
+ try "filter_by_#{key}", value
21
21
  end
22
22
  end
23
23
 
@@ -40,10 +40,13 @@ class Card
40
40
  filter card_id_map[key], card_id
41
41
  end
42
42
 
43
- def not_ids_query value
44
- add_condition "#{lookup_class.card_column} not in (?)", value.split(",")
43
+ def filter_by_not_ids value
44
+ add_condition "#{lookup_class.card_column} not in (?)",
45
+ not_ids_value(value)
45
46
  end
46
47
 
48
+ private
49
+
47
50
  def to_card_id value
48
51
  if value.is_a? Array
49
52
  value.map(&:card_id)
@@ -63,9 +66,22 @@ class Card
63
66
  @restrict_to_ids[col] = existing ? (existing & ids) : ids
64
67
  end
65
68
 
66
- def restrict_by_cql col, cql
67
- cql.reverse_merge! return: :id, limit: 0
68
- @conditions << "#{filter_table col}.#{col} IN (#{Card::Query.new(cql).sql})"
69
+ def restrict_by_cql suffix, col, cql
70
+ q = Card::Query.new cql.merge(table_suffix: suffix)
71
+ on = "#{filter_table col}.#{col} = #{q.table_alias}.#{cql[:return] || :id}"
72
+ @joins << "JOIN cards #{q.table_alias} ON #{on}"
73
+ @joins << q.sql_statement.joins
74
+ @conditions << q.sql_statement.where(false)
75
+
76
+ # cql.reverse_merge! return: :id, limit: 0
77
+ # @conditions << "#{filter_table col}.#{col} IN (#{Card::Query.new(cql).sql})"
78
+
79
+ # original strat: list of ids. slightly slower than IN. uglier SQL
80
+ # restrict_to_ids col, Card.search(cql)
81
+
82
+ # exists strat: got crazy slow
83
+ # new_cond = "#{filter_table col}.#{col} = c0.#{cql[:return]}"
84
+ # @conditions << "EXISTS (#{Card::Query.new(cql).sql} AND #{new_cond})"
69
85
  end
70
86
 
71
87
  def filter field, value, operator=nil
@@ -93,6 +109,11 @@ class Card
93
109
  def db_value value
94
110
  value.is_a?(Array) ? "(?)" : "?"
95
111
  end
112
+
113
+ def not_ids_value value
114
+ return value if value.is_a? Array
115
+ value.to_s.split ","
116
+ end
96
117
  end
97
118
  end
98
119
  end
@@ -1,6 +1,6 @@
1
1
  class Card
2
- # base class for FilterQuery classes built on lookup tables
3
- class LookupFilterQuery
2
+ # base class for query classes built on lookup tables
3
+ class LookupQuery
4
4
  include Filtering
5
5
 
6
6
  attr_accessor :filter_args, :sort_args, :paging_args
@@ -54,7 +54,8 @@ class Card
54
54
 
55
55
  # @return [Array]
56
56
  def count
57
- @empty_result ? 0 : main_query.count
57
+ # we need the id because some joins distort the count
58
+ @empty_result ? 0 : main_query.select("#{lookup_table}.id").distinct.count
58
59
  end
59
60
 
60
61
  def limit
@@ -75,7 +76,10 @@ class Card
75
76
  def sort_and_page
76
77
  relation = yield
77
78
  @sort_joins.uniq.each { |j| relation = relation.joins(j) }
78
-
79
+ if @sort_hash.present?
80
+ select = ["#{lookup_table}.*", sort_fields].flatten.compact
81
+ relation = relation.select(select).distinct
82
+ end
79
83
  relation.sort(@sort_hash).paging(@paging_args)
80
84
  end
81
85
 
@@ -86,9 +90,23 @@ class Card
86
90
  end
87
91
  end
88
92
 
93
+ def sort_fields
94
+ @sort_hash.keys.map do |key|
95
+ return nil if key == :random
96
+
97
+ if key.match?(/_bookmarkers$/)
98
+ "cts.value as bookmarkers"
99
+ else
100
+ Card::Query.safe_sql key
101
+ end
102
+ end
103
+ end
104
+
89
105
  def sort_by sort_by
90
106
  if (id_field = sort_by_cardname[sort_by])
91
107
  sort_by_join sort_by, lookup_table, id_field
108
+ elsif sort_by == :random
109
+ "rand()"
92
110
  else
93
111
  simple_sort_by sort_by
94
112
  end
@@ -12,12 +12,12 @@ def run_query_returning query, return_type
12
12
  end
13
13
  end
14
14
 
15
- def filter_class
16
- raise Error::ServerError, "filter_class required!"
15
+ def query_class
16
+ raise Error::ServerError, "query_class required!"
17
17
  end
18
18
 
19
19
  def query paging={}
20
- filter_class.new query_hash, {}, paging
20
+ query_class.new query_hash, {}, paging
21
21
  end
22
22
 
23
23
  def query_hash
@@ -29,7 +29,7 @@ def count
29
29
  end
30
30
 
31
31
  format do
32
- delegate :filter_class, to: :card
32
+ delegate :query_class, to: :card
33
33
 
34
34
  def search_with_params
35
35
  @search_with_params ||= card.search query: query
@@ -40,11 +40,11 @@ format do
40
40
  end
41
41
 
42
42
  def query
43
- filter_class.new query_hash, sort_hash, paging_params
43
+ query_class.new query_hash, sort_hash, paging_params
44
44
  end
45
45
 
46
46
  def count_query
47
- filter_class.new query_hash
47
+ query_class.new query_hash
48
48
  end
49
49
 
50
50
  def query_hash
@@ -85,10 +85,12 @@ format do
85
85
  end
86
86
 
87
87
  def sort_by_from_param
88
- safe_sql_param(:sort_by)&.to_sym
88
+ # :sort is DEPRECATED
89
+ safe_sql_param(:sort_by)&.to_sym || safe_sql_param(:sort)&.to_sym
89
90
  end
90
91
 
91
- def default_limit
92
- Auth.signed_in? ? 5000 : 500
92
+ # not a CQL search
93
+ def filter_cql
94
+ {}
93
95
  end
94
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: card-mod-lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philipp Kühl
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-12-23 00:00:00.000000000 Z
12
+ date: 2023-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: card
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: card-mod-filter
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
28
42
  description: ''
29
43
  email:
30
44
  - info@decko.org
@@ -33,9 +47,9 @@ extensions: []
33
47
  extra_rdoc_files: []
34
48
  files:
35
49
  - README.md
36
- - lib/card/lookup_filter_query.rb
37
- - lib/card/lookup_filter_query/active_record_extension.rb
38
- - lib/card/lookup_filter_query/filtering.rb
50
+ - lib/card/lookup_query.rb
51
+ - lib/card/lookup_query/active_record_extension.rb
52
+ - lib/card/lookup_query/filtering.rb
39
53
  - lib/lookup_table.rb
40
54
  - lib/lookup_table/class_methods.rb
41
55
  - set/abstract/lookup.rb
@@ -62,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
76
  - !ruby/object:Gem::Version
63
77
  version: '0'
64
78
  requirements: []
65
- rubygems_version: 3.2.15
79
+ rubygems_version: 3.4.10
66
80
  signing_key:
67
81
  specification_version: 4
68
82
  summary: lookup