services 7.0.3 → 7.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 061cb612feb2b4f1da9bb7117fac09c029eff49c7372db3e8a8d7eacf85de384
4
- data.tar.gz: 1d32e9e17ebe5c27413d1aad1ec243ecd7285e91e043969592964b7f002e1c7a
3
+ metadata.gz: 6fe25182ce1c9112cb5038acb3c2b2142bb62e4cc92457d4db4da8ef5b0a323a
4
+ data.tar.gz: 395f8a026b22de6fa716faba2e336c75542f79b23fcc0c7c7ed6f8d8926a0008
5
5
  SHA512:
6
- metadata.gz: 66093db9f4b3199596f016c99a82f735e8576daeff3d6fc73dcaf4100b08fceace291e8eb9bf0c7f4a6a1f076aed62aa0bce81e31616914c1160056f6bd04af6
7
- data.tar.gz: 6409121bf266995e1ad13c124f487ca5f5312dbbde47785bea5c088f6f6986a8b15ea5bffe40fa47d0bde7a12101df8387a53a4681f0d22cea48ce5a6c4d420f
6
+ metadata.gz: 7dd47187a80a3f174d7699160c1e4ae0606fa145bbed2b07c8c363a8b4eebde80586031ee6035ee0b9637be33af3b560a8e29d20789ab7db5338f6487c152802
7
+ data.tar.gz: 938b7cc0457dbed15ef374b8ba9771c6f91de395d095f8864ba7959f848731d5d60262f4c2110809eecb741eb8506afb6f7e658ba449e74b197dfd770d0062a6
@@ -2,6 +2,10 @@ module Services
2
2
  class Query
3
3
  include ObjectClass
4
4
 
5
+ COMMA_REGEX = /\s*,\s*/
6
+ TABLE_NAME_REGEX = /\A([A-Za-z0-9_]+)\./
7
+ CREATED_BEFORE_AFTER_REGEX = /\Acreated_(before|after)\z/
8
+
5
9
  class << self
6
10
  delegate :call, to: :new
7
11
 
@@ -33,10 +37,12 @@ module Services
33
37
  conditions[:order] = object_table_id
34
38
  end
35
39
 
36
- scope = conditions.delete(:scope).try(:dup) || object_class.public_send(ActiveRecord::VERSION::MAJOR == 3 ? :scoped : :all)
37
- scope = scope.where(object_table_id => ids) unless ids.empty?
40
+ scope = conditions.delete(:scope).try(:dup) || object_class.all
41
+ if ids.any?
42
+ scope = scope.where(object_table_id => ids)
43
+ end
38
44
 
39
- unless conditions.empty?
45
+ if conditions.any?
40
46
  self.class.object_to_id_class_names.each do |class_name|
41
47
  if object_or_objects = conditions.delete(class_name)
42
48
  ids = case object_or_objects
@@ -68,32 +74,36 @@ module Services
68
74
  case k
69
75
  when :id_not
70
76
  scope = scope.where.not(id: v)
71
- when /\Acreated_(before|after)\z/
77
+ when CREATED_BEFORE_AFTER_REGEX
72
78
  operator = $1 == 'before' ? '<' : '>'
73
79
  scope = scope.where("#{object_class.table_name}.created_at #{operator} ?", v)
74
80
  when :order
75
81
  next unless v
76
- case v
77
- when 'random'
78
- order = 'RANDOM()'
79
- when /\A([A-Za-z0-9_]+)\./
80
- table_name = $1
81
- unless table_name == object_class.table_name
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
82
87
  unless reflection = object_class.reflections.values.detect { |reflection| reflection.table_name == table_name }
83
88
  fail "Reflection on class #{object_class} with table name #{table_name} not found."
84
89
  end
85
- # TODO: In Rails 5, we can use #left_outer_joins
86
- # http://blog.bigbinary.com/2016/03/24/support-for-left-outer-joins-in-rails-5.html
87
- join_conditions = "LEFT OUTER JOIN #{table_name} ON #{table_name}.#{reflection.foreign_key} = #{object_class.table_name}.id"
88
- if reflection.type
89
- join_conditions << " AND #{table_name}.#{reflection.type} = '#{object_class}'"
90
+
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}'"
97
+ end
98
+ scope = scope.joins(join_conditions)
90
99
  end
91
- scope = scope.joins(join_conditions)
100
+
101
+ order = order_part
102
+ when !table_name
103
+ order = "#{object_class.table_name}.#{order_part}"
92
104
  end
93
- order = v
94
- else
95
- order = "#{object_class.table_name}.#{v}"
96
- end
105
+ end.join(', ')
106
+
97
107
  scope = scope.order(order)
98
108
  when :limit
99
109
  scope = scope.limit(v)
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '7.0.3'.freeze
2
+ VERSION = '7.1.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.0.3
4
+ version: 7.1.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-06-27 00:00:00.000000000 Z
11
+ date: 2019-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake