gauze 0.0.4 → 0.0.5

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: 527dae15f5adffa62ef1b3325a193b704a8894c2
4
- data.tar.gz: 96b137d7878a66b32b8a52abddfd9ffb4bc178ab
3
+ metadata.gz: 714ba17d4effef3630da04ca15666c44c21b5012
4
+ data.tar.gz: a7c07c9cddb3a4664cff85eeec4fa52adcb4d302
5
5
  SHA512:
6
- metadata.gz: 78c419437f8fa15ca7f28fbc26c690b26fac850841d26f8ee19a49f1f5c31cc1d4ad400e53b044294f871210dd7704061075a30821ea0f52c2200ee0c375e51b
7
- data.tar.gz: 4257f394a2244ef6a9ef140aa9f7a275b018572fedbb055b91ed512b3e11554849d6ab6809edee3b050908aadf17bb2903813f06af8ed8cc9b8d1f926a3840e6
6
+ metadata.gz: d82d231d298f61324cdc3e50c36d9c4d44271af3620d6747bd960d56446977ee4654a4c856fbc7371c96e63df45e2629548aea2ba6effbfa762ea313ee7353d2
7
+ data.tar.gz: 1900ee37463aff73f5bfc5803f792cf7d6a910cbad977d38965c6d60c0174e646e63d6c99803446b4d7efa6acff20fe318066d94313152b0217b77e737db173b
@@ -22,6 +22,10 @@ module Gauze
22
22
  @sorters.push param_key: param_key, column: column
23
23
  end
24
24
 
25
+ def self.sort_direction(param_key, default, preprocessor = nil)
26
+ @sort_direction_param = {param_key: param_key, default: default, preprocessor: preprocessor}
27
+ end
28
+
25
29
  def self.build(resource, params = {})
26
30
  new(resource, params).build
27
31
  end
@@ -42,6 +46,8 @@ module Gauze
42
46
 
43
47
  if get_klass_var(:@sorters).present?
44
48
  _query = build_order_query(_query)
49
+ elsif get_klass_var(:@sort_direction_param).present?
50
+ _query = _query.order(get_klass_var(:@sort_direction_param)[:default])
45
51
  end
46
52
 
47
53
  return _query
@@ -53,21 +59,27 @@ module Gauze
53
59
 
54
60
  if filter_hash[:column].is_a?(Hash)
55
61
  arel_column_from_hash(filter_hash[:column]).method(filter_hash[:method]).call(filter_val)
62
+ elsif filter_hash[:column].is_a?(Array)
63
+ arel_column_from_array(filter_hash[:column], filter_hash[:method], filter_val)
56
64
  else
57
65
  @resource.arel_table[filter_hash[:column]].method(filter_hash[:method]).call(filter_val)
58
66
  end
59
67
  end
60
68
 
61
69
  def build_order_query(query)
62
- return query unless @params[:sort].present?
70
+ return default_sort(query) unless @params[:sort].present?
71
+
63
72
  sort_column = get_klass_var(:@sorters).find {|h| h[:param_key].to_s == @params[:sort].underscore}
64
- return query unless sort_column.present?
73
+
74
+ return default_sort(query) unless sort_column.present?
65
75
 
66
76
  if sort_column[:column].is_a?(Hash)
67
- query.order(arel_column_from_hash(sort_column[:column]))
77
+ _arel_column = arel_column_from_hash(sort_column[:column])
68
78
  else
69
- query.order(sort_column[:column])
79
+ _arel_column = @resource.arel_table[sort_column[:column]]
70
80
  end
81
+
82
+ query.order(_arel_column.method(sort_direction).call).group(_arel_column)
71
83
  end
72
84
 
73
85
  private
@@ -75,6 +87,44 @@ module Gauze
75
87
  self.class.instance_variable_get(var)
76
88
  end
77
89
 
90
+ def default_sort(query)
91
+ if get_klass_var(:@sort_direction_param) && get_klass_var(:@sort_direction_param)[:default]
92
+ query.order(get_klass_var(:@sort_direction_param)[:default])
93
+ else
94
+ query.order(:created_at)
95
+ end
96
+ end
97
+
98
+ def sort_direction
99
+ sort_options = get_klass_var(:@sort_direction_param)
100
+
101
+ sort_direction = @params[sort_options[:param_key]]
102
+ sort_direction = sort_options[:preprocessor].call(sort_direction) if sort_options[:preprocessor]
103
+
104
+ return unless sort_direction.present?
105
+ sort_direction.to_sym
106
+ end
107
+
108
+ def arel_column_from_array(columns, method, value)
109
+ nodes = nil
110
+
111
+ columns.each do |column|
112
+ if column.is_a?(Hash)
113
+ arel_node = arel_column_from_hash(column)
114
+ else
115
+ arel_node = @resource.arel_table[column]
116
+ end
117
+
118
+ if nodes.present?
119
+ nodes = nodes.or(arel_node.method(method).call(value))
120
+ else
121
+ nodes = arel_node.method(method).call(value)
122
+ end
123
+ end
124
+
125
+ return nodes
126
+ end
127
+
78
128
  def arel_column_from_hash(hash_param)
79
129
  raise ArgumentError, "Hash can only have one key." if hash_param.length > 1
80
130
  _resource = hash_param.keys.first.to_s.classify.constantize
@@ -1,3 +1,3 @@
1
1
  module Gauze
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gauze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Ostrowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails