scopiform 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/scopiform/association_scopes.rb +5 -5
- data/lib/scopiform/common_scopes.rb +5 -5
- data/lib/scopiform/core.rb +28 -8
- data/lib/scopiform/filters.rb +5 -5
- data/lib/scopiform/helpers.rb +4 -0
- data/lib/scopiform/reflection_plugin.rb +3 -3
- data/lib/scopiform/scope_definition.rb +12 -1
- data/lib/scopiform/string_number_date_scopes.rb +6 -6
- data/lib/scopiform/string_number_scopes.rb +21 -13
- data/lib/scopiform/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 934151f9380f5ddc631f49bf933feb2a4763c899a47cae97e17dd682931074c2
|
4
|
+
data.tar.gz: 7b11d4ffe28fbc13613932b4b3aad56cd01e3ad76a18c3b90286e652ca80b847
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28c1c3c6a4dadfa6a3a00271fce88c6471c770e0d8249db754f7720ec2f47af6418bf450fd8bf22a85d995190906cc0f833b310081dd9773f8af33fc17fd6e3d
|
7
|
+
data.tar.gz: 726a2fa46e53224a825d55a55d4995198a4458e4a3a12ce54d2d55278cc5a5e04ec8c825682bb6d228207d41810d4bf96d93bf773341e784fa24cf6de8fec637
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ module Scopiform
|
|
30
30
|
def setup_association_auto_scopes(association)
|
31
31
|
auto_scope_add(
|
32
32
|
association.name,
|
33
|
-
|
33
|
+
proc { |value, joins: nil|
|
34
34
|
is_root = joins.nil?
|
35
35
|
joins = {} if is_root
|
36
36
|
|
@@ -47,13 +47,13 @@ module Scopiform
|
|
47
47
|
argument_type: :hash
|
48
48
|
)
|
49
49
|
|
50
|
-
#
|
50
|
+
# Sorting
|
51
51
|
auto_scope_add(
|
52
52
|
association.name,
|
53
|
-
|
54
|
-
prefix: '
|
53
|
+
proc { |value| scopiform_joins(association.name).merge(association.klass.apply_sorts(value)) },
|
54
|
+
prefix: 'sort_by_',
|
55
55
|
argument_type: :hash,
|
56
|
-
type: :
|
56
|
+
type: :sort
|
57
57
|
)
|
58
58
|
end
|
59
59
|
end
|
@@ -21,24 +21,24 @@ module Scopiform
|
|
21
21
|
|
22
22
|
auto_scope_add(
|
23
23
|
name,
|
24
|
-
|
24
|
+
proc { |value| where(name_sym => value) },
|
25
25
|
suffix: '_is',
|
26
26
|
argument_type: type
|
27
27
|
)
|
28
28
|
auto_scope_add(
|
29
29
|
name,
|
30
|
-
|
30
|
+
proc { |value| where.not(name_sym => value) },
|
31
31
|
suffix: '_not',
|
32
32
|
argument_type: type
|
33
33
|
)
|
34
34
|
|
35
|
-
#
|
35
|
+
# Sorting
|
36
36
|
auto_scope_add(
|
37
37
|
name,
|
38
38
|
->(value = :asc) { order(name_sym => value) },
|
39
|
-
prefix: '
|
39
|
+
prefix: 'sort_by_',
|
40
40
|
argument_type: :string,
|
41
|
-
type: :
|
41
|
+
type: :sort
|
42
42
|
)
|
43
43
|
end
|
44
44
|
end
|
data/lib/scopiform/core.rb
CHANGED
@@ -22,12 +22,10 @@ module Scopiform
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def auto_scope_add(attribute, block, prefix: nil, suffix: nil, **options)
|
25
|
-
scope_name = "#{prefix}#{attribute}#{suffix}".underscore
|
26
|
-
scope_name_sym = scope_name.to_sym
|
27
|
-
scope scope_name_sym, block
|
28
|
-
|
29
25
|
scope_definition = auto_scope_add_definition(attribute, prefix: prefix, suffix: suffix, **options)
|
30
26
|
|
27
|
+
scope scope_definition.name, block
|
28
|
+
|
31
29
|
aliases_for(attribute).each do |alias_name|
|
32
30
|
auto_scope_for_alias(alias_name, scope_definition)
|
33
31
|
end
|
@@ -41,8 +39,31 @@ module Scopiform
|
|
41
39
|
end
|
42
40
|
end
|
43
41
|
|
42
|
+
def enum(definitions)
|
43
|
+
super(definitions)
|
44
|
+
|
45
|
+
definitions.each_key { |name| update_scope_to_enum(name) }
|
46
|
+
end
|
47
|
+
|
44
48
|
private
|
45
49
|
|
50
|
+
def update_scope_to_enum(name)
|
51
|
+
scopes = auto_scopes_by_attribute(name)
|
52
|
+
|
53
|
+
scopes.each do |scope|
|
54
|
+
if scope.options[:type].blank?
|
55
|
+
argument_type = scope.options[:argument_type]
|
56
|
+
scope.options[:argument_type] = argument_type.is_a?(Array) ? [:string] : :string if argument_type
|
57
|
+
scope.options[:type] = :enum
|
58
|
+
end
|
59
|
+
|
60
|
+
if scope.options[:remove_for_enum]
|
61
|
+
singleton_class.remove_method scope.name
|
62
|
+
@auto_scopes.delete(scope)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
46
67
|
def auto_scope_add_definition(attribute, **options)
|
47
68
|
definition = ScopeDefinition.new(attribute, **options)
|
48
69
|
@auto_scopes ||= []
|
@@ -52,10 +73,9 @@ module Scopiform
|
|
52
73
|
end
|
53
74
|
|
54
75
|
def auto_scope_for_alias(alias_name, scope_definition)
|
55
|
-
|
56
|
-
|
57
|
-
singleton_class.send(:alias_method,
|
58
|
-
auto_scope_add_definition(alias_name, prefix: scope_definition.prefix, suffix: scope_definition.suffix, **scope_definition.options)
|
76
|
+
alias_scope_definition = scope_definition.dup
|
77
|
+
alias_scope_definition.attribute = alias_name
|
78
|
+
singleton_class.send(:alias_method, alias_scope_definition.name, scope_definition.name)
|
59
79
|
end
|
60
80
|
end
|
61
81
|
end
|
data/lib/scopiform/filters.rb
CHANGED
@@ -11,8 +11,8 @@ module Scopiform
|
|
11
11
|
filters_hash.keys.inject(injecting) { |out, filter_name| resolve_filter(out, filter_name, filters_hash[filter_name], joins) }
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
14
|
+
def apply_sorts(sorts_hash, injecting = all)
|
15
|
+
sorts_hash.keys.inject(injecting) { |out, sort_name| resolve_sort(out, sort_name, sorts_hash[sort_name]) }
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
@@ -31,9 +31,9 @@ module Scopiform
|
|
31
31
|
out.send(filter_name, filter_argument, joins: joins)
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
method_name = "
|
36
|
-
out.send(method_name,
|
34
|
+
def resolve_sort(out, sort_name, sort_argument)
|
35
|
+
method_name = "sort_by_#{sort_name}"
|
36
|
+
out.send(method_name, sort_argument)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/scopiform/helpers.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Scopiform
|
2
2
|
module ReflectionPlugin
|
3
|
-
def add_reflection(
|
4
|
-
super(
|
3
|
+
def add_reflection(record, name, reflection)
|
4
|
+
super(record, name, reflection)
|
5
5
|
|
6
|
-
|
6
|
+
record.reflection_added(name, reflection) if record.respond_to? :reflection_added
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -10,7 +10,18 @@ module Scopiform
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def name
|
13
|
-
|
13
|
+
name_for(attribute)
|
14
|
+
end
|
15
|
+
|
16
|
+
def name_for(attribute_name)
|
17
|
+
"#{prefix}#{attribute}#{suffix}".underscore.to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
def dup
|
21
|
+
duplicate = super
|
22
|
+
duplicate.options = options.dup
|
23
|
+
|
24
|
+
duplicate
|
14
25
|
end
|
15
26
|
end
|
16
27
|
end
|
@@ -24,42 +24,42 @@ module Scopiform
|
|
24
24
|
|
25
25
|
auto_scope_add(
|
26
26
|
name,
|
27
|
-
|
27
|
+
proc { |value| where(name_sym => value) },
|
28
28
|
suffix: '_in',
|
29
29
|
argument_type: [type]
|
30
30
|
)
|
31
31
|
|
32
32
|
auto_scope_add(
|
33
33
|
name,
|
34
|
-
|
34
|
+
proc { |value| where.not(name_sym => value) },
|
35
35
|
suffix: '_not_in',
|
36
36
|
argument_type: [type]
|
37
37
|
)
|
38
38
|
|
39
39
|
auto_scope_add(
|
40
40
|
name,
|
41
|
-
|
41
|
+
proc { |value| where(arel_column.lt(value)) },
|
42
42
|
suffix: '_lt',
|
43
43
|
argument_type: type
|
44
44
|
)
|
45
45
|
|
46
46
|
auto_scope_add(
|
47
47
|
name,
|
48
|
-
|
48
|
+
proc { |value| where(arel_column.lteq(value)) },
|
49
49
|
suffix: '_lte',
|
50
50
|
argument_type: type
|
51
51
|
)
|
52
52
|
|
53
53
|
auto_scope_add(
|
54
54
|
name,
|
55
|
-
|
55
|
+
proc { |value| where(arel_column.gt(value)) },
|
56
56
|
suffix: '_gt',
|
57
57
|
argument_type: type
|
58
58
|
)
|
59
59
|
|
60
60
|
auto_scope_add(
|
61
61
|
name,
|
62
|
-
|
62
|
+
proc { |value| where(arel_column.gteq(value)) },
|
63
63
|
suffix: '_gte',
|
64
64
|
argument_type: type
|
65
65
|
)
|
@@ -15,7 +15,9 @@ module Scopiform
|
|
15
15
|
|
16
16
|
def setup_string_and_number_auto_scopes
|
17
17
|
string_numbers = Helpers::STRING_TYPES + Helpers::NUMBER_TYPES
|
18
|
-
string_number_columns = safe_columns.select
|
18
|
+
string_number_columns = safe_columns.select do |column|
|
19
|
+
string_numbers.include?(column.type) && !enum_attribute?(column.name)
|
20
|
+
end
|
19
21
|
string_number_columns.each do |column|
|
20
22
|
name = column.name
|
21
23
|
type = column.type
|
@@ -34,44 +36,50 @@ module Scopiform
|
|
34
36
|
|
35
37
|
auto_scope_add(
|
36
38
|
name,
|
37
|
-
|
39
|
+
proc { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
|
38
40
|
suffix: '_contains',
|
39
|
-
|
41
|
+
argument_type: type,
|
42
|
+
remove_for_enum: true
|
40
43
|
)
|
41
44
|
|
42
45
|
auto_scope_add(
|
43
46
|
name,
|
44
|
-
|
47
|
+
proc { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
|
45
48
|
suffix: '_not_contains',
|
46
|
-
|
49
|
+
argument_type: type,
|
50
|
+
remove_for_enum: true
|
47
51
|
)
|
48
52
|
|
49
53
|
auto_scope_add(
|
50
54
|
name,
|
51
|
-
|
55
|
+
proc { |value| where(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
|
52
56
|
suffix: '_starts_with',
|
53
|
-
|
57
|
+
argument_type: type,
|
58
|
+
remove_for_enum: true
|
54
59
|
)
|
55
60
|
|
56
61
|
auto_scope_add(
|
57
62
|
name,
|
58
|
-
|
63
|
+
proc { |value| where.not(arel_column.matches("#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}%")) },
|
59
64
|
suffix: '_not_starts_with',
|
60
|
-
|
65
|
+
argument_type: type,
|
66
|
+
remove_for_enum: true
|
61
67
|
)
|
62
68
|
|
63
69
|
auto_scope_add(
|
64
70
|
name,
|
65
|
-
|
71
|
+
proc { |value| where(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
|
66
72
|
suffix: '_ends_with',
|
67
|
-
|
73
|
+
argument_type: type,
|
74
|
+
remove_for_enum: true
|
68
75
|
)
|
69
76
|
|
70
77
|
auto_scope_add(
|
71
78
|
name,
|
72
|
-
|
79
|
+
proc { |value| where.not(arel_column.matches("%#{ActiveRecord::Base.sanitize_sql_like(value.to_s)}")) },
|
73
80
|
suffix: '_not_ends_with',
|
74
|
-
|
81
|
+
argument_type: type,
|
82
|
+
remove_for_enum: true
|
75
83
|
)
|
76
84
|
end
|
77
85
|
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.
|
4
|
+
version: 0.2.0
|
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-03-
|
11
|
+
date: 2020-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: warning
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description:
|
56
70
|
email:
|
57
71
|
- jayce.pulsipher@3-form.com
|