paginative 0.0.23 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2642afe5cdba6115566e177eb2414467e99a31b
4
- data.tar.gz: d9c517c793c053edd25df1c4834cf0c2bf727d7f
3
+ metadata.gz: 97984ef48fec7c34fd8ab3dd073f4bf463786805
4
+ data.tar.gz: 48d2ebf5b44cb52b06f3ac8effbb164bed2149cd
5
5
  SHA512:
6
- metadata.gz: ea2dc1b6140adb5b00fe17ac2c3a4dc45e5048e6e1f554b9c6f1e1c6bffb89f3dc81f374366c11bd2d39736be27ee9b48eba74aed37475578f64ca69ea4f0421
7
- data.tar.gz: a65fe086f1b61fa33c7cb9cf16ae73d7f611e7bd30d7d0d9ef65076c5cf0dafe1d3bdb96f0954a92d5517be2f6bfed94f077671447127aecdebee9d88bd933d7
6
+ metadata.gz: 0de5ec95993076ac5ef7cde82295fd2536d2fcbbfbcaf6f6f0cc59ae526e0328628c377f304e0036979ce587372bc604cd063e2ac46031595e42e95c9d6f1900
7
+ data.tar.gz: 7f73c15ed9b1a05bfe7a9703d664e26a0fb873901c43a05ca2996b7bed7cf723c8cb551a6fd52c692362678469a9d7f772ba5ca670fc223b5eb1e7e2cf645d56
@@ -5,36 +5,40 @@ module Paginative
5
5
  included do
6
6
  include Paginative::OrderingHelpers
7
7
 
8
- def self.by_distance_from(latitude, longitude, distance=0, limit=25)
9
- return [] unless latitude.present? && longitude.present?
10
- distance_sql = send(:distance_sql, latitude.to_f, longitude.to_f, {:units => :km, select_bearing: false})
8
+ def self.by_distance_from(latitude, longitude, distance=0, limit=25)
9
+ return [] unless latitude.present? && longitude.present?
10
+ distance_sql = send(:distance_sql, latitude.to_f, longitude.to_f, {:units => :km, select_bearing: false})
11
11
 
12
- self.where("#{distance_sql} > ?", distance).offset(0).limit(limit)
13
- end
12
+ self.where("#{distance_sql} > ?", distance).offset(0).limit(limit)
13
+ end
14
14
 
15
- def self.with_name_from(name="", limit=25, order="asc")
16
- return self.order("name DESC").where("lower(name) < ?", name.downcase).offset(0).limit(limit) if order == "desc"
17
- self.order(name: :asc).where("lower(#{self.table_name}.name) > ?", name.downcase).offset(0).limit(limit)
18
- end
15
+ def self.with_name_from(name="", limit=25, order="asc")
16
+ return self.order("name DESC").where("lower(name) < ?", name.downcase).offset(0).limit(limit) if order == "desc"
17
+ self.order(name: :asc).where("lower(#{self.table_name}.name) > ?", name.downcase).offset(0).limit(limit)
18
+ end
19
19
 
20
- def self.with_id_from(id=0, limit=25)
21
- self.order(id: :asc).where("id > ?", id).limit(limit)
22
- end
20
+ def self.with_id_from(id=0, limit=25)
21
+ self.order(id: :asc).where("id > ?", id).limit(limit)
22
+ end
23
23
 
24
- def self.with_field_from(field="", value="", limit=25, order="asc")
25
- if field.is_a? Array
26
- 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
24
+ def self.with_field_from(field="", value="", limit=25, order="asc")
25
+ if field.is_a? Array
26
+ return raise "Wrong number of values. Expected 2, got #{value.try(:length)}. You must pass a value for each field that you are sorting by" unless value.is_a?(Array) && value.length == 2
27
27
  # You can now pass in an array of 'field' params so that you can have a secondary sort order.
28
28
  # This is important if your primary sort field could have duplicate values
29
29
  primary_sort_field = field[0]
30
+ primary_value = value[0]
30
31
  secondary_sort_field = field[1]
31
- return self.order(sanitized_ordering(self.table_name, primary_sort_field, order), sanitized_ordering(self.table_name, secondary_sort_field, order)).where("#{secondary_sort_field} < ?", value).limit(limit) if order.downcase == "desc"
32
- self.order(sanitized_ordering(self.table_name, primary_sort_field, order), sanitized_ordering(self.table_name, secondary_sort_field, order)).where("#{secondary_sort_field} > ?", value).limit(limit)
32
+ secondary_value = value[1]
33
+ # Postgres sorts strings differently to Rails. We use the Postgres string concat and sort so that there is no confusion here.
34
+ # We need to treat the 2 columns as one string to accurately paginate from a certain point when 2 columns are passed into the argument
35
+ return self.order(sanitized_ordering(self.table_name, field, order)).where("#{primary_sort_field} || #{secondary_sort_field} < ?", "#{primary_value}#{secondary_value}").limit(limit) if order.downcase == "desc"
36
+ self.order(sanitized_ordering(self.table_name, field, order)).where("#{primary_sort_field} || #{secondary_sort_field} > ?", "#{primary_value}#{secondary_value}").limit(limit)
33
37
  else
34
38
  return self.order(sanitized_ordering(self.table_name, field, order)).where("#{field} < ?", value).limit(limit) if order.downcase == "desc"
35
39
  self.order(sanitized_ordering(self.table_name, field, order)).where("#{field} > ?", value).limit(limit)
36
40
  end
37
41
  end
42
+ end
38
43
  end
39
44
  end
40
- end
@@ -2,22 +2,25 @@ module Paginative
2
2
  module OrderingHelpers
3
3
  extend ActiveSupport::Concern
4
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
5
+ included do
6
+ def self.sanitized_ordering(table_name, field, order)
7
+ if field.is_a? Array
8
+ 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
9
+ "#{table_name}.#{sanitize_column(field[0])} || #{table_name}.#{sanitize_column(field[1])} #{sanitize_column_direction(order)}"
10
+ else
11
+ "#{table_name}.#{sanitize_column(field)} #{sanitize_column_direction(order)}"
12
+ end
13
+ end
13
14
 
14
- def sanitize_column_direction(direction)
15
- direction = direction.upcase
16
- ['DESC', 'ASC'].include?(direction) ? direction : "DESC"
17
- end
15
+ private
16
+ def self.sanitize_column(column)
17
+ self.column_names.include?(column) ? column : "created_at"
18
+ end
18
19
 
19
- def resource
20
- controller_name.camelize.singularize.safe_constantize
20
+ def self.sanitize_column_direction(direction)
21
+ direction = direction.upcase
22
+ ['DESC', 'ASC'].include?(direction) ? direction : "DESC"
23
+ end
21
24
  end
22
25
  end
23
26
  end
@@ -1,3 +1,3 @@
1
1
  module Paginative
2
- VERSION = "0.0.23"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,5 +2,4 @@ class TestModel < ActiveRecord::Base
2
2
  include Paginative::ModelExtension
3
3
 
4
4
  reverse_geocoded_by :latitude, :longitude
5
- after_validation :reverse_geocode # auto-fetch coordinates
6
5
  end
@@ -1,4 +1,4 @@
1
- Rails.application.configure do
1
+ Dummy::Application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb.
3
3
 
4
4
  # In the development environment your application's code is reloaded on
@@ -20,7 +20,7 @@ Rails.application.configure do
20
20
  config.active_support.deprecation = :log
21
21
 
22
22
  # Raise an error on page load if there are pending migrations.
23
- config.active_record.migration_error = :page_load
23
+ # config.active_record.migration_error = :page_load
24
24
 
25
25
  # Debug mode disables concatenation and preprocessing of assets.
26
26
  # This option may cause significant delays in view rendering with a large
@@ -1,4 +1,4 @@
1
- Rails.application.configure do
1
+ Dummy::Application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb.
3
3
 
