baseapi 0.1.6 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c9e314b6c730e998e6bbd6561afb2c71e358750
4
- data.tar.gz: 3787d1a14be6662f1b8ff79cb036a2363fd4be84
3
+ metadata.gz: 2c43cc198f8095f49cc275feb31170f0af5f72c1
4
+ data.tar.gz: 4a0ae6530a6150ae894be6424a956381fae09b86
5
5
  SHA512:
6
- metadata.gz: 8fe4b5d984c9d02c24c7671c4737ec3d2ac21620303286ee4df10712142fd2873960d6954b6f74bff5798449467bd1205707c87114b9935ea9a495c10820c411
7
- data.tar.gz: a8782940c31430ebe0ad71fa77822aad5d50e68571fd0a50a9297f4e18a2a8588e67e83a441a4c4faea78870c77ca5a3bafef6603c506ffd9f61832bb66727e0
6
+ metadata.gz: fdae87d0508923b64d14e9f77562f38304c9b76ddf630715406d37609bb8f831f6473e83064f27b876f7924abdfd4ba16ba434804dbcee91738c6fc2984d8f7b
7
+ data.tar.gz: 7ff5f9e7aecd8be6fdff8679b3637744bbc0abcfe76924689782e33bcf5c5b2b446b8d6b5dc41b07b630a29952cd2eecba54f91abbbec39341b96e82680eb685
data/README.md CHANGED
@@ -109,6 +109,14 @@ Specify multiple possible
109
109
 
110
110
  GET /users.json?name[]=hoge&name[]=huga
111
111
 
112
+ Specify the not id (v0.1.7~)
113
+
114
+ GET /users.json?id=!1
115
+
116
+ Of course the other is also possible (v0.1.7~)
117
+
118
+ GET /users.json?name=!hoge
119
+
112
120
  Specify the belongs to company name
113
121
 
114
122
  Note that this is a single
@@ -240,6 +248,8 @@ users has_many search
240
248
  end
241
249
 
242
250
 
251
+ ### like & match, or & and Search (v0.1.3~)
252
+
243
253
  There is a useful function for the table Search
244
254
  By default, it looks like the following
245
255
  'Like' search and, you can change the 'and' and 'or'
@@ -270,16 +280,26 @@ The short so please read the [code](https://github.com/arakawamoriyuki/baseapi/b
270
280
 
271
281
 
272
282
 
273
- ### hook action
283
+ ### hook action (v0.1.4~)
274
284
 
275
285
  Controller of 'create, update, destroy' function in advance by attaching the prefix of before, you can post processing
286
+ Delete the related table or may be useful for error handling
287
+ It may be good even before_action, but you may use if you want to process in the transaction.
288
+ It is always surrounded by model of transaction.
276
289
 
277
- class UsersController < BaseApiController
290
+ class CompaniesController < BaseApiController
291
+ # Name Required items
278
292
  def before_create
279
293
  if params['name'].blank?
280
294
  raise 'Please enter your name'
281
295
  end
282
296
  end
297
+ # delete the relation table
298
+ def before_destroy
299
+ User.where('company_id = ?', @model.id).each do |user|
300
+ user.destroy()
301
+ end
302
+ end
283
303
  end
284
304
 
285
305
  And if not sent the name to api in the above example, it returns an error in the json. Message is a string that was passed to raise.
@@ -29,7 +29,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
29
29
  # @option String operator 'or' or 'and'
30
30
  def column_match(models, column, values, operator:'or')
31
31
  column_call(models, column, values, ->(column, value){
32
- "#{models.name.pluralize.underscore}.#{column} = '#{value}'"
32
+ "#{getPrefix(value)} #{models.name.pluralize.underscore}.#{column} #{getOperator(value)} #{getValue(value, "'")}"
33
33
  }, operator:operator)
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
40
40
  # @option String operator 'or' or 'and'
41
41
  def column_like(models, column, values, operator:'or')
42
42
  column_call(models, column, values, ->(column, value){
43
- "#{models.name.pluralize.underscore}.#{column} like '%#{value}%'"
43
+ "#{getPrefix(value)} #{models.name.pluralize.underscore}.#{column} like #{getValue(value, "%", "'")}"
44
44
  }, operator:operator)
45
45
  end
46
46
 
@@ -79,7 +79,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
79
79
  # @option String operator 'or' or 'and'
80
80
  def relation_match(models, table, hash, operator:'or')
81
81
  relation_call(models, table, hash, ->(table, column, value){
82
- "#{table}.#{column} = '#{value}'"
82
+ "#{getPrefix(value)} #{table}.#{column} #{getOperator(value)} #{getValue(value, "'")}"
83
83
  }, operator:operator)
84
84
  end
85
85
 
@@ -90,7 +90,7 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
90
90
  # @option String operator 'or' or 'and'
91
91
  def relation_like(models, table, hash, operator:'or')
92
92
  relation_call(models, table, hash, ->(table, column, value){
93
- "#{table}.#{column} like '%#{value}%'"
93
+ "#{getPrefix(value)} #{table}.#{column} like #{getValue(value, "%", "'")}"
94
94
  }, operator:operator)
95
95
  end
96
96
 
@@ -110,6 +110,36 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
110
110
  models
111
111
  end
112
112
 
113
+ # get sql prefix 'NOT'
114
+ # @param String value
115
+ # @return String value
116
+ def getPrefix(value)
117
+ (value[0] == '!') ? 'NOT' : ''
118
+ end
119
+
120
+ # return = or IS
121
+ # @param String value
122
+ # @return String operator
123
+ def getOperator(value)
124
+ (value.gsub('!', '').upcase == 'NULL') ? 'IS' : '='
125
+ end
126
+
127
+ # slice '!' value
128
+ # @param String value
129
+ # @param String wraps ' or %
130
+ # @return String value
131
+ def getValue(value, *wraps)
132
+ value.slice!(0) if value[0] == '!'
133
+ if value.upcase == 'NULL'
134
+ value = 'NULL'
135
+ else
136
+ wraps.each do |wrap|
137
+ value = "#{wrap}#{value}#{wrap}"
138
+ end
139
+ end
140
+ return value
141
+ end
142
+
113
143
 
114
144
  # get relation tables
115
145
  # @param String relate 'belongs_to','hasmany'..
@@ -1,3 +1,3 @@
1
1
  module Baseapi
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
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.6
4
+ version: 0.1.7
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-08-17 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler