services 7.1.2 → 7.2.0

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: fbc8ee7823c6de0d29bccc1fe0e99019253ced65da5a26fdc2c6204c5d003c47
4
- data.tar.gz: 6c03b5b5a530568641be0b04354250cc7e54ba8dd57234d96c8a357808e256db
3
+ metadata.gz: 7aedebb8369e465e6bc0dbde201a6f2e353ef4fb50b9c7e1568914a6aa5c6bdd
4
+ data.tar.gz: b9fcc3a9b6a1d38de2257bdab6145c68db707e0665d5538430e3db2ac46bef2e
5
5
  SHA512:
6
- metadata.gz: 3ab77b16e9de075c8aace8df5006a3e6a6a9a8af7828c43468f8abea3f178473c14931d8b1e49ac1f47ff930c770cc7993d141f7727888f42944f97f50a5be94
7
- data.tar.gz: bf509b8cb3a1d6825b38fedcb83d202f5f3a82ea5396e1b12f8e8c472eb713ef5eb89379a04f2a95023c3006c876ecfadb000299f765adf1a142c7a83aa302a1
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'
@@ -71,47 +71,64 @@ module Services
71
71
  end
72
72
 
73
73
  conditions.each do |k, v|
74
- case k
75
- when :id_not
76
- scope = scope.where.not(id: v)
77
- when CREATED_BEFORE_AFTER_REGEX
78
- operator = $1 == 'before' ? '<' : '>'
79
- scope = scope.where("#{object_class.table_name}.created_at #{operator} ?", v)
80
- when :order
81
- next unless v
82
-
83
- order = v.split(COMMA_REGEX).map do |order_part|
84
- table_name = order_part[TABLE_NAME_REGEX, 1]
85
- case
86
- when table_name && table_name != object_class.table_name
87
- unless reflection = object_class.reflections.values.detect { |reflection| reflection.table_name == table_name }
88
- fail "Reflection on class #{object_class} with table name #{table_name} not found."
89
- end
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
- if ActiveRecord::VERSION::MAJOR >= 5
92
- scope = scope.left_outer_joins(reflection.name)
93
- else
94
- join_conditions = "LEFT OUTER JOIN #{table_name} ON #{table_name}.#{reflection.foreign_key} = #{object_class.table_name}.id"
95
- if reflection.type
96
- join_conditions << " AND #{table_name}.#{reflection.type} = '#{object_class}'"
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
- scope = scope.joins(join_conditions)
116
+ when !table_name
117
+ order_part.prepend "#{object_class.table_name}."
99
118
  end
100
- when !table_name
101
- order_part.prepend "#{object_class.table_name}."
102
- end
103
- order_part
104
- end.join(', ')
105
-
106
- scope = scope.order(order)
107
- when :limit
108
- scope = scope.limit(v)
109
- when :page
110
- scope = scope.page(v)
111
- when :per_page
112
- scope = scope.per(v)
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
 
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '7.1.2'.freeze
2
+ VERSION = '7.2.0'.freeze
3
3
  end
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.1.2
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-10-28 00:00:00.000000000 Z
11
+ date: 2019-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake