ii_finder 1.1.1 → 2.0.1

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: 452fde95e0ced0ad63b8ccf586d493c1514895d074fbb7ae7c4c8e105ae66fb4
4
- data.tar.gz: b07f0b0c3354b93a6b189187ba423605169709b8a4fb56fe57cb8efc92d9c6f0
3
+ metadata.gz: 0af875699d606b3d6b00fc1dba12000c1eea5767d029b5bd125b10f7ef3cdabd
4
+ data.tar.gz: e865e05b213f6e69522d3d9df5b96b2d2bffed9cfe62dd15e9e3ad0e964677bb
5
5
  SHA512:
6
- metadata.gz: c973652162caad6088d84acee6605344679976504869a35c4687b8332e825ec5778cf8b605ae0882573f1998be02ffabdb29d40c9f91b41976b27dadaebaceab
7
- data.tar.gz: d56881a31d2aaa55052094cb1f0e26832339a9ee2478d2a9a18580ef2aef66a6068259230179ca72129e683fb5c97925d28ab8285496614221eefb898b007f35
6
+ metadata.gz: 89f8b24b33d997bf4f530673cf7066390864fbf3d4dbe56bd871edef522664febf803f4c06cd906a6792837dbb08336973c9f8538b2ca9c626f591d9939dee0c
7
+ data.tar.gz: 8d65c22603983ed0cbd3c7dccdcc349f8f8466be5bf76be8241e45c7181db63c6d36bed2eb5431323da5ad74a4935e12b6cceaad79f615e0660a4aab4f955d38
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.0.1
4
+
5
+ * Return relation after callbacks called.
6
+
7
+ ## 2.0.0
8
+
9
+ * Replace chain feature with coactive.
10
+
11
+ ## 1.2.0
12
+
13
+ * Add chain feature.
14
+
15
+ ## 1.1.2
16
+
17
+ * Try fetch prior to calling instance method.
18
+
3
19
  ## 1.1.1
4
20
 
5
21
  * Check arel_table existence for model.
data/README.md CHANGED
@@ -24,14 +24,14 @@ Then execute:
24
24
  Prepare model:
25
25
 
26
26
  ```ruby
27
- class User < ActiveRecord::Base
27
+ class Item < ActiveRecord::Base
28
28
  end
29
29
  ```
30
30
 
31
31
  Prepare finder:
32
32
 
33
33
  ```ruby
34
- class UsersFinder < IIFinder::Base
34
+ class ItemsFinder < IIFinder::Base
35
35
  parameters :name
36
36
 
37
37
  def name(value)
@@ -43,15 +43,15 @@ end
43
43
  Use finder as follows:
44
44
 
45
45
  ```ruby
46
- UsersFinder.call(name: 'NAME').to_sql
47
- #=> SELECT "users".* FROM "users" WHERE "users"."name" = 'NAME'
46
+ ItemsFinder.call(name: 'NAME').to_sql
47
+ #=> SELECT "items".* FROM "items" WHERE "items"."name" = 'NAME'
48
48
  ```
49
49
 
50
50
  You can also specify relation as first argument:
51
51
 
52
52
  ```ruby
53
- UsersFinder.call(User.where(id: [1, 2, 3]), name: 'NAME').to_sql
54
- #=> SELECT "users".* FROM "users" WHERE "users"."id" IN (1, 2, 3) AND "users"."name" = 'NAME'
53
+ ItemsFinder.call(Item.where(id: [1, 2, 3]), name: 'NAME').to_sql
54
+ #=> SELECT "items".* FROM "items" WHERE "items"."id" IN (1, 2, 3) AND "items"."name" = 'NAME'
55
55
  ```
56
56
 
57
57
  ### Finder
@@ -61,7 +61,7 @@ Finder method will not be called when the value of parameter is blank.
61
61
  If you want to receive such value, set `allow_blank` as follows:
62
62
 
63
63
  ```ruby
64
- class UsersFinder < IIFinder::Base
64
+ class ItemsFinder < IIFinder::Base
65
65
  parameters :name, allow_blank: true
66
66
 
67
67
  def name(value)
@@ -69,14 +69,14 @@ class UsersFinder < IIFinder::Base
69
69
  end
70
70
  end
71
71
 
72
- UsersFinder.call(name: '').to_sql
73
- #=> SELECT "users".* FROM "users" WHERE "users"."name" = ''
72
+ ItemsFinder.call(name: '').to_sql
73
+ #=> SELECT "items".* FROM "items" WHERE "items"."name" = ''
74
74
  ```
75
75
 
76
76
  Finder has following attributes:
77
77
 
78
78
  ```ruby
79
- class UsersFinder < IIFinder::Base
79
+ class ItemsFinder < IIFinder::Base
80
80
  parameters :name
81
81
 
82
82
  def name(value)
@@ -87,10 +87,10 @@ class UsersFinder < IIFinder::Base
87
87
  end
88
88
  end
89
89
 
90
- UsersFinder.call(name: 'NAME')
91
- #=> relation: #<User::ActiveRecord_Relation:
90
+ ItemsFinder.call(name: 'NAME')
91
+ #=> relation: #<Item::ActiveRecord_Relation:
92
92
  # criteria: {:name=>'NAME'}
93
- # model: User
93
+ # model: Item
94
94
  # table: #<Arel::Table ...>
95
95
  ```
96
96
 
@@ -102,7 +102,7 @@ IIFinder.configure do |config|
102
102
  config.merge_relation = false
103
103
  end
104
104
 
105
- class UsersFinder < IIFinder::Base
105
+ class ItemsFinder < IIFinder::Base
106
106
  parameters :name
107
107
 
108
108
  def name(value)
@@ -110,8 +110,8 @@ class UsersFinder < IIFinder::Base
110
110
  end
111
111
  end
112
112
 
113
- UsersFinder.call(name: 'NAME').to_sql
114
- #=> SELECT "users".* FROM "users" WHERE "users"."name" = 'NAME'
113
+ ItemsFinder.call(name: 'NAME').to_sql
114
+ #=> SELECT "items".* FROM "items" WHERE "items"."name" = 'NAME'
115
115
  ```
116
116
 
117
117
  #### Callbacks
@@ -125,7 +125,7 @@ Following callbacks are available.
125
125
  For example:
126
126
 
127
127
  ```ruby
128
- class UsersFinder < IIFinder::Base
128
+ class ItemsFinder < IIFinder::Base
129
129
  after_call :default_order
130
130
 
131
131
  def default_order
@@ -133,14 +133,45 @@ class UsersFinder < IIFinder::Base
133
133
  end
134
134
  end
135
135
 
136
- UsersFinder.call.to_sql
137
- #=> SELECT "users".* FROM "users" ORDER BY "users"."id" DESC
136
+ ItemsFinder.call.to_sql
137
+ #=> SELECT "items".* FROM "items" ORDER BY "items"."id" DESC
138
138
  ```
139
139
 
140
140
  Note that finder does not handle the return value of callback.
141
141
  When you want to update `@relation` in the callback,
142
142
  reassign `@relation` or use methods like `where!` or `order!`.
143
143
 
