baseapi 0.1.2 → 0.1.3
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/README.md +100 -27
- data/lib/baseapi/active_record/base_extension.rb +38 -2
- data/lib/baseapi/active_record/relation_extension.rb +10 -14
- data/lib/baseapi/version.rb +1 -1
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dca945b60e066f84367a241d9ffdf4f63b69742a
|
4
|
+
data.tar.gz: abd87f5100db1cb5418a82801edc7efcf507dd27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c009cb8fc952ec300a1194d82c3e8d83b70351455a4cd6d8069bd6a236e8047e29e905ea2d787b41f87361928e2cc030647ae3e01d7bce735912bc002af821d4
|
7
|
+
data.tar.gz: 06cbe31f266e92e7d20f55c7ddbe1401a8c0f7a80d12a539f255a2bbe3d5382e419ceb372f90b6840cf1512b331ba7a0780241ed8353f641cdb82f5e982129a1
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ Create a Model (app/models/user.rb):
|
|
32
32
|
|
33
33
|
Extend the BaseApiController is when you create a Controller (app/controllers/users_controller.rb):
|
34
34
|
|
35
|
-
class
|
35
|
+
class UsersController < BaseApiController
|
36
36
|
end
|
37
37
|
|
38
38
|
Routing configuration (config/routes.rb):
|
@@ -48,11 +48,13 @@ Routing configuration (config/routes.rb):
|
|
48
48
|
|
49
49
|
Corresponding API:
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
| url | action | method |
|
52
|
+
|------------------|---------|-------------|
|
53
|
+
| /users.json | index | GET |
|
54
|
+
| /users/{id}.json | show | GET |
|
55
|
+
| /users.json | create | POST |
|
56
|
+
| /users/{id}.json | update | PATCH / PUT |
|
57
|
+
| /users/{id}.json | destroy | DELETE |
|
56
58
|
|
57
59
|
|
58
60
|
### Examples
|
@@ -71,8 +73,8 @@ Users table data
|
|
71
73
|
|
72
74
|
| id | name | company_id |
|
73
75
|
|----|----------|------------|
|
74
|
-
| 1 |
|
75
|
-
| 2 |
|
76
|
+
| 1 | hoge | 1 |
|
77
|
+
| 2 | huga | 2 |
|
76
78
|
|
77
79
|
Company table data
|
78
80
|
|
@@ -87,21 +89,37 @@ Get all
|
|
87
89
|
|
88
90
|
GET /users.json
|
89
91
|
|
92
|
+
Specify the count
|
93
|
+
|
94
|
+
GET /users.json?count=10
|
95
|
+
|
96
|
+
Specify the page
|
97
|
+
|
98
|
+
GET /users.json?page=2
|
99
|
+
|
100
|
+
Specify the sorting order
|
101
|
+
|
102
|
+
GET /users.json?order=desc&orderby=name
|
103
|
+
|
90
104
|
Specify the name
|
91
105
|
|
92
|
-
GET /users.json?name=
|
106
|
+
GET /users.json?name=hoge
|
93
107
|
|
94
108
|
Specify multiple possible
|
95
109
|
|
96
|
-
GET /users.json?name[]=
|
110
|
+
GET /users.json?name[]=hoge&name[]=huga
|
97
111
|
|
98
112
|
Specify the belongs to company name
|
99
113
|
|
100
|
-
|
114
|
+
Note that this is a single
|
115
|
+
|
116
|
+
GET /users.json?company[name]=Google
|
101
117
|
|
102
118
|
Specify the has many users name
|
103
119
|
|
104
|
-
|
120
|
+
Note that this is a multiple
|
121
|
+
|
122
|
+
GET /companies.json?users[name]=hoge
|
105
123
|
|
106
124
|
#### action show
|
107
125
|
|
@@ -153,64 +171,93 @@ You can corresponding to the logical deletion, if you want to search condition t
|
|
153
171
|
Get all
|
154
172
|
|
155
173
|
class User < ActiveRecord::Base
|
156
|
-
|
157
|
-
|
174
|
+
module ClassMethods
|
175
|
+
def self._all
|
176
|
+
self.all # default
|
177
|
+
end
|
158
178
|
end
|
159
179
|
|
160
180
|
delete
|
161
181
|
|
162
182
|
class User < ActiveRecord::Base
|
163
183
|
def _destroy
|
184
|
+
self.destroy # default
|
164
185
|
end
|
165
186
|
end
|
166
187
|
|
167
188
|
column search
|
168
189
|
|
169
190
|
class User < ActiveRecord::Base
|
170
|
-
|
171
|
-
|
191
|
+
module ClassMethods
|
192
|
+
def self._where(models, column, values)
|
193
|
+
column_match(models, column, values) # default
|
194
|
+
end
|
172
195
|
end
|
173
196
|
|
174
197
|
name column search
|
175
198
|
|
176
199
|
class User < ActiveRecord::Base
|
177
|
-
|
178
|
-
|
200
|
+
module ClassMethods
|
201
|
+
def self._where_name(models, column, values)
|
202
|
+
column_match(models, column, values) # default
|
203
|
+
end
|
179
204
|
end
|
180
205
|
|
181
206
|
belongs_to search
|
182
207
|
|
183
208
|
class User < ActiveRecord::Base
|
184
|
-
|
185
|
-
|
209
|
+
module ClassMethods
|
210
|
+
def self._belongs_to(models, table, hash)
|
211
|
+
relation_match(models, table, hash) # default
|
212
|
+
end
|
186
213
|
end
|
187
214
|
|
188
215
|
company belongs_to search
|
189
216
|
|
190
217
|
class User < ActiveRecord::Base
|
191
|
-
|
192
|
-
|
218
|
+
module ClassMethods
|
219
|
+
def self._belongs_to_company(models, table, hash)
|
220
|
+
relation_match(models, table, hash) # default
|
221
|
+
end
|
193
222
|
end
|
194
223
|
|
195
224
|
has_many search
|
196
225
|
|
197
226
|
class Company < ActiveRecord::Base
|
198
|
-
|
199
|
-
|
227
|
+
module ClassMethods
|
228
|
+
def self._has_many(models, table, hash)
|
229
|
+
relation_match(models, table, hash) # default
|
230
|
+
end
|
200
231
|
end
|
201
232
|
|
202
233
|
users has_many search
|
203
234
|
|
204
235
|
class Company < ActiveRecord::Base
|
205
|
-
|
206
|
-
|
236
|
+
module ClassMethods
|
237
|
+
def self._has_many_users(models, table, hash)
|
238
|
+
relation_match(models, table, hash) # default
|
239
|
+
end
|
207
240
|
end
|
208
241
|
|
209
242
|
|
210
|
-
There is a useful function for the
|
243
|
+
There is a useful function for the table Search
|
211
244
|
By default, it looks like the following
|
212
245
|
'Like' search and, you can change the 'and' and 'or'
|
213
246
|
|
247
|
+
Simply If you want to override the search processing of the name column
|
248
|
+
column_match, you can use the column_like function.
|
249
|
+
|
250
|
+
class User < ActiveRecord::Base
|
251
|
+
def self._where_name(models, column, values)
|
252
|
+
column_match(models, column, values, operator:'or') # default is match OR
|
253
|
+
# column_like(models, column, values, operator:'or') # LIKE OR
|
254
|
+
# column_like(models, column, values, operator:'and') # LIKE AND
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
If the search process of the related table is to override
|
259
|
+
relation_match, you can use the relation_like function.
|
260
|
+
|
214
261
|
class User < ActiveRecord::Base
|
215
262
|
def self._belongs_to(models, table, hash)
|
216
263
|
relation_match(models, table, hash, operator:'or') # default is match OR
|
@@ -221,6 +268,32 @@ By default, it looks like the following
|
|
221
268
|
|
222
269
|
The short so please read the [code](https://github.com/arakawamoriyuki/baseapi/blob/master/lib/baseapi/active_record/base_extension.rb) for more information
|
223
270
|
|
271
|
+
|
272
|
+
### jbuilder
|
273
|
+
|
274
|
+
It uses basically
|
275
|
+
|
276
|
+
/app/views/baseapi/ooo.json.jbuilder
|
277
|
+
|
278
|
+
but you can also make
|
279
|
+
|
280
|
+
/app/views/{models}/ooo.json.jbuilder
|
281
|
+
|
282
|
+
It will return to a single data (action:show,create,delete,update)
|
283
|
+
|
284
|
+
model.json.jbuilder
|
285
|
+
|
286
|
+
It will return multiple data (action:index)
|
287
|
+
|
288
|
+
models.json.jbuilder
|
289
|
+
|
290
|
+
It will return an error content
|
291
|
+
|
292
|
+
error.json.jbuilder
|
293
|
+
|
294
|
+
[jbuilder details here](https://github.com/rails/jbuilder)
|
295
|
+
|
296
|
+
|
224
297
|
## Development
|
225
298
|
|
226
299
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec baseapi` to use the code located in this directory, ignoring other installed copies of this gem.
|
@@ -13,14 +13,49 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
|
|
13
13
|
self.all
|
14
14
|
end
|
15
15
|
|
16
|
+
|
16
17
|
# override or create method '_where_{column}' if necessary
|
17
18
|
# @param ActiveRecordRelation models
|
18
19
|
# @param String column column name
|
19
|
-
# @param Array
|
20
|
+
# @param Array/String values search values
|
20
21
|
def _where(models, column, values)
|
21
|
-
models
|
22
|
+
column_match(models, column, values)
|
23
|
+
end
|
24
|
+
|
25
|
+
# column exact match search
|
26
|
+
# @param ActiveRecordRelation models
|
27
|
+
# @param String column column name
|
28
|
+
# @param Array/String values search values
|
29
|
+
# @option String operator 'or' or 'and'
|
30
|
+
def column_match(models, column, values, operator:'or')
|
31
|
+
column_call(models, column, values, ->(column, value){
|
32
|
+
"#{column} = '#{value}'"
|
33
|
+
}, operator:operator)
|
34
|
+
end
|
35
|
+
|
36
|
+
# column like search
|
37
|
+
# @param ActiveRecordRelation models
|
38
|
+
# @param String column column name
|
39
|
+
# @param Array/String values search values
|
40
|
+
# @option String operator 'or' or 'and'
|
41
|
+
def column_like(models, column, values, operator:'or')
|
42
|
+
column_call(models, column, values, ->(column, value){
|
43
|
+
"#{column} like '%#{value}%'"
|
44
|
+
}, operator:operator)
|
22
45
|
end
|
23
46
|
|
47
|
+
# @param ActiveRecordRelation models
|
48
|
+
# @param String column column name
|
49
|
+
# @param Array/String values search values
|
50
|
+
# @param Callable callable
|
51
|
+
# @option String operator orかand
|
52
|
+
def column_call(models, column, values, callable, operator:'or')
|
53
|
+
column_values = values.instance_of?(Array) ? values : [values]
|
54
|
+
models.where!(column_values.map{|value| callable.call(column, value)}.join(" #{operator} "))
|
55
|
+
models
|
56
|
+
end
|
57
|
+
|
58
|
+
|
24
59
|
# override or create method '_belongs_to_{table}' if necessary
|
25
60
|
# @param ActiveRecordRelation models
|
26
61
|
# @param String table table name
|
@@ -75,6 +110,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
|
|
75
110
|
models
|
76
111
|
end
|
77
112
|
|
113
|
+
|
78
114
|
# get relation tables
|
79
115
|
# @param String relate 'belongs_to','hasmany'..
|
80
116
|
# @return Hash associations relation name => talbe name array
|
@@ -1,29 +1,25 @@
|
|
1
1
|
module ActiveRecordRelationExtension
|
2
|
-
# ----- model relation object methods -----
|
3
|
-
# def relation_object_method
|
4
|
-
# end
|
5
|
-
# Model.all.relation_object_method
|
6
2
|
|
7
|
-
#
|
8
|
-
# @param Hash params
|
3
|
+
# column search
|
4
|
+
# @param Hash params
|
9
5
|
def filtering!(params)
|
10
6
|
models = self
|
11
7
|
associations = self.model.get_associations()
|
12
8
|
params.each do |key, value|
|
13
9
|
if key.present? and value.present?
|
14
10
|
if column_names.include?(key)
|
15
|
-
#
|
11
|
+
# array change
|
16
12
|
values = value.instance_of?(Array) ? value : [value]
|
17
13
|
values.reject!(&:blank?)
|
18
|
-
#
|
14
|
+
# call function
|
19
15
|
function_name = self.model.methods.include?("_where_#{key}".to_sym) ? "_where_#{key}" : '_where'
|
20
16
|
models = self.model.send(function_name, models, key, values)
|
21
17
|
end
|
22
18
|
|
23
|
-
# belongs_to, has_many
|
19
|
+
# belongs_to, has_many search
|
24
20
|
associations.keys.each do |association|
|
25
21
|
if associations[association].include?(key) and value.instance_of?(ActionController::Parameters)#hash型はActionController::Parameters
|
26
|
-
#
|
22
|
+
# call function
|
27
23
|
function_name = self.model.methods.include?("_#{association}_#{key}".to_sym) ? "_#{association}_#{key}" : "_#{association}"
|
28
24
|
models = self.model.send(function_name, models, key, value)
|
29
25
|
end
|
@@ -33,8 +29,8 @@ module ActiveRecordRelationExtension
|
|
33
29
|
return models
|
34
30
|
end
|
35
31
|
|
36
|
-
#
|
37
|
-
# @param Hash params
|
32
|
+
# pager
|
33
|
+
# @param Hash params
|
38
34
|
def paging!(params)
|
39
35
|
count = params[:count].present? ? params[:count].to_i : -1;
|
40
36
|
page = params[:page].present? ? params[:page].to_i : 1;
|
@@ -48,8 +44,8 @@ module ActiveRecordRelationExtension
|
|
48
44
|
end
|
49
45
|
end
|
50
46
|
|
51
|
-
#
|
52
|
-
# @param Hash params
|
47
|
+
# sort
|
48
|
+
# @param Hash params
|
53
49
|
def sorting!(params)
|
54
50
|
if params[:order].present? and params[:orderby].present?
|
55
51
|
order!({params[:orderby] => params[:order]})
|
data/lib/baseapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baseapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moriyuki Arakawa
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.9'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.9'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
description: ruby on rails gem baseapi
|
@@ -46,9 +46,9 @@ executables:
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
-
- .gitignore
|
50
|
-
- .rspec
|
51
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
52
|
- CODE_OF_CONDUCT.md
|
53
53
|
- Gemfile
|
54
54
|
- LICENSE.txt
|
@@ -77,12 +77,12 @@ require_paths:
|
|
77
77
|
- lib
|
78
78
|
required_ruby_version: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
84
|
requirements:
|
85
|
-
- -
|
85
|
+
- - ">="
|
86
86
|
- !ruby/object:Gem::Version
|
87
87
|
version: '0'
|
88
88
|
requirements: []
|