baseapi 0.1.17 → 0.1.18

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: 4e9f185316292e93a8d5049926c7320dd52fb510
4
- data.tar.gz: fdfe47036f032b62ee6f47186e58d4018a15ee1f
3
+ metadata.gz: 39bf71d43a891a48a723f7449148bd4f2a8524a0
4
+ data.tar.gz: 315a004a9355d7fd2cd691636e00091196fa57b7
5
5
  SHA512:
6
- metadata.gz: eff2568e7d77ef73b0cdb1f1bb7c777a94bed409328f5b81e5a69b02b1c2202aaf5198ec3a32a4466fcd518455e2c9c83a128d7008f9e074f4e9e4d7d426a4ec
7
- data.tar.gz: fb139caa35aee2e316b0b6ddb3bd74de12a74d4aeb1afbdf84d623592ff45d4b0b53610df1195b1dcdb0c1c6790b7b6dbc01e03f0e316060e0178e88b32d0f28
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
- module ClassMethods
299
- def self._all
300
- self.all # default
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
- module ClassMethods
316
- def self._where(models, column, values)
317
- column_match(models, column, values) # default
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
- module ClassMethods
325
- def self._where_name(models, column, values)
326
- column_match(models, column, values) # default
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
- module ClassMethods
334
- def self._belongs_to(models, table, hash)
335
- relation_match(models, table, hash) # default
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
- module ClassMethods
343
- def self._belongs_to_company(models, table, hash)
344
- relation_match(models, table, hash) # default
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
- def self._belongs_to_company_units_...(models, table, hash)
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
- module ClassMethods
356
- def self._has_many(models, table, hash)
357
- relation_match(models, table, hash) # default
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
- module ClassMethods
365
- def self._has_many_users(models, table, hash)
366
- relation_match(models, table, hash) # default
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
- def self._has_many_users_families_...(models, table, hash)
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
@@ -10,3 +10,6 @@ ActiveRecord::Base.send(:include, ActiveRecordBaseExtension)
10
10
 
11
11
  # ActionController::Base Extension
12
12
  require 'baseapi/app/controllers/base_api_controller'
13
+
14
+ # baseapi module
15
+ require 'baseapi/app/controllers/concerns/base_api'
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Baseapi
2
- VERSION = "0.1.17"
2
+ VERSION = "0.1.18"
3
3
  end
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.17
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-15 00:00:00.000000000 Z
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