baseapi 0.1.18 → 0.1.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/baseapi/active_record/relation_extension.rb +27 -1
- data/lib/baseapi/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d717b23db853f03f0e050478301cae6de593aa31
|
4
|
+
data.tar.gz: 3a1b5c98aeb1064cb8ff21393bf4c55a975c3b48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7823dfc05b717d03b70fc6f494f506dcf42890604e41c3bdc5b36c4cafb358cdca44592dd7ab420a91b5464b620325f8e27be8e28ee3f0a0622e104358943711
|
7
|
+
data.tar.gz: e415c9b3e1bf3923c815a716d900bd5a695ead5e428c9cac5a4ed807ac26c22c058c90c6856b769898aca8838d3e2c58c789b9d6963827da8097548f10dad8a8
|
data/README.md
CHANGED
@@ -102,6 +102,16 @@ Specify the sorting order
|
|
102
102
|
GET /users.json?order=desc&orderby=name
|
103
103
|
SQL => SELECT DISTINCT `users`.* FROM `users` ORDER BY `users`.`name` DESC
|
104
104
|
|
105
|
+
Specify the multiple sorting order (v0.1.19~)
|
106
|
+
|
107
|
+
GET /users.json?order[]=desc&order[]=asc&orderby[]=name&orderby[]=company_id
|
108
|
+
SQL => SELECT DISTINCT `users`.* FROM `users` ORDER BY name DESC, company_id ASC
|
109
|
+
|
110
|
+
Specify the association sorting order (v0.1.19~)
|
111
|
+
|
112
|
+
GET /users.json?order=asc&orderby=company.name
|
113
|
+
SQL => SELECT DISTINCT `users`.* FROM `users` ...JOIN... ORDER BY companies.name ASC
|
114
|
+
|
105
115
|
Specify the name
|
106
116
|
|
107
117
|
GET /users.json?name=hoge
|
@@ -67,7 +67,33 @@ module ActiveRecordRelationExtension
|
|
67
67
|
def sorting!(params)
|
68
68
|
prefix = self.model.get_reserved_word_prefix
|
69
69
|
if params["#{prefix}order".to_sym].present? and params["#{prefix}orderby".to_sym].present?
|
70
|
-
|
70
|
+
# array exchange
|
71
|
+
orderby = params["#{prefix}orderby".to_sym]
|
72
|
+
orderbys = orderby.instance_of?(Array) ? orderby : [orderby]
|
73
|
+
order = params["#{prefix}order".to_sym]
|
74
|
+
orders = order.instance_of?(Array) ? order : [order]
|
75
|
+
# multiple order
|
76
|
+
orderbys.each_with_index do |orderby, index|
|
77
|
+
if orders[index].present? and ['DESC', 'ASC'].include?(orders[index].upcase)
|
78
|
+
order = orders[index].upcase
|
79
|
+
# dot notation example: company.name
|
80
|
+
joins_tables = orderby.split(".")
|
81
|
+
column_name = joins_tables.pop
|
82
|
+
table_name = joins_tables.count > 0 ? joins_tables.last.pluralize.underscore : ''
|
83
|
+
# joins_tables exists check
|
84
|
+
joins_tables.each do |table|
|
85
|
+
next unless ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore)
|
86
|
+
end
|
87
|
+
# table_name exists check
|
88
|
+
next unless ActiveRecord::Base.connection.tables.include?(table_name)
|
89
|
+
# column_name exists check
|
90
|
+
next unless table_name.camelize.singularize.constantize.column_names.include?(column_name)
|
91
|
+
# joins
|
92
|
+
joins_array!(joins_tables)
|
93
|
+
# order
|
94
|
+
order!("#{table_name}#{table_name.present? ? '.' : ''}#{column_name} #{order}")
|
95
|
+
end
|
96
|
+
end
|
71
97
|
end
|
72
98
|
end
|
73
99
|
|
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.19
|
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-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|