filterer 0.3.4 → 0.4.0

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: 4a138d652fabbc12e24e17721a99cd126ae35591
4
- data.tar.gz: 4d651ac67a3d09a90a4a198abdc2e6b860c8e8d7
3
+ metadata.gz: 8f2be938199447e226d9ae6551db7dfb601c4c4c
4
+ data.tar.gz: d3cecf768d8c860468b9cd61069453f0c8cd7c01
5
5
  SHA512:
6
- metadata.gz: e9accb1c92193354c204b89585a6ba6d75642b9613573acba6dce924bc76ccb4c968b8d3ce5ab4853bc02a0265c858078c282291ad74566e25d5e23fb453096b
7
- data.tar.gz: 0cba437c9e6e9a896987c2d927a145c14bd5c989f54db85042915f0c8d86f902a17aeff08e2945ba5c6fc21ca69d5b252518022e60d48bb66246df53c493faac
6
+ metadata.gz: 0e89f19b32af85c618fa6ac27531d2d7c3d5289af89ba03e4ff6a6fd73f2ba21dc5758388dfa03cb9aab841a8d616f13875f76d67b5e7a03ff06db2e60aaaf00
7
+ data.tar.gz: 29d5c3bc304a8f43d054a2a558d64f0d0bdec9f466bb3fedfd4aa1c5c8f99adca98a3fc2605d0b63f2d6da3c3469c187894744467e18e6e58ffd7fb3a7b70971
data/lib/filterer/base.rb CHANGED
@@ -1,23 +1,22 @@
1
1
  module Filterer
2
2
  class Base
3
-
4
3
  IGNORED_PARAMS = [:page]
5
4
 
6
5
  attr_accessor :results, :meta, :direction, :sort, :params, :opts
7
6
 
8
- class << self
9
- attr_accessor :sort_options, :per_page_num, :per_page_allow_override
7
+ class_attribute :sort_options
8
+ self.sort_options = []
10
9
 
11
- def sort_options
12
- @sort_options ||= []
13
- end
10
+ class_attribute :per_page
11
+ self.per_page = 20
14
12
 
15
- def inherited(subclass)
16
- %w(sort_options per_page_num per_page_allow_override).each do |x|
17
- subclass.send("#{x}=", instance_variable_get("@#{x}"))
18
- end
19
- end
13
+ class_attribute :per_page_allow_override
14
+ self.per_page_allow_override = false
20
15
 
16
+ class_attribute :per_page_max
17
+ self.per_page_max = 1000
18
+
19
+ class << self
21
20
  def sort_option(key, query_string_or_proc = nil, opts = {})
22
21
  if query_string_or_proc.is_a?(Hash)
23
22
  opts, query_string_or_proc = query_string_or_proc.clone, nil
@@ -39,16 +38,19 @@ module Filterer
39
38
  raise "Tiebreaker can't be a proc."
40
39
  end
41
40
 
42
- sort_options << {
41
+ self.sort_options += [{
43
42
  key: key,
44
43
  query_string_or_proc: query_string_or_proc,
45
44
  opts: opts
46
- }
45
+ }]
47
46
  end
48
47
 
49
- def per_page(num, opts = {})
50
- @per_page_num = num
51
- @per_page_allow_override = opts[:allow_override]
48
+ def count(params = {}, opts = {})
49
+ self.new(params, { chainable: true }.merge(opts)).results.count
50
+ end
51
+
52
+ def chain(params = {}, opts = {})
53
+ self.new(params, { chainable: true }.merge(opts)).results
52
54
  end
53
55
  end
54
56
 
@@ -71,35 +73,32 @@ module Filterer
71
73
 
72
74
  def get_per_page
73
75
  if self.class.per_page_allow_override && @params[:per_page].present?
74
- @params[:per_page]
76
+ [@params[:per_page], self.per_page_max].min
75
77
  else
76
- self.class.per_page_num || 20
78
+ self.class.per_page
77
79
  end
78
80
  end
79
81
 
80
82
  def find_results
81
83
  @results = opts.delete(:starting_query) || starting_query
82
-
83
- # Add params
84
84
  add_params_to_query
85
-
86
85
  return if @opts[:chainable]
87
-
88
- # Order results
89
86
  order_results
90
-
91
- @meta[:total] = @results.unscope(:select).count
92
- @meta[:last_page] = [(@meta[:total].to_f / @meta[:per_page]).ceil, 1].max
93
- @meta[:page] = [@meta[:last_page], @meta[:page]].min
94
-
95
- return if @opts[:count_only]
87
+ add_meta
96
88
 
97
89
  # Add custom meta data if we've defined the method
98
90
  @meta.merge!(self.custom_meta_data) if self.respond_to?(:custom_meta_data)
