services 4.0.2 → 4.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/services/query.rb +24 -1
- data/lib/services/version.rb +1 -1
- data/services.gemspec +1 -0
- data/spec/services/query_spec.rb +17 -5
- data/spec/spec_helper.rb +3 -1
- data/spec/support/activerecord_models_and_services.rb +77 -0
- data/spec/support/test_services.rb +0 -2
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f7714cb0e9390aa539649cc30b178705ef47aa6
|
4
|
+
data.tar.gz: 8821244fee82df7be557f4bbc4591e7d084c2c56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecbfa1b8fe169b98f68fcab6ea905b75a70e51c54872a39bbcc3a6e0b6c838caf18f093df743c0a1a84d61b9de209516205ab0a61114674ccc707ffafe5edd43
|
7
|
+
data.tar.gz: 6b81875b67297940f7fbae262b9a6532dca7aa2497e346940f3e8033bf67d325f9952725e273521fae06ce613a5768d40dca1b1194b0290177315b9f42118cb3
|
data/CHANGELOG.md
CHANGED
data/lib/services/query.rb
CHANGED
@@ -4,6 +4,14 @@ module Services
|
|
4
4
|
|
5
5
|
class << self
|
6
6
|
delegate :call, to: :new
|
7
|
+
|
8
|
+
def convert_condition_objects_to_ids(*class_names)
|
9
|
+
@object_to_id_class_names = class_names
|
10
|
+
end
|
11
|
+
|
12
|
+
def object_to_id_class_names
|
13
|
+
@object_to_id_class_names || []
|
14
|
+
end
|
7
15
|
end
|
8
16
|
|
9
17
|
def call(ids = [], conditions = {})
|
@@ -15,11 +23,26 @@ module Services
|
|
15
23
|
special_conditions = conditions.extract!(:order, :limit, :page, :per_page)
|
16
24
|
special_conditions[:order] = object_table_id unless special_conditions.has_key?(:order)
|
17
25
|
|
18
|
-
scope = object_class.public_send(
|
26
|
+
scope = object_class.public_send(ActiveRecord::VERSION::MAJOR == 3 ? :scoped : :all)
|
19
27
|
scope = scope.where(object_table_id => ids) unless ids.empty?
|
20
28
|
|
21
29
|
unless conditions.empty?
|
30
|
+
self.class.object_to_id_class_names.each do |class_name|
|
31
|
+
if object_or_objects = conditions.delete(class_name)
|
32
|
+
ids = case object_or_objects
|
33
|
+
when Array
|
34
|
+
object_or_objects.map(&:id)
|
35
|
+
when ActiveRecord::Relation
|
36
|
+
object_or_objects.pluck(:id)
|
37
|
+
else
|
38
|
+
[object_or_objects.id]
|
39
|
+
end
|
40
|
+
conditions[:"#{class_name}_id"] = ids.size == 1 ? ids.first : ids
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
22
44
|
scope = process(scope, conditions)
|
45
|
+
|
23
46
|
# If a JOIN is involved, use a subquery to make sure we're getting DISTINCT records.
|
24
47
|
if scope.to_sql =~ / join /i
|
25
48
|
scope = object_class.where(id: scope.select("DISTINCT #{object_table_id}"))
|
data/lib/services/version.rb
CHANGED
data/services.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
28
28
|
gem.add_development_dependency 'redis', '~> 3.0'
|
29
29
|
gem.add_development_dependency 'tries', '~> 0.3'
|
30
30
|
gem.add_development_dependency 'timecop', '~> 0.7'
|
31
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
31
32
|
gem.add_runtime_dependency 'rails', '>= 3.2.0'
|
32
33
|
gem.add_runtime_dependency 'gem_config', '~> 0.3'
|
33
34
|
end
|
data/spec/services/query_spec.rb
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require SUPPORT_DIR.join('activerecord_models_and_services')
|
2
3
|
|
3
4
|
describe Services::Query do
|
4
5
|
include_context 'capture logs'
|
5
6
|
|
6
|
-
let(:base_find) { Services::Models::BaseFind }
|
7
|
-
|
8
7
|
it 'has call logging disabled by default' do
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
expect { Services::Posts::Find.call [] }.to_not change { logs }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.convert_condition_objects_to_ids' do
|
12
|
+
let(:comment) { Comment.create! }
|
13
|
+
let(:comments) { (1..3).map { Comment.create! } }
|
14
|
+
|
15
|
+
it 'converts condition objects to ids' do
|
16
|
+
{
|
17
|
+
comment => comment.id,
|
18
|
+
comments => comments.map(&:id),
|
19
|
+
Comment.all => Comment.all.pluck(:id)
|
20
|
+
}.each do |condition_before, condition_after|
|
21
|
+
expect { Services::Posts::FindRaiseConditions.call [], comment: condition_before }.to raise_error({ comment_id: condition_after }.to_json)
|
22
|
+
end
|
23
|
+
end
|
12
24
|
end
|
13
25
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,7 +19,9 @@ START_TIMEOUT = 5
|
|
19
19
|
SIDEKIQ_TIMEOUT = 20
|
20
20
|
REDIS_PORT = 6479
|
21
21
|
|
22
|
-
|
22
|
+
%w(shared helpers test_services).each do |file|
|
23
|
+
require SUPPORT_DIR.join(file)
|
24
|
+
end
|
23
25
|
|
24
26
|
Services.configure do |config|
|
25
27
|
config.redis = Redis.new
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define do
|
6
|
+
create_table :posts, force: true do |t|
|
7
|
+
t.string :title
|
8
|
+
t.text :body
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :comments, force: true do |t|
|
12
|
+
t.string :body
|
13
|
+
t.references :post
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Post < ActiveRecord::Base
|
18
|
+
has_many :comments
|
19
|
+
end
|
20
|
+
|
21
|
+
class Comment < ActiveRecord::Base
|
22
|
+
belongs_to :post
|
23
|
+
end
|
24
|
+
|
25
|
+
module Services
|
26
|
+
module Posts
|
27
|
+
class FindRaiseConditions < Services::Query
|
28
|
+
convert_condition_objects_to_ids :comment
|
29
|
+
|
30
|
+
private def process(scope, conditions)
|
31
|
+
raise conditions.to_json
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Services
|
38
|
+
module Posts
|
39
|
+
class Find < Services::Query
|
40
|
+
convert_condition_objects_to_ids :comment
|
41
|
+
|
42
|
+
private def process(scope, conditions)
|
43
|
+
conditions.each do |k, v|
|
44
|
+
case k
|
45
|
+
when :title, :body
|
46
|
+
scope = scope.where(k => v)
|
47
|
+
when :comment_id
|
48
|
+
scope = scope.joins(:comments).where("#{Comment.table_name}.id" => v)
|
49
|
+
else
|
50
|
+
raise ArgumentError, "Unexpected condition: #{k}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
scope
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
module Services
|
60
|
+
module Comments
|
61
|
+
class Find < Services::Query
|
62
|
+
convert_condition_objects_to_ids :post
|
63
|
+
|
64
|
+
private def process(scope, conditions)
|
65
|
+
conditions.each do |k, v|
|
66
|
+
case k
|
67
|
+
when :body, :post_id
|
68
|
+
scope = scope.where(k => v)
|
69
|
+
else
|
70
|
+
raise ArgumentError, "Unexpected condition: #{k}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
scope
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
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: 4.0
|
4
|
+
version: 4.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: 2015-
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0.7'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: sqlite3
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.3'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.3'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: rails
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -172,6 +186,7 @@ files:
|
|
172
186
|
- spec/services/modules/uniqueness_checker_spec.rb
|
173
187
|
- spec/services/query_spec.rb
|
174
188
|
- spec/spec_helper.rb
|
189
|
+
- spec/support/activerecord_models_and_services.rb
|
175
190
|
- spec/support/call_proxy.rb
|
176
191
|
- spec/support/helpers.rb
|
177
192
|
- spec/support/log/.gitkeep
|
@@ -212,6 +227,7 @@ test_files:
|
|
212
227
|
- spec/services/modules/uniqueness_checker_spec.rb
|
213
228
|
- spec/services/query_spec.rb
|
214
229
|
- spec/spec_helper.rb
|
230
|
+
- spec/support/activerecord_models_and_services.rb
|
215
231
|
- spec/support/call_proxy.rb
|
216
232
|
- spec/support/helpers.rb
|
217
233
|
- spec/support/log/.gitkeep
|