like_query 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/like_query/collect.rb +39 -5
- data/lib/like_query/version.rb +1 -1
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8b7d1785a26770724244a95ae4d5aba9b8dcf7f3ea55792960025d02ff4afb0
|
|
4
|
+
data.tar.gz: '0988e85f08d02bf75339646f522050ff2cda511c2940168a7eec449070844d73'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 89f5a4610db824380444956b705c2363e13b93f464501b25a204b4e0b4154dd72883b23bfce4bf3ae6cdab9505d73d1f0e96e283634ef27872c13ca06d611988
|
|
7
|
+
data.tar.gz: '081009a472c7fb3245dc98a55222222f0e1f7b1595497ef5c15325cc70a9c27f351497e596f6053961fc98921b9bdd94f3d72823ded1d03f0bc0151e9074139e'
|
data/lib/like_query/collect.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
module LikeQuery
|
|
2
2
|
class Collect
|
|
3
|
+
|
|
4
|
+
# Initializes the collector. Sets global result limit (falls back to Rails config),
|
|
5
|
+
# and prepares internal state for data, schemas, images, and URLs.
|
|
3
6
|
def initialize(limit = nil)
|
|
4
7
|
if limit
|
|
5
8
|
@limit = limit
|
|
@@ -21,13 +24,31 @@ module LikeQuery
|
|
|
21
24
|
@no_action = {}
|
|
22
25
|
end
|
|
23
26
|
|
|
27
|
+
# Pre-registers a schema, URL, and no_action flag for a given model class.
|
|
28
|
+
# Use before collect when the model won't be the primary query target (e.g. parent records).
|
|
24
29
|
def set_schema(model, schema = nil, url: nil, no_action: false)
|
|
25
30
|
@schemes[model.to_s] = schema_to_hash(schema)
|
|
26
31
|
@urls[model.to_s] = url if url
|
|
27
32
|
@no_action[model.to_s] = no_action
|
|
28
33
|
end
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
# Queries records and adds them to the result set for AutoSelect.svelte.
|
|
36
|
+
#
|
|
37
|
+
# @param output_schema [Array<Symbol>, nil] Columns shown as +rec.values+ in the dropdown table.
|
|
38
|
+
# Also included in +rec.attributes+ (named). Supports dot-notation and Hash form.
|
|
39
|
+
# @param extra_attributes [Array<Symbol>] Extra model columns/methods added only to +rec.attributes+
|
|
40
|
+
# (not displayed in the table, but returned to the caller on row click).
|
|
41
|
+
# @param limit [Integer, nil] Per-call result cap. Bounded by the global limit from {#initialize}.
|
|
42
|
+
# @param parent [Symbol, nil] A +belongs_to+ association name. Groups child records under their
|
|
43
|
+
# parent row (+rec.children+). Requires the parent schema to be set via {#set_schema}.
|
|
44
|
+
# @param image [Symbol, nil] Method name on the record that returns an image URL (+rec.image+).
|
|
45
|
+
# @param url [Proc, nil] +proc { |record| path(record) }+ — result stored as +rec.url+.
|
|
46
|
+
# @yield Block must return an ActiveRecord relation (the query).
|
|
47
|
+
# @return [Boolean] +false+ if the global limit is already reached, +true+ otherwise.
|
|
48
|
+
#
|
|
49
|
+
# @example Basic usage
|
|
50
|
+
# c.collect([:title, :number], extra_attributes: [:id_code]) { MyModel.like(f, [:title]) }
|
|
51
|
+
def collect(output_schema = nil, extra_attributes: [], limit: nil, parent: nil, image: nil, url: nil, &block)
|
|
31
52
|
|
|
32
53
|
_limit = (limit ? (@limit && @limit < limit ? @limit : limit) : @limit)
|
|
33
54
|
return false if @length >= _limit
|
|
@@ -69,7 +90,7 @@ module LikeQuery
|
|
|
69
90
|
|
|
70
91
|
if parent
|
|
71
92
|
parent_record = rec.send(parent)
|
|
72
|
-
r = record_to_hash(rec, schema, image, parent_record, url: _url)
|
|
93
|
+
r = record_to_hash(rec, schema, image, parent_record, url: _url, extra_attributes: extra_attributes)
|
|
73
94
|
parent_class_name = parent_record.class.to_s
|
|
74
95
|
parent_key = "#{parent_class_name}#{parent_record.id}"
|
|
75
96
|
|
|
@@ -85,7 +106,7 @@ module LikeQuery
|
|
|
85
106
|
@data[parent_key][:children] ||= []
|
|
86
107
|
@data[parent_key][:children].push(r)
|
|
87
108
|
else
|
|
88
|
-
r = record_to_hash(rec, schema, image, url: _url)
|
|
109
|
+
r = record_to_hash(rec, schema, image, url: _url, extra_attributes: extra_attributes)
|
|
89
110
|
@data["#{rec.class}#{rec.id}"] = r
|
|
90
111
|
end
|
|
91
112
|
c = (@image ? 1 : 0) + r[:values].to_a.length
|
|
@@ -98,6 +119,8 @@ module LikeQuery
|
|
|
98
119
|
true
|
|
99
120
|
end
|
|
100
121
|
|
|
122
|
+
# Returns the full result hash for JSON serialization.
|
|
123
|
+
# Includes data records, total count, overflow flag, column counts, and elapsed time.
|
|
101
124
|
def generate_hash
|
|
102
125
|
data = @data.map { |_, v| v }
|
|
103
126
|
{
|
|
@@ -112,13 +135,17 @@ module LikeQuery
|
|
|
112
135
|
}
|
|
113
136
|
end
|
|
114
137
|
|
|
138
|
+
# Serializes generate_hash to a JSON string.
|
|
115
139
|
def generate_json
|
|
116
140
|
generate_hash.to_json
|
|
117
141
|
end
|
|
118
142
|
|
|
119
143
|
private
|
|
120
144
|
|
|
121
|
-
|
|
145
|
+
# Converts a single record to the hash structure consumed by AutoSelect.svelte.
|
|
146
|
+
# rec.values - ordered array of display values (shown as table columns)
|
|
147
|
+
# rec.attributes - named hash of the same values + extra_attributes (returned on row click)
|
|
148
|
+
def record_to_hash(record, schema, image, parent = nil, url: nil, extra_attributes: [])
|
|
122
149
|
r = {}
|
|
123
150
|
schema[:values].each do |k|
|
|
124
151
|
v = get_column_value(record, k)
|
|
@@ -127,6 +154,10 @@ module LikeQuery
|
|
|
127
154
|
r[:attributes] ||= {}
|
|
128
155
|
r[:attributes][k] = v
|
|
129
156
|
end
|
|
157
|
+
extra_attributes.each do |k|
|
|
158
|
+
r[:attributes] ||= {}
|
|
159
|
+
r[:attributes][k] = get_column_value(record, k)
|
|
160
|
+
end
|
|
130
161
|
r[:id] = record.id
|
|
131
162
|
if parent
|
|
132
163
|
r[:model] = "#{parent.class.to_s}.#{record.class}"
|
|
@@ -140,6 +171,7 @@ module LikeQuery
|
|
|
140
171
|
r
|
|
141
172
|
end
|
|
142
173
|
|
|
174
|
+
# Normalizes schema input (String, Symbol, Array, Hash) into { values: [...], image: ... }.
|
|
143
175
|
def schema_to_hash(schema)
|
|
144
176
|
|
|
145
177
|
if schema.is_a?(Array) && schema.first.is_a?(Array)
|
|
@@ -177,6 +209,8 @@ module LikeQuery
|
|
|
177
209
|
end
|
|
178
210
|
end
|
|
179
211
|
|
|
212
|
+
# Reads a column value from a record. Supports dot-notation ("assoc.attr")
|
|
213
|
+
# and Hash form ({ assoc: [:a, :b] }) for joining multiple association attributes.
|
|
180
214
|
def get_column_value(record, column)
|
|
181
215
|
val = nil
|
|
182
216
|
if column.is_a?(Hash)
|
|
@@ -204,4 +238,4 @@ module LikeQuery
|
|
|
204
238
|
end
|
|
205
239
|
|
|
206
240
|
end
|
|
207
|
-
end
|
|
241
|
+
end
|
data/lib/like_query/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: like_query
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- christian
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: rails
|
|
@@ -61,7 +60,6 @@ metadata:
|
|
|
61
60
|
homepage_uri: https://gitlab.com/sedl/like_query
|
|
62
61
|
source_code_uri: https://gitlab.com/sedl/like_query
|
|
63
62
|
changelog_uri: https://gitlab.com/sedl/like_query
|
|
64
|
-
post_install_message:
|
|
65
63
|
rdoc_options: []
|
|
66
64
|
require_paths:
|
|
67
65
|
- lib
|
|
@@ -76,8 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
76
74
|
- !ruby/object:Gem::Version
|
|
77
75
|
version: '0'
|
|
78
76
|
requirements: []
|
|
79
|
-
rubygems_version:
|
|
80
|
-
signing_key:
|
|
77
|
+
rubygems_version: 4.0.4
|
|
81
78
|
specification_version: 4
|
|
82
79
|
summary: helper for building active record calls.
|
|
83
80
|
test_files: []
|