scopiform 0.2.6 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/scopiform/association_scopes.rb +13 -0
- data/lib/scopiform/common_scopes.rb +9 -0
- data/lib/scopiform/filters.rb +16 -3
- data/lib/scopiform/helpers.rb +16 -7
- data/lib/scopiform/utilities.rb +4 -0
- data/lib/scopiform/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cca47f44f585db581edd54791485e5016ecc1d8f73c5e09dc0cb3be21a853d0e
|
4
|
+
data.tar.gz: fea9cb2762ac42bdcb1089262ac0e116857fb1d568590bb3c570e9b1f49f60d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc021d11757854fc08b8d238ffe7f8bdf2300cf94fb2ceb2d5b87720dbb9de977845343007e6f04377b93f1c95ee94cdc6b89c3daa29ac538cfa184aeb4cae72
|
7
|
+
data.tar.gz: 97669e667b8610924637a185fd5b31c0421f03e54834ccc76fa4ea326fa68ce82e44011ce0c55a47d1f4a2380171c597e5f4c1a7dffca7ed644ee34f0ea8cceb
|
@@ -44,6 +44,19 @@ module Scopiform
|
|
44
44
|
argument_type: :hash,
|
45
45
|
type: :sort
|
46
46
|
)
|
47
|
+
|
48
|
+
# Grouping
|
49
|
+
auto_scope_add(
|
50
|
+
association.name,
|
51
|
+
proc { |value, ctx: nil|
|
52
|
+
next all unless Utilities.truthy_hash(value)
|
53
|
+
|
54
|
+
Utilities.association_scope(self, association, :apply_groupings, value, ctx: ctx)
|
55
|
+
},
|
56
|
+
prefix: 'group_by_',
|
57
|
+
argument_type: :hash,
|
58
|
+
type: :group
|
59
|
+
)
|
47
60
|
end
|
48
61
|
end
|
49
62
|
end
|
@@ -40,6 +40,15 @@ module Scopiform
|
|
40
40
|
argument_type: :string,
|
41
41
|
type: :sort
|
42
42
|
)
|
43
|
+
|
44
|
+
# Grouping
|
45
|
+
auto_scope_add(
|
46
|
+
name,
|
47
|
+
proc { |value = true, ctx: nil, **| group(scopiform_arel(ctx)[name_sym]) if value },
|
48
|
+
prefix: 'group_by_',
|
49
|
+
argument_type: :boolean,
|
50
|
+
type: :group
|
51
|
+
)
|
43
52
|
end
|
44
53
|
end
|
45
54
|
end
|
data/lib/scopiform/filters.rb
CHANGED
@@ -16,20 +16,28 @@ module Scopiform
|
|
16
16
|
sorts_hash.keys.inject(injecting) { |out, sort_name| resolve_sort(out, sort_name, sorts_hash[sort_name], ctx: ctx) }
|
17
17
|
end
|
18
18
|
|
19
|
+
def apply_groupings(groupings_hash, injecting = all, ctx: nil)
|
20
|
+
groupings_hash.keys.inject(injecting) { |out, grouping_name| resolve_grouping(out, grouping_name, groupings_hash[grouping_name], ctx: ctx) }
|
21
|
+
end
|
22
|
+
|
19
23
|
private
|
20
24
|
|
21
25
|
def resolve_filter(out, filter_name, filter_argument, ctx:)
|
22
|
-
|
26
|
+
is_or = filter_name.to_s.casecmp?('OR')
|
27
|
+
is_and = filter_name.to_s.casecmp?('AND')
|
28
|
+
if is_or || is_and
|
23
29
|
ctx ||= ScopeContext.new
|
24
30
|
ctx.joins = []
|
31
|
+
chain_method = is_or ? :or : :merge
|
25
32
|
|
26
33
|
return (
|
27
34
|
filter_argument
|
28
|
-
.map { |
|
35
|
+
.map { |filter_hash| apply_filters(filter_hash, injecting: out, ctx: ctx) }
|
29
36
|
.map { |a| a.joins(ctx.joins) }
|
30
|
-
.inject { |chain, applied| chain.
|
37
|
+
.inject { |chain, applied| chain.public_send(chain_method, applied) }
|
31
38
|
)
|
32
39
|
end
|
40
|
+
|
33
41
|
out.send(filter_name, filter_argument, ctx: ctx)
|
34
42
|
end
|
35
43
|
|
@@ -37,6 +45,11 @@ module Scopiform
|
|
37
45
|
method_name = "sort_by_#{sort_name}"
|
38
46
|
out.send(method_name, sort_argument, ctx: ctx)
|
39
47
|
end
|
48
|
+
|
49
|
+
def resolve_grouping(out, group_name, group_argument, ctx:)
|
50
|
+
method_name = "group_by_#{group_name}"
|
51
|
+
out.send(method_name, group_argument, ctx: ctx)
|
52
|
+
end
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
data/lib/scopiform/helpers.rb
CHANGED
@@ -44,7 +44,7 @@ module Scopiform
|
|
44
44
|
name = resolve_alias(name)
|
45
45
|
association = reflect_on_association(name)
|
46
46
|
|
47
|
-
association.klass if association.present?
|
47
|
+
association.klass if association.present? && !association.polymorphic?
|
48
48
|
association
|
49
49
|
rescue NameError
|
50
50
|
logger.warn "Unable to load class for association `#{name}` in model `#{self.name}`"
|
@@ -62,17 +62,26 @@ module Scopiform
|
|
62
62
|
protected
|
63
63
|
|
64
64
|
def safe_columns
|
65
|
-
columns
|
66
|
-
rescue
|
65
|
+
@safe_columns ||= columns
|
66
|
+
rescue *safe_column_rescue_errors
|
67
67
|
logger.warn "Unable to load columns for `#{name}`"
|
68
|
-
[]
|
68
|
+
@safe_columns = []
|
69
69
|
end
|
70
70
|
|
71
71
|
def safe_columns_hash
|
72
|
-
columns_hash
|
73
|
-
rescue
|
72
|
+
@safe_columns_hash ||= columns_hash
|
73
|
+
rescue *safe_column_rescue_errors
|
74
74
|
logger.warn "Unable to load columns_hash for `#{name}`"
|
75
|
-
{}
|
75
|
+
@safe_columns_hash = {}
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def safe_column_rescue_errors
|
81
|
+
[
|
82
|
+
ActiveRecord::StatementInvalid,
|
83
|
+
Object.const_defined?('TinyTds') ? TinyTds::Error : nil,
|
84
|
+
]
|
76
85
|
end
|
77
86
|
end
|
78
87
|
end
|
data/lib/scopiform/utilities.rb
CHANGED
@@ -26,5 +26,9 @@ module Scopiform
|
|
26
26
|
ctx.scopes.reduce(active_record.all.merge(applied)) { |chain, scope| chain.merge(scope) }
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
def self.truthy_hash(hash)
|
31
|
+
hash.values.find { |value| value.is_a?(Hash) ? truthy_hash(value) : value.present? }
|
32
|
+
end
|
29
33
|
end
|
30
34
|
end
|
data/lib/scopiform/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopiform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jayce.pulsipher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|