forest_liana 1.0.2 → 1.0.3

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: 42211f7fdd6d6bf42f56b919a8f6d935bd9626c1
4
- data.tar.gz: 792780e77690ec9532bde36a69ce26db608f5ac2
3
+ metadata.gz: ab20200b70356fc864e92fa629cee986a8f5bc35
4
+ data.tar.gz: e0cc7b3e3a71162b03dd8fc807c26c90fb59987d
5
5
  SHA512:
6
- metadata.gz: 191cf390e2fd3d5c536e0dc7c93b57a5b8ce333268703373c61650a2b6c70b08f6496dc4665317287218ce6fe9628bff75da32067611833ad1a9223706d495a9
7
- data.tar.gz: 78e90aa5449cebb4c8ce274cebfb8a8e8176d0ecbe193d9007bc8c0e21d2778ac230c8cbb14724c5a440049e02e0e308f26d1f4121f16a17dd97a4c196d57dfa
6
+ metadata.gz: b855d0f833f4172e2c711316d9423ce46738965bc97fa47d577b3aee91c16ec1d4aecaa01707ca3796d1e22b1be00f5bed42a0e3ada08b97d11b9bdcb145277d
7
+ data.tar.gz: 7b339c923eea4c3febcd22c1cc08497e487d142648eb9304485c71d7882df68e59fd280248c74ea8664c7f4554fd3952d07142f2cda70831472a1c8d7041ea55
@@ -6,16 +6,16 @@ module ForestLiana
6
6
  end
7
7
 
8
8
  def perform
9
- @records = search_query
9
+ @records_without_sort = @records = search_query
10
10
  @records = sort_query
11
11
  end
12
12
 
13
13
  def records
14
- @records.offset(offset).limit(limit)
14
+ @records.offset(offset).limit(limit).to_a
15
15
  end
16
16
 
17
17
  def count
18
- @records.count
18
+ @records_without_sort.count
19
19
  end
20
20
 
21
21
  private
@@ -25,25 +25,57 @@ module ForestLiana
25
25
  end
26
26
 
27
27
  def sort_query
28
- query = nil
29
-
30
28
  if @params[:sort]
31
29
  @params[:sort].split(',').each do |field|
32
30
  order = detect_sort_order(@params[:sort])
33
31
  field.slice!(0) if order == :desc
34
32
  field = detect_reference(field)
35
33
 
36
- query = "#{field} #{order.upcase}"
34
+ association = @resource.reflections[field.to_sym]
35
+ if association.try(:macro) == :has_many
36
+ @records = has_many_sort(association, order)
37
+ elsif association.try(:macro) == :has_and_belongs_to_many
38
+ @records = has_and_belongs_to_many(association, order)
39
+ else
40
+ @records = @records.order("#{field} #{order.upcase}")
41
+ end
37
42
  end
38
43
  elsif @resource.column_names.include?('created_at')
39
- query = 'created_at DESC'
44
+ @records = @records.order('created_at DESC')
40
45
  elsif @resource.column_names.include?('id')
41
- query = 'id DESC'
46
+ @records = @records.order('id DESC')
42
47
  else
43
- return @records
48
+ @records
44
49
  end
45
50
 
46
- @records.order(query)
51
+ @records
52
+ end
53
+
54
+ def has_many_sort(association, order)
55
+ @records
56
+ .select("#{@resource.table_name}.*,
57
+ COUNT(#{association.klass.table_name}.id) has_many_count")
58
+ .joins("LEFT JOIN #{association.klass.table_name}
59
+ ON #{@resource.table_name}.id =
60
+ #{association.klass.table_name}.#{association.foreign_key}")
61
+ .group("#{@resource.table_name}.id")
62
+ .order("has_many_count #{order.upcase}")
63
+ end
64
+
65
+ def has_and_belongs_to_many(association, order)
66
+ @records
67
+ .select("#{@resource.table_name}.*,
68
+ COUNT(#{association.klass.table_name}.id) has_many_count")
69
+ .joins("LEFT JOIN #{association.options[:join_table]}
70
+ ON #{@resource.table_name}.id =
71
+ #{association.options[:join_table]}.
72
+ #{association.foreign_key}")
73
+ .joins("LEFT JOIN #{association.klass.table_name}
74
+ ON #{association.klass.table_name}.id =
75
+ #{association.options[:join_table]}.
76
+ #{association.klass.name.foreign_key}")
77
+ .group("#{@resource.table_name}.id")
78
+ .order("has_many_count #{order.upcase}")
47
79
  end
48
80
 
49
81
  def detect_sort_order(field)
@@ -58,7 +90,7 @@ module ForestLiana
58
90
  .find {|a| a.name == ref.to_sym }
59
91
 
60
92
  if association
61
- "#{association.class_name.to_s.underscore.pluralize}.#{field}"
93
+ "#{association.table_name}.#{field}"
62
94
  else
63
95
  param
64
96
  end
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-12 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails