scopiform 0.2.4 → 0.2.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60fdf69684da8b4a16efc5b52b69bcae18d30824b844fb57fdc3219780ed2894
4
- data.tar.gz: fbc86f9e966127b7c64546adc46b805e1c78caaa6d8aa9c49acceaeea7120639
3
+ metadata.gz: 5f4f7678ce36e47c795736985a53b7656a2223afbd0b119702a2ccd41ff2d523
4
+ data.tar.gz: 282cfccb56d1760a2ae9d2e55bc4706555684086c0fc906161eefaf663d13865
5
5
  SHA512:
6
- metadata.gz: 6165fbab6073d4ec637aed25cfb58d2f13ef7ecd590724b17374f47e9718850c62740655768f11f1e1b7f14a119a30eba863897c6189b620844e03b0c54fa0ee
7
- data.tar.gz: 5730438bea472073e31a9f37d45ce3de8e066615e9dea0b0fa99af5bb955d5b3192cd068898c6367515d959d84354a202c90d405f5a08f488f8f07f342439aa7
6
+ metadata.gz: 8ea922c85bd6098946058bbafe2cf3b93b7012535af34932eb2168b0569b19b86d25a3fadf97e30265e49132a746dffab93a30889a8e29d6683fc71d03291f04
7
+ data.tar.gz: 68daa6ffcbe5ddc91e61b68a662bc52352a860eb64e31a511473177b0454497e937f947e7cd3300844faf34b47b49904cba0292043d7e8c1c81c6d8db7b08c7c
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/concern'
4
- require 'scopiform/scope_context'
4
+ require 'scopiform/utilities'
5
5
 
6
6
  module Scopiform
7
7
  module AssociationScopes
@@ -12,23 +12,6 @@ module Scopiform
12
12
  end
13
13
 
14
14
  module ClassMethods
15
- def scopiform_association_scope(association, method, value, ctx:)
16
- is_root = ctx.nil?
17
- ctx = ScopeContext.from(ctx)
18
- ctx.set(arel_table) if is_root || ctx.arel_table.blank?
19
-
20
- ctx.association = association
21
- ctx.build_joins
22
-
23
- applied = ctx.association.klass.send(method, value, ctx: ScopeContext.from(ctx).set(ctx.association_arel_table))
24
-
25
- if is_root
26
- ctx.scopes.reduce(joins(ctx.joins).merge(applied)) { |chain, scope| chain.merge(scope) }
27
- else
28
- ctx.scopes.reduce(all.merge(applied)) { |chain, scope| chain.merge(scope) }
29
- end
30
- end
31
-
32
15
  def reflection_added(_name, reflection)
33
16
  setup_association_auto_scopes(reflection)
34
17
  end
