baseapi 0.1.24 → 0.1.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a559935b8e1eb2c1ac7418e084c0bb9d4a6185b
4
- data.tar.gz: 8a4b2598c2a23c09e1a79ebcbad0a2b2c6923d0a
3
+ metadata.gz: 25e5520506f037fce90f73374e06640c124f2b74
4
+ data.tar.gz: f95d92c2f4a326c7f1f798a3b88403dbce01af0c
5
5
  SHA512:
6
- metadata.gz: bca05467579b8dd593c7161ac69558fe8e222275c107aa6c65ddcfb485ce712449732bf8c41a719cebb38ec85e34f02a66b4f5eab0557fffe09507d6063eb9e5
7
- data.tar.gz: 752a6a65f0cabc07bb31d3309b97ecae395818d8b065865ca6251680169e39853af3eb480f6f4a3538da9df295db7fadf9628cdb89cecec077aeb0738f4e4c30
6
+ metadata.gz: 82f792e4c93ddb83b4ae0e67d2da016edff044f1b8cbfbd14cf40acd8dd805f2e11fa98dedaf6b255b3e40c8a26dcdbf3bb4affd2f71d198caf0426f18e55ff0
7
+ data.tar.gz: 52a922a6cc3fc4c773ef2c0af598edbee4501a1ae089c6e7c4b8d19349acb968ed35cc82660ac1be24b050e9c3c72ab9531ad8730133327f6c5163264dcd336c
@@ -80,20 +80,46 @@ module ActiveRecordRelationExtension
80
80
  joins_tables = orderby.split(".")
81
81
  column_name = joins_tables.pop
82
82
  table_name = joins_tables.count > 0 ? joins_tables.last.pluralize.underscore : self.model.to_s.pluralize.underscore
83
- # joins_tables exists check
84
- is_next = false
85
- joins_tables.each do |table|
86
- is_next = true and break if !ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore)
83
+ # table_name parent table
84
+ parent_table_name = joins_tables.count > 1 ? joins_tables.last(2).first : self.model.to_s.pluralize.underscore
85
+ # parent_table get association
86
+ association = parent_table_name.camelize.singularize.constantize.reflect_on_association(table_name.singularize)
87
+ # If you have specified class_name in belongs_to method (for example, you have changed the foreign key)
88
+ # example:
89
+ # class Project < ActiveRecord::Base
90
+ # belongs_to :manager, foreign_key: 'manager_id', class_name: 'User'
91
+ # belongs_to :leader, foreign_key: 'leader_id', class_name: 'User'
92
+ # end
93
+ if association and association.options[:class_name].present?
94
+ association_table_name = association.options[:class_name].pluralize.underscore
95
+ table_alias = table_name.pluralize.underscore
96
+ # check
97
+ next if !ActiveRecord::Base.connection.tables.include?(association_table_name)
98
+ # join
99
+ joins!("INNER JOIN `#{association_table_name}` AS `#{table_alias}` ON `#{table_alias}`.`id` = `#{parent_table_name}`.`#{table_alias.singularize}_id`")
100
+ # order
101
+ order!("`#{table_alias}`.`#{column_name}` #{order}")
102
+ # belongs_to along the rails convention
103
+ # example:
104
+ # class Project < ActiveRecord::Base
105
+ # belongs_to :manager
106
+ # end
107
+ else
108
+ # joins_tables exists check
109
+ is_next = false
110
+ joins_tables.each do |table|
111
+ is_next = true and break if !ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore)
112
+ end
113
+ next if is_next
114
+ # table exists check
115
+ next if !ActiveRecord::Base.connection.tables.include?(table_name)
116
+ # column_name exists check
117
+ next if !table_name.camelize.singularize.constantize.column_names.include?(column_name)
118
+ # joins
119
+ joins_array!(joins_tables)
120
+ # order
121
+ order!("`#{table_name}`.`#{column_name}` #{order}")
87
122
  end
88
- next if is_next
89
- # table exists check
90
- next if !ActiveRecord::Base.connection.tables.include?(table_name)
91
- # column_name exists check
92
- next if !table_name.camelize.singularize.constantize.column_names.include?(column_name)
93
- # joins
94
- joins_array!(joins_tables)
95
- # order
96
- order!("`#{table_name}`.`#{column_name}` #{order}")
97
123
  end
98
124
  end
99
125
  end
@@ -1,3 +1,3 @@
1
1
  module Baseapi
2
- VERSION = "0.1.24"
2
+ VERSION = "0.1.25"
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.24
4
+ version: 0.1.25
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-10-07 00:00:00.000000000 Z
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler