services 4.0.2 → 4.1.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
  SHA1:
3
- metadata.gz: f848f7e6c1284b6671f565cb1d27d4119cb4a355
4
- data.tar.gz: 3dbce97879f9e9768715839d9151fb2e3e0bd3ee
3
+ metadata.gz: 8f7714cb0e9390aa539649cc30b178705ef47aa6
4
+ data.tar.gz: 8821244fee82df7be557f4bbc4591e7d084c2c56
5
5
  SHA512:
6
- metadata.gz: 3c370c5325be285ad139465aeb88b471570de89dd0b791afcb4d18f83cf9505071edb0234c61f13946ed1bee9e2dda0013e5644607747ed04d23f049fd06149d
7
- data.tar.gz: 5cfe01abb853bf5b6ac99127fecde95f225b23df823a1d46d6b09bee498a2a7d41f72062ed5d8bc3a07937c0ffda9e0d087e7b61a512fdd3df87076500e4c419
6
+ metadata.gz: ecbfa1b8fe169b98f68fcab6ea905b75a70e51c54872a39bbcc3a6e0b6c838caf18f093df743c0a1a84d61b9de209516205ab0a61114674ccc707ffafe5edd43
7
+ data.tar.gz: 6b81875b67297940f7fbae262b9a6532dca7aa2497e346940f3e8033bf67d325f9952725e273521fae06ce613a5768d40dca1b1194b0290177315b9f42118cb3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 4.1.0
2
+
3
+ * Add possibility to automatically convert condition objects to IDs in query
4
+
1
5
  ## 4.0.2
2
6
 
3
7
  * Add null logger
@@ -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(Rails::VERSION::MAJOR == 3 ? :scoped : :all)
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}"))
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '4.0.2'
2
+ VERSION = '4.1.0'
3
3
  end
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
@@ -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
- pending 'Rails has to be loaded to call Query'
10
- expect(base_find.call_logging_disabled).to eq(true)
11
- expect { base_find.call }.to_not change { logs }
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
- Dir[SUPPORT_DIR.join('**', '*.rb')].each { |f| require f }
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
@@ -1,5 +1,3 @@
1
- require 'services/query'
2
-
3
1
  class Model
4
2
  class << self
5
3
  def table_name
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.2
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-04-10 00:00:00.000000000 Z
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