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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d00d49da9c151fd2e10b73220edce2b812df815
4
- data.tar.gz: e8287d9df7f50bdfc048c6a7870c5d6aa0c48a84
3
+ metadata.gz: 50c8afe497f0ce3be4000d49f56f5a784c84d5cd
4
+ data.tar.gz: 74a4da68bc84672fce18cf23ba2c6064f19167e6
5
5
  SHA512:
6
- metadata.gz: 4f85f2c12c2631b7fcb86d6c35283085c2fbfeccdabb2fcb0176414d9b62a4fa9dc14be5a1a6d8bdc7bc890695013150286a9e6ab7af98fafd525cb86eceb956
7
- data.tar.gz: ca9dc9d4e5f34ca9b4767f5b0578b9df983eaef5170dbaa4291bf1cc3ee74fcca8252257b1860001af88ade26074e8bb7a74cd2a0643dfaf3a20e0e9cf7bcdb1
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].present? and ['DESC', 'ASC'].include?(orders[index].upcase)
80
- order = orders[index].upcase
81
- # dot notation example: company.name
82
- joins_tables = orderby.split(".")
83
- column_name = joins_tables.pop
84
- table_name = joins_tables.count > 0 ? joins_tables.last.pluralize.underscore : self.model.to_s.pluralize.underscore
85
- # table_name parent table
86
- parent_table_name = joins_tables.count > 1 ? joins_tables.last(2).first : self.model.to_s.pluralize.underscore
87
- # parent_table get association
88
- association = parent_table_name.camelize.singularize.constantize.reflect_on_association(table_name.singularize)
89
- # If you have specified class_name in belongs_to method (for example, you have changed the foreign key)
90
- # example:
91
- # class Project < ActiveRecord::Base
92
- # belongs_to :manager, foreign_key: 'manager_id', class_name: 'User'
93
- # belongs_to :leader, foreign_key: 'leader_id', class_name: 'User'
94
- # end
95
- if association and association.options[:class_name].present?
96
- association_table_name = association.options[:class_name].pluralize.underscore
97
- table_alias = table_name.pluralize.underscore
98
- # check
99
- next if !ActiveRecord::Base.connection.tables.include?(association_table_name)
100
- # join
101
- joins!("INNER JOIN `#{association_table_name}` AS `#{table_alias}` ON `#{table_alias}`.`id` = `#{parent_table_name}`.`#{table_alias.singularize}_id`")
102
- # order
103
- order!("`#{table_alias}`.`#{column_name}` #{order}")
104
- # belongs_to along the rails convention
105
- # example:
106
- # class Project < ActiveRecord::Base
107
- # belongs_to :manager
108
- # end
109
- else
110
- # joins_tables exists check
111
- is_next = false
112
- joins_tables.each do |table|
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
@@ -1,3 +1,3 @@
1
1
  module Baseapi
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
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.3.1
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-04 00:00:00.000000000 Z
11
+ date: 2016-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler