baseapi 0.3.1 → 0.3.2
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/lib/baseapi/active_record/relation_extension.rb +57 -48
- 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: 50c8afe497f0ce3be4000d49f56f5a784c84d5cd
|
4
|
+
data.tar.gz: 74a4da68bc84672fce18cf23ba2c6064f19167e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51f33afc48e9e76694f2f969201c8358046e5fb468a40d832b5a9695343c9095502e9e16e52781413d5bbf3ce6c3d90ac43d6b551bddfa371a32f137d7e7ad07
|
7
|
+
data.tar.gz: ca4fd1d21dd61b42cb716f2656b80f81a94a8b34681af89877bd9f924d686dca781f24fe36b63efb5d19c5a673c7bc2595aadfd510ed97550153804e40cf410e
|
@@ -50,6 +50,11 @@ module ActiveRecordRelationExtension
|
|
50
50
|
|
51
51
|
# pager
|
52
52
|
# @param Hash params
|
53
|
+
# count & page params example:
|
54
|
+
# count
|
55
|
+
# GET /?count=10
|
56
|
+
# page
|
57
|
+
# GET /?count=10&page=2
|
53
58
|
def paging!(params)
|
54
59
|
prefix = self.model.get_reserved_word_prefix
|
55
60
|
count = params["#{prefix}count".to_sym].present? ? params["#{prefix}count".to_sym].to_i : -1;
|
@@ -66,62 +71,66 @@ module ActiveRecordRelationExtension
|
|
66
71
|
|
67
72
|
# sort
|
68
73
|
# @param Hash params
|
74
|
+
# order & orderby params example:
|
75
|
+
# Model
|
76
|
+
# class Project < ActiveRecord::Base
|
77
|
+
# belongs_to :status
|
78
|
+
# belongs_to :manager, foreign_key: 'manager_id', class_name: 'User'
|
79
|
+
# belongs_to :leader, foreign_key: 'leader_id', class_name: 'User'
|
80
|
+
# has_many :tasks
|
81
|
+
# end
|
82
|
+
# single order
|
83
|
+
# GET /?orderby=id&order=desc
|
84
|
+
# multiple order
|
85
|
+
# GET /?orderby[]=name&order[]=asc&orderby[]=id&order[]=desc
|
86
|
+
# belongs_to association order
|
87
|
+
# GET /?orderby=status.id&order=desc
|
88
|
+
# belongs_to association order (changed the foreign key)
|
89
|
+
# GET /?orderby=manager.id&order=desc
|
90
|
+
# has_many association order
|
91
|
+
# GET /?orderby=tasks.id&order=asc
|
69
92
|
def sorting!(params)
|
70
93
|
prefix = self.model.get_reserved_word_prefix
|
71
94
|
if params["#{prefix}order".to_sym].present? and params["#{prefix}orderby".to_sym].present?
|
72
|
-
# array exchange
|
73
95
|
orderby = params["#{prefix}orderby".to_sym]
|
74
96
|
orderbys = orderby.instance_of?(Array) ? orderby : [orderby]
|
75
97
|
order = params["#{prefix}order".to_sym]
|
76
98
|
orders = order.instance_of?(Array) ? order : [order]
|
77
|
-
# multiple order
|
78
99
|
orderbys.each_with_index do |orderby, index|
|
79
|
-
if orders[index].
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
#
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#
|
106
|
-
|
107
|
-
#
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
is_next = true and break if !ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore)
|
114
|
-
end
|
115
|
-
next if is_next
|
116
|
-
# table exists check
|
117
|
-
next if !ActiveRecord::Base.connection.tables.include?(table_name)
|
118
|
-
# column_name exists check
|
119
|
-
next if !table_name.camelize.singularize.constantize.column_names.include?(column_name)
|
120
|
-
# joins
|
121
|
-
joins_array!(joins_tables)
|
122
|
-
# order
|
123
|
-
order!("`#{table_name}`.`#{column_name}` #{order}")
|
124
|
-
end
|
100
|
+
next if orders[index].blank?
|
101
|
+
next unless ['DESC', 'ASC'].include?(orders[index].upcase)
|
102
|
+
order = orders[index].upcase
|
103
|
+
joins_tables = orderby.split(".")
|
104
|
+
column_name = joins_tables.pop
|
105
|
+
table_name = joins_tables.count > 0 ? joins_tables.last.pluralize.underscore : self.model.to_s.pluralize.underscore
|
106
|
+
parent_table_name = joins_tables.count > 1 ? joins_tables.last(2).first : self.model.to_s.pluralize.underscore
|
107
|
+
association = parent_table_name.camelize.singularize.constantize.reflect_on_association(table_name.singularize)
|
108
|
+
# belongs_to association order (changed the foreign key)
|
109
|
+
if association and association.options[:class_name].present?
|
110
|
+
association_table_name = association.options[:class_name].pluralize.underscore
|
111
|
+
table_alias = table_name.pluralize.underscore
|
112
|
+
next unless ActiveRecord::Base.connection.tables.include?(association_table_name)
|
113
|
+
parent_model = parent_table_name.camelize.singularize.constantize
|
114
|
+
association_model = association_table_name.camelize.singularize.constantize
|
115
|
+
# joins
|
116
|
+
arel_alias = association_model.arel_table.alias(table_alias)
|
117
|
+
joins_arel = parent_model.arel_table.join(arel_alias).on(arel_alias[:id].eq parent_model.arel_table["#{table_alias.singularize}_id".to_sym])
|
118
|
+
joins_arel.join_sources
|
119
|
+
joins!(joins_arel.join_sources)
|
120
|
+
# order
|
121
|
+
order!(arel_alias[column_name.to_sym].send(order.downcase))
|
122
|
+
# other order
|
123
|
+
else
|
124
|
+
# joins_tables exists check
|
125
|
+
next unless joins_tables.select{|table| ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore) }.count == joins_tables.count
|
126
|
+
# table exists check
|
127
|
+
next unless ActiveRecord::Base.connection.tables.include?(table_name)
|
128
|
+
# column_name exists check
|
129
|
+
next unless table_name.camelize.singularize.constantize.column_names.include?(column_name)
|
130
|
+
# joins
|
131
|
+
joins_array!(joins_tables)
|
132
|
+
# order
|
133
|
+
order!(table_name.camelize.singularize.constantize.arel_table[column_name.to_sym].send(order.downcase))
|
125
134
|
end
|
126
135
|
end
|
127
136
|
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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moriyuki Arakawa
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|