card-mod-lookup 0.1 → 0.3

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: b4215382b1478803a91069e8f71e7a980a64d632c2d5b62e1bd69cf5fb0c7cbe
4
- data.tar.gz: 746772c19ae1fa95a3d685a8f8befd8d2d2b516033908d99ae82a7ce0175eeed
3
+ metadata.gz: 2c77c10ee00c6b928424df37db61a768e58a610f35d6db668e2a3d02a58a46ee
4
+ data.tar.gz: 78cc69b638d553606d18f558f0702aadf2c42100ec0af3a0e2ba38a57c12c7f9
5
5
  SHA512:
6
- metadata.gz: 03eef8d51212c24dffb854ea9d1727c8accd0cb1371a4d46f0b3c05ae478b209c3bd3cd86946ee9a4637e18513126fc342b26d9a7395f5656baaa45972df8c07
7
- data.tar.gz: 3eab763cc4378322b543659203326ab436a4990b6ec8be550e5b2b97bd2ed4714c74e54db61c310b9eae44cb6e8573d426f4ee45baa0a370c53ebcd7f686ca3f
6
+ metadata.gz: c3650ecfd40809a93ce97167062ad4df2ff40970554565267370d192491230d8e29b27b2e68b0d28ec518e0f81791b5af03e302108c5beb05334dad3c8daf9fe
7
+ data.tar.gz: 1844dddf1146c2575cf4fab268de1a9e5369e1b1ef3d168c4f0b05fe532e058b5409f2f62aa1a5b6234247ef9d523fa00772e70304be3a916d2d71d05d8e17c7
data/README.md CHANGED
@@ -18,7 +18,7 @@ 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
24
  and metric designers that WikiRate supports, you can imagine the queries
@@ -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
@@ -45,10 +45,10 @@ class Card
45
45
  end
46
46
 
47
47
  def to_card_id value
48
- if value.is_a?(Array)
49
- value.map { |v| Card.fetch_id(v) }
48
+ if value.is_a? Array
49
+ value.map(&:card_id)
50
50
  else
51
- Card.fetch_id(value)
51
+ value.card_id
52
52
  end
53
53
  end
54
54
 
@@ -63,9 +63,22 @@ class Card
63
63
  @restrict_to_ids[col] = existing ? (existing & ids) : ids
64
64
  end
65
65
 
66
- def restrict_by_cql col, cql
67
- cql.reverse_merge! return: :id, limit: 0
68
- restrict_to_ids col, Card.search(cql)
66
+ def restrict_by_cql suffix, col, cql
67
+ q = Card::Query.new cql.merge(table_suffix: suffix)
68
+ on = "#{filter_table col}.#{col} = #{q.table_alias}.#{cql[:return] || :id}"
69
+ @joins << "JOIN cards #{q.table_alias} ON #{on}"
70
+ @joins << q.sql_statement.joins
71
+ @conditions << q.sql_statement.where(false)
72
+
73
+ # cql.reverse_merge! return: :id, limit: 0
74
+ # @conditions << "#{filter_table col}.#{col} IN (#{Card::Query.new(cql).sql})"
75
+
76
+ # original strat: list of ids. slightly slower than IN. uglier SQL
77
+ # restrict_to_ids col, Card.search(cql)
78
+
79
+ # exists strat: got crazy slow
80
+ # new_cond = "#{filter_table col}.#{col} = c0.#{cql[:return]}"
81
+ # @conditions << "EXISTS (#{Card::Query.new(cql).sql} AND #{new_cond})"
69
82
  end
70
83
 
71
84
  def filter field, value, operator=nil
@@ -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
@@ -4,7 +4,7 @@ end
4
4
 
5
5
  # lookup fields are often based on cards' compound names
6
6
  event :refresh_lookup, :integrate, changed: :name, on: :update do
7
- lookup.refresh
7
+ lookup ? lookup.refresh : create_lookup
8
8
  end
9
9
 
10
10
  event :delete_lookup, :finalize, on: :delete do
@@ -85,10 +85,16 @@ 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
92
  def default_limit
92
93
  Auth.signed_in? ? 5000 : 500
93
94
  end
95
+
96
+ # not a CQL search
97
+ def filter_cql
98
+ {}
99
+ end
94
100
  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.1'
4
+ version: '0.3'
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-10-26 00:00:00.000000000 Z
12
+ date: 2023-05-02 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
@@ -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.28
79
+ rubygems_version: 3.2.33
66
80
  signing_key:
67
81
  specification_version: 4
68
82
  summary: lookup