144
+ #### Coactors
145
+
146
+ You can chain multiple finders by using `coact`. For example:
147
+
148
+ ```ruby
149
+ class NameFinder < IIFinder::Base
150
+ parameters :name
151
+
152
+ def name(value)
153
+ @relation.where(name: value)
154
+ end
155
+ end
156
+
157
+ class AgeFinder < IIFinder::Base
158
+ parameters :age
159
+
160
+ def age(value)
161
+ @relation.where(age: value)
162
+ end
163
+ end
164
+
165
+ class ItemsFinder < IIFinder::Base
166
+ coact NameFinder, AgeFinder
167
+ end
168
+
169
+ ItemsFinder.call(Item.all, name: 'name', age: 10).to_sql
170
+ #=> SELECT "items".* FROM "items" WHERE "items"."name" = 'name' AND "items"."age" = 10
171
+ ```
172
+
173
+ See [coactive](https://github.com/kanety/coactive) for more `coact` examples:
174
+
144
175
  ### Lookup for model
145
176
 
146
177
  Finder lookups related model by its class name when the first argument of `call` is not relation.
@@ -148,30 +179,30 @@ So the name of finder class should be composed of the name of model class.
148
179
  For example:
149
180
 
150
181
  ```ruby
151
- class User < ActiveRecord::Base
182
+ class Item < ActiveRecord::Base
152
183
  end
153
184
 
154
- class UsersFinder < IIFinder::Base
185
+ class ItemsFinder < IIFinder::Base
155
186
  end
156
187
 
157
- IIFinder::Base.lookup(UsersFinder)
158
- #=> User
188
+ IIFinder::Base.lookup(ItemsFinder)
189
+ #=> Item
159
190
  ```
160
191
 
161
192
  Note that superclass of finder is also looked up until related model is found.
162
193
 
163
194
  ```ruby
164
- class User < ActiveRecord::Base
195
+ class Item < ActiveRecord::Base
165
196
  end
166
197
 
167
- class UsersFinder < IIFinder::Base
198
+ class ItemsFinder < IIFinder::Base
168
199
  end
169
200
 
170
- class InheritedUsersFinder < UsersFinder
201
+ class InheritedItemsFinder < ItemsFinder
171
202
  end
172
203
 
173
- IIFinder::Base.lookup(InheritedUsersFinder)
174
- #=> User
204
+ IIFinder::Base.lookup(InheritedItemsFinder)
205
+ #=> Item
175
206
  ```
176
207
 
177
208
  ### Scope for model
@@ -179,12 +210,12 @@ IIFinder::Base.lookup(InheritedUsersFinder)
179
210
  In case you want to call finder from model, include `IIFinder::Scope` into model as follows:
180
211
 
181
212
  ```ruby
182
- class User < ActiveRecord::Base
213
+ class Item < ActiveRecord::Base
183
214
  include IIFinder::Scope
184
215
  end
185
216
 
186
- User.finder_scope(name: 'NAME').to_sql
187
- #=> SELECT "users".* FROM "users" WHERE "users"."name" = 'NAME'
217
+ Item.finder_scope(name: 'NAME').to_sql
218
+ #=> SELECT "items".* FROM "items" WHERE "items"."name" = 'NAME'
188
219
  ```
189
220
 
190
221
  ### Logging
@@ -199,7 +230,7 @@ IIFinder::LogSubscriber.attach_to :ii_finder
199
230
  This subscriber will write logs in debug mode as the following example:
200
231
 
201
232
  ```
202
- Called UsersFinder with {:id=>1} (Duration: 9.9ms, Allocations: 915)
233
+ Called ItemsFinder with {:id=>1} (Duration: 9.9ms, Allocations: 915)
203
234
  ```
204
235
 
205
236
  ## Contributing
data/ii_finder.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_dependency "activesupport", ">= 5.0"
21
+ spec.add_dependency "coactive", ">= 0.1"
21
22
 
22
23
  spec.add_development_dependency "rails", ">= 5.0"
23
24
  spec.add_development_dependency "sqlite3"
@@ -5,6 +5,7 @@ require_relative 'parameters'
5
5
  require_relative 'callbacks'
6
6
  require_relative 'instrumentation'
7
7
  require_relative 'lookup'
8
+ require_relative 'coactors'
8
9
 
9
10
  module IIFinder
10
11
  class Base
@@ -13,5 +14,6 @@ module IIFinder
13
14
  include Callbacks
14
15
  include Instrumentation
15
16
  include Lookup
17
+ include Coactors
16
18
  end
17
19
  end
@@ -13,6 +13,8 @@ module IIFinder
13
13
  run_callbacks :call do
14
14
  super
15
15
  end
16
+
17
+ @relation
16
18
  end
17
19
 
18
20
  class_methods do
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIFinder
4
+ module Coactors
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Coactive::Base
9
+
10
+ configure_coactive do |config|
11
+ config.load_paths = ['app/finders']
12
+ config.class_suffix = 'Finder'
13
+ config.use_cache = true
14
+ config.lookup_superclass_until = ['ActiveRecord::Base', 'ActiveModel::Base']
15
+ end
16
+
17
+ class << self
18
+ alias_method :chain, :coact
19
+ end
20
+ end
21
+
22
+ def call
23
+ coactors.each do |finder|
24
+ merge_relation!(finder.call(*@_args))
25
+ end
26
+ super
27
+ end
28
+ end
29
+ end
@@ -9,6 +9,7 @@ module IIFinder
9
9
  end
10
10
 
11
11
  def initialize(*args)
12
+ @_args = args;
12
13
  if args.size == 0 || args.size == 1
13
14
  @model = self.class.lookup
14
15
  raise IIFinder::Error.new("could not find model for #{self.class}") unless @model
@@ -26,7 +27,7 @@ module IIFinder
26
27
  self.class._parameters.each do |param|
27
28
  value = fetch_criteria(param.name)
28
29
  if value.present? || param.allow_blank?
29
- call_method(param.name, value)
30
+ merge_relation!(send(param.name, value))
30
31
  end
31
32
  end
32
33
 
@@ -34,18 +35,16 @@ module IIFinder
34
35
  end
35
36
 
36
37
  def fetch_criteria(name)
37
- if @criteria.respond_to?(name)
38
- @criteria.send(name)
39
- elsif @criteria.respond_to?(:fetch)
38
+ if @criteria.respond_to?(:fetch)
40
39
  @criteria.fetch(name, nil)
40
+ elsif @criteria.respond_to?(name)
41
+ @criteria.send(name)
41
42
  end
42
43
  end
43
44
 
44
- def call_method(name, value)
45
- result = send(name, value)
46
-
47
- if result.respond_to?(:merge) && Config.merge_relation
48
- @relation = @relation.merge(result)
45
+ def merge_relation!(relation)
46
+ if relation.respond_to?(:merge) && Config.merge_relation
47
+ @relation = @relation.merge(relation)
49
48
  end
50
49
  end
51
50
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIFinder
4
- VERSION = '1.1.1'
4
+ VERSION = '2.0.1'
5
5
  end
data/lib/ii_finder.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'active_support'
2
+ require 'coactive'
2
3
 
3
4
  require 'ii_finder/version'
4
5
  require 'ii_finder/config'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ii_finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-21 00:00:00.000000000 Z
11
+ date: 2021-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coactive
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rails
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -120,6 +134,7 @@ files:
120
134
  - lib/ii_finder.rb
121
135
  - lib/ii_finder/base.rb
122
136
  - lib/ii_finder/callbacks.rb
137
+ - lib/ii_finder/coactors.rb
123
138
  - lib/ii_finder/config.rb
124
139
  - lib/ii_finder/core.rb
125
140
  - lib/ii_finder/errors.rb
@@ -148,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
163
  - !ruby/object:Gem::Version
149
164
  version: '0'
150
165
  requirements: []
151
- rubygems_version: 3.1.2
166
+ rubygems_version: 3.1.6
152
167
  signing_key:
153
168
  specification_version: 4
154
169
  summary: A base finder to support building relations from parameters