@@ -45,7 +28,7 @@ module Scopiform
45
28
  auto_scope_add(
46
29
  association.name,
47
30
  proc { |value, ctx: nil|
48
- scopiform_association_scope(association, :apply_filters, value, ctx: ctx)
31
+ Utilities.association_scope(self, association, :apply_filters, value, ctx: ctx)
49
32
  },
50
33
  suffix: '_is',
51
34
  argument_type: :hash
@@ -55,7 +38,7 @@ module Scopiform
55
38
  auto_scope_add(
56
39
  association.name,
57
40
  proc { |value, ctx: nil|
58
- scopiform_association_scope(association, :apply_sorts, value, ctx: ctx)
41
+ Utilities.association_scope(self, association, :apply_sorts, value, ctx: ctx)
59
42
  },
60
43
  prefix: 'sort_by_',
61
44
  argument_type: :hash,
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/concern'
4
+ require 'scopiform/utilities'
4
5
 
5
6
  module Scopiform
6
7
  module StringNumberScopes
@@ -13,14 +14,6 @@ module Scopiform
13
14
  module ClassMethods
14
15
  private
15
16
 
16
- def cast_to_text(arel_column)
17
- cast_to = connection.adapter_name.downcase.starts_with?('mysql') ? 'CHAR' : 'TEXT'
18
- Arel::Nodes::NamedFunction.new(
19
- 'CAST',
20
- [arel_column.as(cast_to)]
21
- )
22
- end
23
-
24
17
  def setup_string_and_number_auto_scopes
25
18
  string_numbers = Helpers::STRING_TYPES + Helpers::NUMBER_TYPES
26
19
  string_number_columns = safe_columns.select do |column|
@@ -42,7 +35,7 @@ module Scopiform
42
35
  name,
43
36
  proc { |value, ctx: nil, **|
44
37
  arel_column = scopiform_arel(ctx)[name]
45
- arel_column = cast_to_text(arel_column) if cast
38
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
46
39
  where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%"))
47
40
  },
48
41
  suffix: '_contains',
@@ -54,7 +47,7 @@ module Scopiform
54
47
  name,
55
48
  proc { |value, ctx: nil, **|
56
49
  arel_column = scopiform_arel(ctx)[name]
57
- arel_column = cast_to_text(arel_column) if cast
50
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
58
51
  where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%"))
59
52
  },
60
53
  suffix: '_not_contains',
@@ -66,7 +59,7 @@ module Scopiform
66
59
  name,
67
60
  proc { |value, ctx: nil, **|
68
61
  arel_column = scopiform_arel(ctx)[name]
69
- arel_column = cast_to_text(arel_column) if cast
62
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
70
63
  where(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%"))
71
64
  },
72
65
  suffix: '_starts_with',
@@ -78,7 +71,7 @@ module Scopiform
78
71
  name,
79
72
  proc { |value, ctx: nil, **|
80
73
  arel_column = scopiform_arel(ctx)[name]
81
- arel_column = cast_to_text(arel_column) if cast
74
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
82
75
  where.not(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%"))
83
76
  },
84
77
  suffix: '_not_starts_with',
@@ -90,7 +83,7 @@ module Scopiform
90
83
  name,
91
84
  proc { |value, ctx: nil, **|
92
85
  arel_column = scopiform_arel(ctx)[name]
93
- arel_column = cast_to_text(arel_column) if cast
86
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
94
87
  where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}"))
95
88
  },
96
89
  suffix: '_ends_with',
@@ -102,7 +95,7 @@ module Scopiform
102
95
  name,
103
96
  proc { |value, ctx: nil, **|
104
97
  arel_column = scopiform_arel(ctx)[name]
105
- arel_column = cast_to_text(arel_column) if cast
98
+ arel_column = Utilities.cast_to_text(arel_column, connection) if cast
106
99
  where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}"))
107
100
  },
108
101
  suffix: '_not_ends_with',
@@ -0,0 +1,30 @@
1
+ require 'scopiform/scope_context'
2
+
3
+ module Scopiform
4
+ module Utilities
5
+ def self.cast_to_text(arel_column, connection)
6
+ cast_to = connection.adapter_name.downcase.starts_with?('mysql') ? 'CHAR' : 'TEXT'
7
+ Arel::Nodes::NamedFunction.new(
8
+ 'CAST',
9
+ [arel_column.as(cast_to)]
10
+ )
11
+ end
12
+
13
+ def self.association_scope(active_record, association, method, value, ctx:)
14
+ is_root = ctx.nil?
15
+ ctx = ScopeContext.from(ctx)
16
+ ctx.set(active_record.arel_table) if is_root || ctx.arel_table.blank?
17
+
18
+ ctx.association = association
19
+ ctx.build_joins
20
+
21
+ applied = ctx.association.klass.send(method, value, ctx: ScopeContext.from(ctx).set(ctx.association_arel_table))
22
+
23
+ if is_root
24
+ ctx.scopes.reduce(active_record.joins(ctx.joins).merge(applied)) { |chain, scope| chain.merge(scope) }
25
+ else
26
+ ctx.scopes.reduce(active_record.all.merge(applied)) { |chain, scope| chain.merge(scope) }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Scopiform
2
- VERSION = '0.2.4'.freeze
2
+ VERSION = '0.2.5'.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.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - jayce.pulsipher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-04 00:00:00.000000000 Z
11
+ date: 2021-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -101,6 +101,7 @@ files:
101
101
  - lib/scopiform/scope_definition.rb
102
102
  - lib/scopiform/string_number_date_scopes.rb
103
103
  - lib/scopiform/string_number_scopes.rb
104
+ - lib/scopiform/utilities.rb
104
105
  - lib/scopiform/version.rb
105
106
  - lib/tasks/scopiform_tasks.rake
106
107
  homepage: https://github.com/3-form/scopiform