paginative 0.0.23 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paginative/models/model_extension.rb +22 -18
- data/lib/paginative/models/ordering_helpers.rb +17 -14
- data/lib/paginative/version.rb +1 -1
- data/spec/dummy/app/models/test_model.rb +0 -1
- data/spec/dummy/config/environments/development.rb +2 -2
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +5 -14
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +53 -0
- data/spec/dummy/log/test.log +16308 -0
- data/spec/models/paginative/test_model_spec.rb +15 -2
- data/spec/spec_helper.rb +1 -15
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97984ef48fec7c34fd8ab3dd073f4bf463786805
|
4
|
+
data.tar.gz: 48d2ebf5b44cb52b06f3ac8effbb164bed2149cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
12
|
+
self.where("#{distance_sql} > ?", distance).offset(0).limit(limit)
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
32
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
private
|
16
|
+
def self.sanitize_column(column)
|
17
|
+
self.column_names.include?(column) ? column : "created_at"
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
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
|
data/lib/paginative/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
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
|
Binary file
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -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
|
12
|
+
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version
|
14
|
+
ActiveRecord::Schema.define(:version => 20140416035443) do
|
15
15
|
|
16
|
-
create_table "
|
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
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -81,3 +81,56 @@ Migrating to CreateTestModels (20140416035443)
|
|
81
81
|
[1m[36m (0.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140416035443')[0m
|
82
82
|
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
83
83
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
84
|
+
Connecting to database specified by database.yml
|
85
|
+
[1m[36m (0.3ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
86
|
+
Migrating to CreatePaginativeTestModels (20140415060518)
|
87
|
+
Migrating to AddAddressToTestModels (20140416020706)
|
88
|
+
Migrating to CreateTestModels (20140416035443)
|
89
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
90
|
+
[1m[36m (0.4ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
91
|
+
Connecting to database specified by database.yml
|
92
|
+
[1m[36m (0.4ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
93
|
+
Migrating to CreatePaginativeTestModels (20140415060518)
|
94
|
+
Migrating to AddAddressToTestModels (20140416020706)
|
95
|
+
Migrating to CreateTestModels (20140416035443)
|
96
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
97
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
98
|
+
Connecting to database specified by database.yml
|
99
|
+
[1m[36m (1.6ms)[0m [1mselect sqlite_version(*)[0m
|
100
|
+
[1m[35m (3.6ms)[0m 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
|
+
[1m[36m (1.1ms)[0m [1mCREATE 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) [0m
|
102
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
103
|
+
[1m[36m (1.0ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
104
|
+
[1m[35m (0.3ms)[0m SELECT version FROM "schema_migrations"
|
105
|
+
[1m[36m (1.2ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140416035443')[0m
|
106
|
+
[1m[35m (1.2ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140415060518')
|
107
|
+
[1m[36m (0.8ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140416020706')[0m
|
108
|
+
[1m[35m (0.1ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
|
109
|
+
Connecting to database specified by database.yml
|
110
|
+
[1m[36m (1.4ms)[0m [1mselect sqlite_version(*)[0m
|
111
|
+
[1m[35m (1.1ms)[0m 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
|
+
[1m[36m (1.6ms)[0m [1mCREATE 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) [0m
|
113
|
+
[1m[35m (1.3ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
114
|
+
[1m[36m (0.9ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
115
|
+
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
116
|
+
[1m[36m (2.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140416035443')[0m
|
117
|
+
[1m[35m (0.1ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
|
118
|
+
Connecting to database specified by database.yml
|
119
|
+
[1m[36m (0.1ms)[0m [1mselect sqlite_version(*)[0m
|
120
|
+
[1m[35m (1.4ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
121
|
+
[1m[36m (0.9ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
122
|
+
[1m[35m (0.1ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
|
123
|
+
Migrating to CreateTestModels (20140416035443)
|
124
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
125
|
+
[1m[35m (0.6ms)[0m 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
|
+
[1m[36m (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES ('20140416035443')[0m
|
127
|
+
[1m[35m (0.8ms)[0m commit transaction
|
128
|
+
[1m[36m (0.2ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
129
|
+
[1m[35m (0.2ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
|
130
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
131
|
+
[1m[35m (0.2ms)[0m select sqlite_version(*)
|
132
|
+
[1m[36m (1.0ms)[0m [1mCREATE 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) [0m
|
133
|
+
[1m[35m (0.8ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
134
|
+
[1m[36m (0.9ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
135
|
+
[1m[35m (0.1ms)[0m SELECT version FROM "schema_migrations"
|
136
|
+
[1m[36m (0.8ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140416035443')[0m
|