baseapi 0.1.12 → 0.1.13
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 +21 -0
- data/lib/baseapi/active_record/base_extension.rb +8 -0
- data/lib/baseapi/active_record/relation_extension.rb +11 -9
- data/lib/baseapi/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6f8c5e16034693c92e956e421277975f060297c
|
4
|
+
data.tar.gz: 6ecded08bc1d03c2f3b434e4d7d688b6a43c0aad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eaee6b87986d514a069f07c5f15a1b42b0d3df9e4fc3865069cea4268319566df49dd107893af8be047dbc117659e92231c5bede60311d6cd4d9b67d86edfe18
|
7
|
+
data.tar.gz: d1702cfa15dc126229ac95b00b9b7bf07c0bc03c81b48da1d206cf16006c2bab78707d4eda25e04903262686f5430e5684d45755233c72c207202f0ffebc6df2
|
data/README.md
CHANGED
@@ -223,6 +223,27 @@ Delete the id to 1
|
|
223
223
|
]
|
224
224
|
}
|
225
225
|
|
226
|
+
### reserved word (v0.1.13~)
|
227
|
+
|
228
|
+
We are using the following as a reserved word.
|
229
|
+
|
230
|
+
- count
|
231
|
+
- page
|
232
|
+
- order
|
233
|
+
- orderby
|
234
|
+
|
235
|
+
It can not be used as a column name in the database.
|
236
|
+
|
237
|
+
But, you can be avoided by specifying the prefix.
|
238
|
+
|
239
|
+
class User < ActiveRecord::Base
|
240
|
+
@reserved_word_prefix = '_'
|
241
|
+
end
|
242
|
+
|
243
|
+
You can request as follows in doing so
|
244
|
+
|
245
|
+
GET /users.json?_count=10&_page=2
|
246
|
+
|
226
247
|
### Override
|
227
248
|
|
228
249
|
You can corresponding to the logical deletion, if you want to search condition to like and or, you will be able to override the processing in the Model
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module ActiveRecordBaseExtension extend ActiveSupport::Concern
|
2
2
|
|
3
|
+
@reserved_word_prefix = ''
|
4
|
+
|
3
5
|
# override if necessary
|
4
6
|
def _destroy
|
5
7
|
destroy
|
@@ -7,6 +9,12 @@ module ActiveRecordBaseExtension extend ActiveSupport::Concern
|
|
7
9
|
|
8
10
|
module ClassMethods
|
9
11
|
|
12
|
+
# reserved word prefix(count,page,order,orderby...)
|
13
|
+
# @return String
|
14
|
+
def get_reserved_word_prefix
|
15
|
+
@reserved_word_prefix
|
16
|
+
end
|
17
|
+
|
10
18
|
# override if necessary
|
11
19
|
# @return ActiveRecordRelation
|
12
20
|
def _all
|
@@ -16,24 +16,24 @@ module ActiveRecordRelationExtension
|
|
16
16
|
models = self.model.send(function_name, models, key, values)
|
17
17
|
# belongs_to, has_many search
|
18
18
|
else
|
19
|
-
relationSearch = -> (models, currentModel, key, value,
|
19
|
+
relationSearch = -> (models, currentModel, key, value, prefix = '', joins = []) {
|
20
20
|
joins.push key
|
21
21
|
associations = currentModel.get_associations()
|
22
22
|
associations.keys.each do |association|
|
23
23
|
if currentModel.column_names.include?(key)
|
24
24
|
# call function
|
25
|
-
function_name = self.model.methods.include?("_#{
|
25
|
+
function_name = self.model.methods.include?("_#{prefix}_#{joins.join('_')}".to_sym) ? "_#{prefix}_#{joins.join('_')}" : "_#{prefix}"
|
26
26
|
table_name = currentModel.name.underscore
|
27
27
|
hash = {key => value}
|
28
28
|
return self.model.send(function_name, models, table_name, hash)
|
29
29
|
elsif associations[association].include?(key)
|
30
|
-
#
|
31
|
-
|
30
|
+
# prefix = first association
|
31
|
+
prefix = association if prefix == ''
|
32
32
|
models.joins_array!(joins)
|
33
33
|
currentModel = key.camelize.singularize.constantize
|
34
34
|
value.each do |k, v|
|
35
35
|
# this fnuction collback
|
36
|
-
models = relationSearch.call(models, currentModel, k, v,
|
36
|
+
models = relationSearch.call(models, currentModel, k, v, prefix, joins)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -49,8 +49,9 @@ module ActiveRecordRelationExtension
|
|
49
49
|
# pager
|
50
50
|
# @param Hash params
|
51
51
|
def paging!(params)
|
52
|
-
|
53
|
-
|
52
|
+
prefix = self.model.get_reserved_word_prefix
|
53
|
+
count = params["#{prefix}count".to_sym].present? ? params["#{prefix}count".to_sym].to_i : -1;
|
54
|
+
page = params["#{prefix}page".to_sym].present? ? params["#{prefix}page".to_sym].to_i : 1;
|
54
55
|
if count > 0
|
55
56
|
if count.present? and count
|
56
57
|
limit!(count)
|
@@ -64,8 +65,9 @@ module ActiveRecordRelationExtension
|
|
64
65
|
# sort
|
65
66
|
# @param Hash params
|
66
67
|
def sorting!(params)
|
67
|
-
|
68
|
-
|
68
|
+
prefix = self.model.get_reserved_word_prefix
|
69
|
+
if params["#{prefix}order".to_sym].present? and params["#{prefix}orderby".to_sym].present?
|
70
|
+
order!({params["#{prefix}orderby".to_sym] => params["#{prefix}order".to_sym]})
|
69
71
|
end
|
70
72
|
end
|
71
73
|
|
data/lib/baseapi/version.rb
CHANGED