99
91
 
92
+ # Return the paginated results
100
93
  @results = @results.limit(@meta[:per_page]).offset((@meta[:page] - 1)*@meta[:per_page])
101
94
  end
102
95
 
96
+ def add_meta
97
+ @meta[:total] = @results.unscope(:select).count
98
+ @meta[:last_page] = [(@meta[:total].to_f / @meta[:per_page]).ceil, 1].max
99
+ @meta[:page] = [@meta[:last_page], @meta[:page]].min
100
+ end
101
+
103
102
  def add_params_to_query
104
103
  @params.reject { |k, v| k.in?(IGNORED_PARAMS) }
105
104
  .select { |k, v| v.present? }
@@ -113,57 +112,59 @@ module Filterer
113
112
  def order_results
114
113
  @direction = @params[:direction] == 'desc' ? 'DESC' : 'ASC'
115
114
  @sort = (params[:sort] && get_sort_option(params[:sort])) ? params[:sort] : default_sort_param
116
- return unless get_sort_option(@sort)
117
-
118
- if get_sort_option(@sort)[:query_string_or_proc].is_a?(String)
119
- @results = @results.order %Q{
120
- #{get_sort_option(@sort)[:query_string_or_proc]}
121
- #{@direction}
122
- #{get_sort_option(@sort)[:opts][:nulls_last] ? 'NULLS LAST' : ''}
123
- #{tiebreaker_sort_string ? ',' + tiebreaker_sort_string : ''}
124
- }.squish
115
+
116
+ if !get_sort_option(@sort)
117
+ @results = @results.order default_sort_sql
118
+ elsif get_sort_option(@sort)[:query_string_or_proc].is_a?(String)
119
+ @results = @results.order basic_sort_sql
125
120
  elsif get_sort_option(@sort)[:query_string_or_proc].is_a?(Proc)
126
- matches = get_sort_option(@sort)[:key].is_a?(Regexp) ? @sort.match(get_sort_option(@sort)[:key]) : nil
127
- @results = get_sort_option(@sort)[:query_string_or_proc].call(@results, matches, self)
121
+ apply_sort_proc
128
122
  end
129
123
  end
130
124
 
125
+ def default_sort_sql
126
+ "#{@results.model.table_name}.id ASC"
127
+ end
128
+
129
+ def basic_sort_sql
130
+ %{
131
+ #{get_sort_option(@sort)[:query_string_or_proc]}
132
+ #{@direction}
133
+ #{get_sort_option(@sort)[:opts][:nulls_last] ? 'NULLS LAST' : ''}
134
+ #{tiebreaker_sort_string ? ',' + tiebreaker_sort_string : ''}
135
+ }.squish
136
+ end
137
+
138
+ def apply_sort_proc
139
+ sort_key = get_sort_option(@sort)[:key]
140
+ matches = sort_key.is_a?(Regexp) && @sort.match(sort_key)
141
+ @results = get_sort_option(@sort)[:query_string_or_proc].call(@results, matches, self)
142
+ end
143
+
131
144
  def get_sort_option(x)
132
- self.class.sort_options.find { |sort_option|
145
+ self.class.sort_options.detect do |sort_option|
133
146
  if sort_option[:key].is_a?(Regexp)
134
147
  x.match(sort_option[:key])
135
148
  else # String
136
149
  x == sort_option[:key]
137
150
  end
138
- }
151
+ end
139
152
  end
140
153
 
141
154
  def default_sort_param
142
- self.class.sort_options.find { |sort_option|
155
+ self.class.sort_options.detect do |sort_option|
143
156
  sort_option[:opts][:default]
144
- }.try(:[], :key)
157
+ end.try(:[], :key)
145
158
  end
146
159
 
147
160
  def tiebreaker_sort_string
148
- self.class.sort_options.find { |sort_option|
161
+ self.class.sort_options.detect do |sort_option|
149
162
  sort_option[:opts][:tiebreaker]
150
- }.try(:[], :query_string_or_proc)
163
+ end.try(:[], :query_string_or_proc)
151
164
  end
152
165
 
153
-
154
166
  def starting_query
155
167
  raise 'You must override this method!'
156
168
  end
157
-
158
- def self.count(params = {}, opts = {})
159
- filterer = self.new(params, { count_only: true }.merge(opts))
160
- return filterer.meta[:total]
161
- end
162
-
163
- def self.chain(params = {}, opts = {})
164
- filterer = self.new(params, { chainable: true }.merge(opts))
165
- return filterer.results
166
- end
167
-
168
169
  end
169
170
  end
@@ -1,3 +1,3 @@
1
1
  module Filterer
2
- VERSION = "0.3.4"
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-25 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails