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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae35b0a41cb186989dbb8f76fb8dbfa11604782e6b795fca0ac5e50e66bdd3fa
4
- data.tar.gz: 88b9bb3f9cf86b143d997502d0ae997606652d813541259c684d76c7c379b40d
3
+ metadata.gz: cca47f44f585db581edd54791485e5016ecc1d8f73c5e09dc0cb3be21a853d0e
4
+ data.tar.gz: fea9cb2762ac42bdcb1089262ac0e116857fb1d568590bb3c570e9b1f49f60d8
5
5
  SHA512:
6
- metadata.gz: a6239fb1c253b895b3bf35f640fde0f600cc853f9348839108536abf2eed887c71c763b1c1ebb71fa4d2eff07bb77b953732641edb4dde3f271127d25d76287e
7
- data.tar.gz: fde9049ba6c66da3901eb99352b3e938130a46e8a24e85c42a04a92c4c641188e112299ea2882b632fe3f6d833f300d9bc8f7aa52928a69b4df7653377af268f
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
@@ -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
- if filter_name.to_s.casecmp('OR').zero?
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 { |or_filters_hash| apply_filters(or_filters_hash, injecting: out, ctx: ctx) }
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.or(applied) }
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
@@ -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 ActiveRecord::StatementInvalid
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 ActiveRecord::StatementInvalid
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Scopiform
2
- VERSION = '0.2.6'.freeze
2
+ VERSION = '0.2.10'.freeze
3
3
  end
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.6
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-03-01 00:00:00.000000000 Z
11
+ date: 2021-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord