baseapi 0.1.17 → 0.1.18
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 +31 -30
- data/lib/baseapi.rb +3 -0
- data/lib/baseapi/app/controllers/concerns/base_api.rb +96 -0
- data/lib/baseapi/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39bf71d43a891a48a723f7449148bd4f2a8524a0
|
4
|
+
data.tar.gz: 315a004a9355d7fd2cd691636e00091196fa57b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fda202545cdae23ae9297438c6def1dadb59fd2514f7ae76db2f89b9ee4a0ca0dd54b3f56585d959cd775a69d5c77c490cc824b45b640232969ce37a7fea505
|
7
|
+
data.tar.gz: ff2ad846616eeef7f296752f33f4a229b75898db0ac5081dfdcb1b71a8272efd17645cdd130d3f397efe8768aef92125f31c704f5ebfec4c4720a5ed9ed73cff
|
data/README.md
CHANGED
@@ -295,10 +295,9 @@ You can corresponding to the logical deletion, if you want to search condition t
|
|
295
295
|
Get all
|
296
296
|
|
297
297
|
class User < ActiveRecord::Base
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
end
|
298
|
+
def self._all
|
299
|
+
self.all # default
|
300
|
+
end
|
302
301
|
end
|
303
302
|
|
304
303
|
delete
|
@@ -312,64 +311,66 @@ delete
|
|
312
311
|
column search
|
313
312
|
|
314
313
|
class User < ActiveRecord::Base
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
end
|
314
|
+
def self._where(models, column, values)
|
315
|
+
column_match(models, column, values) # default
|
316
|
+
end
|
319
317
|
end
|
320
318
|
|
321
319
|
name column search
|
322
320
|
|
323
321
|
class User < ActiveRecord::Base
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
end
|
322
|
+
def self._where_name(models, column, values)
|
323
|
+
column_match(models, column, values) # default
|
324
|
+
end
|
328
325
|
end
|
329
326
|
|
330
327
|
belongs_to search
|
331
328
|
|
332
329
|
class User < ActiveRecord::Base
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
end
|
330
|
+
def self._belongs_to(models, table, hash)
|
331
|
+
relation_match(models, table, hash) # default
|
332
|
+
end
|
337
333
|
end
|
338
334
|
|
339
335
|
company belongs_to search
|
340
336
|
|
341
337
|
class User < ActiveRecord::Base
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
end
|
338
|
+
def self._belongs_to_company(models, table, hash)
|
339
|
+
relation_match(models, table, hash) # default
|
340
|
+
end
|
346
341
|
end
|
347
342
|
|
348
343
|
If there are multiple related belongs_to (v0.1.12~)
|
349
344
|
|
350
|
-
|
345
|
+
class User < ActiveRecord::Base
|
346
|
+
def self._belongs_to_company_units_...(models, table, hash)
|
347
|
+
relation_match(models, table, hash) # default
|
348
|
+
end
|
349
|
+
end
|
351
350
|
|
352
351
|
has_many search
|
353
352
|
|
354
353
|
class Company < ActiveRecord::Base
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
end
|
354
|
+
def self._has_many(models, table, hash)
|
355
|
+
relation_match(models, table, hash) # default
|
356
|
+
end
|
359
357
|
end
|
360
358
|
|
361
359
|
users has_many search
|
362
360
|
|
363
361
|
class Company < ActiveRecord::Base
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
end
|
362
|
+
def self._has_many_users(models, table, hash)
|
363
|
+
relation_match(models, table, hash) # default
|
364
|
+
end
|
368
365
|
end
|
369
366
|
|
370
367
|
If there are multiple related has_many (v0.1.12~)
|
371
368
|
|
372
|
-
|
369
|
+
class Company < ActiveRecord::Base
|
370
|
+
def self._has_many_users_families_...(models, table, hash)
|
371
|
+
relation_match(models, table, hash) # default
|
372
|
+
end
|
373
|
+
end
|
373
374
|
|
374
375
|
### like & match, or & and Search (v0.1.3~)
|
375
376
|
|
data/lib/baseapi.rb
CHANGED
@@ -0,0 +1,96 @@
|
|
1
|
+
module BaseApi extend ActiveSupport::Concern
|
2
|
+
|
3
|
+
included do
|
4
|
+
before_action :set_Model
|
5
|
+
before_action :set_models, only: [:index]
|
6
|
+
before_action :set_model, only: [:show, :create, :update, :destroy]
|
7
|
+
end
|
8
|
+
|
9
|
+
# GET /{models}.json
|
10
|
+
# @param Hash
|
11
|
+
# @return String Json(Models)
|
12
|
+
def index
|
13
|
+
render 'models.json.jbuilder'
|
14
|
+
end
|
15
|
+
|
16
|
+
# GET /{models}/{id}.json
|
17
|
+
# @return String Json(Model)
|
18
|
+
def show
|
19
|
+
render 'model.json.jbuilder'
|
20
|
+
end
|
21
|
+
|
22
|
+
# POST /{models}/{id}.json
|
23
|
+
# @param
|
24
|
+
# @return String Json(Model)
|
25
|
+
def create
|
26
|
+
render 'model.json.jbuilder' if transaction(-> {
|
27
|
+
self.send("before_#{__method__}") if self.methods.include?("before_#{__method__}".to_sym)
|
28
|
+
params.each do |key, value|
|
29
|
+
if key.present? and @Model.column_names.include?(key)
|
30
|
+
@model.send("#{key}=", value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@model.save
|
34
|
+
self.send("after_#{__method__}") if self.methods.include?("after_#{__method__}".to_sym)
|
35
|
+
})
|
36
|
+
end
|
37
|
+
|
38
|
+
# PATCH/PUT /{models}/{id}.json
|
39
|
+
# @param
|
40
|
+
# @return String Json(Model)
|
41
|
+
def update
|
42
|
+
render 'model.json.jbuilder' if transaction(-> {
|
43
|
+
self.send("before_#{__method__}") if self.methods.include?("before_#{__method__}".to_sym)
|
44
|
+
params.each do |key, value|
|
45
|
+
if key.present? and @Model.column_names.include?(key)
|
46
|
+
@model.send("#{key}=", value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@model.save
|
50
|
+
self.send("after_#{__method__}") if self.methods.include?("after_#{__method__}".to_sym)
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
54
|
+
# DELETE /{models}/{id}.json
|
55
|
+
# @param
|
56
|
+
# @return String Json(Model)
|
57
|
+
def destroy
|
58
|
+
render 'model.json.jbuilder' if transaction(-> {
|
59
|
+
self.send("before_#{__method__}") if self.methods.include?("before_#{__method__}".to_sym)
|
60
|
+
@model._destroy
|
61
|
+
self.send("after_#{__method__}") if self.methods.include?("after_#{__method__}".to_sym)
|
62
|
+
})
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
# set Model class
|
67
|
+
def set_Model
|
68
|
+
@Model = params[:controller].camelize.singularize.constantize
|
69
|
+
end
|
70
|
+
|
71
|
+
# set model
|
72
|
+
def set_model
|
73
|
+
@model = params[:id].present? ? @Model.find(params[:id]) : @Model.new
|
74
|
+
end
|
75
|
+
|
76
|
+
# set models
|
77
|
+
def set_models
|
78
|
+
@models = @Model.search(params)
|
79
|
+
end
|
80
|
+
|
81
|
+
# transaction
|
82
|
+
# @param callable callable
|
83
|
+
# @return bool
|
84
|
+
def transaction (callable)
|
85
|
+
begin
|
86
|
+
@Model.transaction do
|
87
|
+
callable.call()
|
88
|
+
end
|
89
|
+
rescue => e
|
90
|
+
@message = e.message
|
91
|
+
render 'error.json.jbuilder'
|
92
|
+
return false
|
93
|
+
end
|
94
|
+
return true
|
95
|
+
end
|
96
|
+
end
|
data/lib/baseapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.18
|
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-09-
|
11
|
+
date: 2015-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/baseapi/active_record/base_extension.rb
|
63
63
|
- lib/baseapi/active_record/relation_extension.rb
|
64
64
|
- lib/baseapi/app/controllers/base_api_controller.rb
|
65
|
+
- lib/baseapi/app/controllers/concerns/base_api.rb
|
65
66
|
- lib/baseapi/app/views/base_api/error.json.jbuilder
|
66
67
|
- lib/baseapi/app/views/base_api/model.json.jbuilder
|
67
68
|
- lib/baseapi/app/views/base_api/models.json.jbuilder
|