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 +4 -4
- data/app/services/forest_liana/resources_getter.rb +43 -11
- data/lib/forest_liana/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: ab20200b70356fc864e92fa629cee986a8f5bc35
|
4
|
+
data.tar.gz: e0cc7b3e3a71162b03dd8fc807c26c90fb59987d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
@
|
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
|
-
|
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
|
-
|
44
|
+
@records = @records.order('created_at DESC')
|
40
45
|
elsif @resource.column_names.include?('id')
|
41
|
-
|
46
|
+
@records = @records.order('id DESC')
|
42
47
|
else
|
43
|
-
|
48
|
+
@records
|
44
49
|
end
|
45
50
|
|
46
|
-
@records
|
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.
|
93
|
+
"#{association.table_name}.#{field}"
|
62
94
|
else
|
63
95
|
param
|
64
96
|
end
|
data/lib/forest_liana/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2015-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|