services 7.1.2 → 7.2.0
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/services.rb +5 -0
- data/lib/services/query.rb +55 -38
- data/lib/services/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: 7aedebb8369e465e6bc0dbde201a6f2e353ef4fb50b9c7e1568914a6aa5c6bdd
|
4
|
+
data.tar.gz: b9fcc3a9b6a1d38de2257bdab6145c68db707e0665d5538430e3db2ac46bef2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08637ac5fe53454bd404fd069f00179a010cfb0f32295948b403796b7d24ec106b9485a732f2c37d05e9983bc8188ae5e76152c871c77a9fa1f6008ae3e4dfbb'
|
7
|
+
data.tar.gz: f494e1694f08b16debb2d21d6d82a2473b46822e5ca433d64e5c74b28d85b074937bd94b440c277593253bd5e67b2c1a2b2aaf0058d1155b92ba7880dbf4e772
|
data/lib/services.rb
CHANGED
@@ -11,11 +11,16 @@ module Services
|
|
11
11
|
with_configuration do
|
12
12
|
has :logger, default: Services::Logger::Null.new
|
13
13
|
has :redis
|
14
|
+
has :allowed_class_methods_in_queries, default: {}
|
14
15
|
end
|
15
16
|
|
16
17
|
def self.redis
|
17
18
|
@redis ||= self.configuration.redis || (defined?(Redis.current) && Redis.current) or fail RedisNotFound, 'Redis not configured.'
|
18
19
|
end
|
20
|
+
|
21
|
+
def self.allow_class_method_in_queries(klass, method)
|
22
|
+
(configuration.allowed_class_methods_in_queries[klass.to_s] ||= Set.new) << method.to_sym
|
23
|
+
end
|
19
24
|
end
|
20
25
|
|
21
26
|
require_relative 'services/version'
|
data/lib/services/query.rb
CHANGED
@@ -71,47 +71,64 @@ module Services
|
|
71
71
|
end
|
72
72
|
|
73
73
|
conditions.each do |k, v|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
74
|
+
if (Services.configuration.allowed_class_methods_in_queries[object_class.to_s] || []).include?(k)
|
75
|
+
case object_class.method(k).arity
|
76
|
+
when 0
|
77
|
+
unless v == true
|
78
|
+
raise ArgumentError, "Method #{k} of class #{self} takes no arguments, so `true` must be passed as the value for this param, not #{v} (#{v.class})."
|
79
|
+
end
|
80
|
+
scope = scope.public_send(k)
|
81
|
+
when 1, -1
|
82
|
+
scope = scope.public_send(k, v)
|
83
|
+
else
|
84
|
+
unless v.is_a?(Array)
|
85
|
+
raise ArgumentError, "Method #{k} of class #{self} takes more than one argument, so an array must be passed as the value for this param, not #{v} (#{v.class})."
|
86
|
+
end
|
87
|
+
scope = scope.public_send(k, *v)
|
88
|
+
end
|
89
|
+
else
|
90
|
+
case k
|
91
|
+
when :id_not
|
92
|
+
scope = scope.where.not(id: v)
|
93
|
+
when CREATED_BEFORE_AFTER_REGEX
|
94
|
+
operator = $1 == 'before' ? '<' : '>'
|
95
|
+
scope = scope.where("#{object_class.table_name}.created_at #{operator} ?", v)
|
96
|
+
when :order
|
97
|
+
next unless v
|
98
|
+
|
99
|
+
order = v.split(COMMA_REGEX).map do |order_part|
|
100
|
+
table_name = order_part[TABLE_NAME_REGEX, 1]
|
101
|
+
case
|
102
|
+
when table_name && table_name != object_class.table_name
|
103
|
+
unless reflection = object_class.reflections.values.detect { |reflection| reflection.table_name == table_name }
|
104
|
+
fail "Reflection on class #{object_class} with table name #{table_name} not found."
|
105
|
+
end
|
90
106
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
107
|
+
if ActiveRecord::VERSION::MAJOR >= 5
|
108
|
+
scope = scope.left_outer_joins(reflection.name)
|
109
|
+
else
|
110
|
+
join_conditions = "LEFT OUTER JOIN #{table_name} ON #{table_name}.#{reflection.foreign_key} = #{object_class.table_name}.id"
|
111
|
+
if reflection.type
|
112
|
+
join_conditions << " AND #{table_name}.#{reflection.type} = '#{object_class}'"
|
113
|
+
end
|
114
|
+
scope = scope.joins(join_conditions)
|
97
115
|
end
|
98
|
-
|
116
|
+
when !table_name
|
117
|
+
order_part.prepend "#{object_class.table_name}."
|
99
118
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
else
|
114
|
-
raise ArgumentError, "Unexpected condition: #{k}"
|
119
|
+
order_part
|
120
|
+
end.join(', ')
|
121
|
+
|
122
|
+
scope = scope.order(order)
|
123
|
+
when :limit
|
124
|
+
scope = scope.limit(v)
|
125
|
+
when :page
|
126
|
+
scope = scope.page(v)
|
127
|
+
when :per_page
|
128
|
+
scope = scope.per(v)
|
129
|
+
else
|
130
|
+
raise ArgumentError, "Unexpected condition: #{k}"
|
131
|
+
end
|
115
132
|
end
|
116
133
|
end
|
117
134
|
|
data/lib/services/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: services
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.
|
4
|
+
version: 7.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Meurer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|