4
4
  # The test environment is used exclusively to run your application's
Binary file
@@ -9,26 +9,17 @@
9
9
  # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
10
  # you'll amass, the slower it'll run and the greater likelihood for issues).
11
11
  #
12
- # It's strongly recommended that you check this file into your version control system.
12
+ # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20140416035443) do
14
+ ActiveRecord::Schema.define(:version => 20140416035443) do
15
15
 
16
- create_table "paginative_test_models", force: true do |t|
17
- t.string "name"
18
- t.float "latitude"
19
- t.float "longitude"
20
- t.datetime "created_at"
21
- t.datetime "updated_at"
22
- t.string "address"
23
- end
24
-
25
- create_table "test_models", force: true do |t|
16
+ create_table "test_models", :force => true do |t|
26
17
  t.string "name"
27
18
  t.string "address"
28
19
  t.float "latitude"
29
20
  t.float "longitude"
30
- t.datetime "created_at"
31
- t.datetime "updated_at"
21
+ t.datetime "created_at", :null => false
22
+ t.datetime "updated_at", :null => false
32
23
  end
33
24
 
34
25
  end
Binary file
@@ -81,3 +81,56 @@ Migrating to CreateTestModels (20140416035443)
81
81
   (0.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20140416035443')
82
82
  ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
83
83
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
84
+ Connecting to database specified by database.yml
85
+  (0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
86
+ Migrating to CreatePaginativeTestModels (20140415060518)
87
+ Migrating to AddAddressToTestModels (20140416020706)
88
+ Migrating to CreateTestModels (20140416035443)
89
+  (0.1ms) select sqlite_version(*)
90
+  (0.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
91
+ Connecting to database specified by database.yml
92
+  (0.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
93
+ Migrating to CreatePaginativeTestModels (20140415060518)
94
+ Migrating to AddAddressToTestModels (20140416020706)
95
+ Migrating to CreateTestModels (20140416035443)
96
+  (0.1ms) select sqlite_version(*)
97
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
98
+ Connecting to database specified by database.yml
99
+  (1.6ms) select sqlite_version(*)
100
+  (3.6ms) CREATE TABLE "paginative_test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "latitude" float, "longitude" float, "created_at" datetime, "updated_at" datetime, "address" varchar(255))
101
+  (1.1ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" varchar(255), "latitude" float, "longitude" float, "created_at" datetime, "updated_at" datetime) 
102
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
103
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
104
+  (0.3ms) SELECT version FROM "schema_migrations"
105
+  (1.2ms) INSERT INTO "schema_migrations" (version) VALUES ('20140416035443')
106
+  (1.2ms) INSERT INTO "schema_migrations" (version) VALUES ('20140415060518')
107
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20140416020706')
108
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
109
+ Connecting to database specified by database.yml
110
+  (1.4ms) select sqlite_version(*)
111
+  (1.1ms) CREATE TABLE "paginative_test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "latitude" float, "longitude" float, "created_at" datetime, "updated_at" datetime, "address" varchar(255))
112
+  (1.6ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" varchar(255), "latitude" float, "longitude" float, "created_at" datetime, "updated_at" datetime) 
113
+  (1.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
114
+  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
115
+  (0.2ms) SELECT version FROM "schema_migrations"
116
+  (2.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20140416035443')
117
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
118
+ Connecting to database specified by database.yml
119
+  (0.1ms) select sqlite_version(*)
120
+  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
121
+  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
122
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
123
+ Migrating to CreateTestModels (20140416035443)
124
+  (0.0ms) begin transaction
125
+  (0.6ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" varchar(255), "latitude" float, "longitude" float, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
126
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20140416035443')
127
+  (0.8ms) commit transaction
128
+  (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
129
+  (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
130
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
131
+  (0.2ms) select sqlite_version(*)
132
+  (1.0ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "address" varchar(255), "latitude" float, "longitude" float, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
133
+  (0.8ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
134
+  (0.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
135
+  (0.1ms) SELECT version FROM "schema_migrations"
136
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20140416035443')