paginative 0.0.18 → 0.0.19

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: ff823b1b6ebd583bbae59ddf13ba3adc566df9de
4
- data.tar.gz: cdc241f9c59a3cb63c883e26e690730ff6ba57ff
3
+ metadata.gz: 82ddbfff48eba50ec39b4044458fb08d929835c2
4
+ data.tar.gz: 99fb78305cfbb619d8d6541024f2822b1707013b
5
5
  SHA512:
6
- metadata.gz: 99103ac2c9a8dbbf33504f96d29ae09a3a4c756253417d073ad41539229b3aca3f672020d3e7daed6ff9008ef7581f01aa5460f48144a2f0beae43a8e237e297
7
- data.tar.gz: 751ea40dc1fbd7e32837ad7beb6b60bf242ce315096e9ad589d541f874032a095632679445c76d59bba82d86bee0417e66f96c0158b3c15ba1e265ebdd65004b
6
+ metadata.gz: 4b0c90ada294bad16087b7a70a32cf162ab08ec71bdfbea4742e19cd86aeaa7c66ffc7d71d3d4b06d6e7d9fb8ae66003eef3a8e40f6665151ed6b048d0428e45
7
+ data.tar.gz: 94a997ef26cb8d3412bbdb657de9b0842f43513806192ecd5a96999302328d707b0e1d5b22e278231b5559877341d2deb052f201ee2e71e839474b6c518639a8
@@ -1,13 +1,14 @@
1
1
  module Paginative
2
2
  module ModelExtension
3
3
  extend ActiveSupport::Concern
4
+ include Paginative::OrderingHelpers
4
5
 
5
6
  included do
6
7
  def self.by_distance_from(latitude, longitude, distance=0, limit=25)
7
8
  return [] unless latitude.present? && longitude.present?
8
9
  distance_sql = send(:distance_sql, latitude.to_f, longitude.to_f, {:units => :km, select_bearing: false})
9
10
 
10
- self.where("#{distance_sql} > #{distance}").offset(0).limit(limit)
11
+ self.where("#{distance_sql} > ?", distance).offset(0).limit(limit)
11
12
  end
12
13
 
13
14
  def self.with_name_from(name="", limit=25, order="asc")
@@ -20,12 +21,19 @@ module Paginative
20
21
  end
21
22
 
22
23
  def self.with_field_from(field="", value="", limit=25, order="asc")
23
- field = "#{self.table_name}.#{field}"
24
-
25
- return self.order("#{field} DESC").where("#{field} < ?", value).limit(limit) if order == "desc"
26
- self.order("#{field} ASC").where("#{field} > ?", value).limit(limit)
24
+ if field.is_a? Array
25
+ return raise "Wrong number of sorting fields. Expected 2, got #{field.length}. If you want to sort by a singular field please pass field argument as a string rather than an array." unless field.length == 2
26
+ # You can now pass in an array of 'field' params so that you can have a secondary sort order.
27
+ # This is important if your primary sort field could have duplicate values
28
+ primary_sort_field = field[0]
29
+ secondary_sort_field = field[1]
30
+ return self.order(sanitized_ordering(self.table_name, primary_sort_field, order), sanitized_ordering(self.table_name, secondary_sort_field, order)).where("#{field} < ?", value).limit(limit) if order.downcase == "desc"
31
+ self.order(sanitized_ordering(self.table_name, primary_sort_field, order), sanitized_ordering(self.table_name, secondary_sort_field, order)).where("#{field} > ?", value).limit(limit)
32
+ else
33
+ return self.order(sanitized_ordering(self.table_name, field, order)).where("#{field} < ?", value).limit(limit) if order.downcase == "desc"
34
+ self.order(sanitized_ordering(self.table_name, field, order)).where("#{field} > ?", value).limit(limit)
35
+ end
27
36
  end
28
- # RUBY
29
37
  end
30
38
  end
31
39
  end
@@ -0,0 +1,23 @@
1
+ module Paginative
2
+ module OrderingHelpers
3
+ extend AcitveModel::Concern
4
+
5
+ def sanitized_ordering(table_name, field, order)
6
+ "#{table_name}.#{sanitize_column(field)} #{sanitize_column_direction(order)}"
7
+ end
8
+
9
+ private
10
+ def sanitize_column(column)
11
+ resource.column_names.include?(column) ? column : "created_at"
12
+ end
13
+
14
+ def sanitize_column_direction(direction)
15
+ direction = direction.upcase
16
+ ['DESC', 'ASC'].include?(direction) ? direction : "DESC"
17
+ end
18
+
19
+ def resource
20
+ controller_name.camelize.singularize.safe_constantize
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Paginative
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.19"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paginative
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Norman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -145,6 +145,7 @@ files:
145
145
  - lib/paginative.rb
146
146
  - lib/paginative/engine.rb
147
147
  - lib/paginative/models/model_extension.rb
148
+ - lib/paginative/models/ordering_helpers.rb
148
149
  - lib/paginative/railtie.rb
149
150
  - lib/paginative/version.rb
150
151
  - lib/tasks/paginative_tasks.rake
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  requirements: []
212
213
  rubyforge_project:
213
- rubygems_version: 2.2.2
214
+ rubygems_version: 2.4.6
214
215
  signing_key:
215
216
  specification_version: 4
216
217
  summary: A new way to paginate your Rails API