sql_search_n_sort 2.1.2 → 2.1.3
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 +4 -4
- data/lib/sql_search_n_sort/model_sort_config.rb +36 -0
- data/lib/sql_search_n_sort/sort_column.rb +22 -0
- data/lib/sql_search_n_sort/sql_searchable_sortable.rb +4 -54
- data/lib/sql_search_n_sort/version.rb +1 -1
- data/lib/sql_search_n_sort.rb +2 -0
- data/test/dummy/app/models/article.rb +1 -0
- data/test/dummy/app/models/comment.rb +6 -0
- data/test/dummy/db/migrate/20160121020023_create_comments.rb +8 -0
- data/test/dummy/db/migrate/20160121020232_add_timestamps_to_comments.rb +5 -0
- data/test/dummy/db/migrate/20160121020730_add_article_id_to_comments.rb +5 -0
- data/test/dummy/log/development.log +37 -0
- data/test/dummy/log/test.log +57471 -0
- data/test/dummy/spec/factories/factory.rb +14 -8
- data/test/dummy/spec/models/comment_spec.rb +55 -0
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1920514b23a356d4fc6c486d7ac4044816fbcc2d
|
4
|
+
data.tar.gz: 703b1cd1000b7eb683c44f619a939b31d0409754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf6dd49ea251cffe528eeb15d0c75bdd497139d473709adced5aaeaa0c0fb0ed8116c0608b7ce5936ccb2f63a1488cdd84b485ad3210ac48b0da386b4f50c70e
|
7
|
+
data.tar.gz: 7256a7ccf9afdf951723315615b71d0345a7f2c7621ad82a68a488ae8adeb9d887a73aa9f40090515806eacd2f3393696c53465e9a6ed501122912b2eb06d085
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class ModelSortConfig < Array
|
2
|
+
|
3
|
+
def initialize(*cols)
|
4
|
+
cols.each do |col|
|
5
|
+
if col.is_a? Hash
|
6
|
+
opts_hash = col.fetch(col.keys.first)
|
7
|
+
self << SortColumn.new(col.keys.first, opts_hash)
|
8
|
+
else
|
9
|
+
self << SortColumn.new(col)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_order(sort_by, dir, def_sort_col, base_class)
|
17
|
+
if sort_column_obj = self.sortable_column(sort_by)
|
18
|
+
# {sort_by => dir}
|
19
|
+
ar_class = sort_column_obj.joined_table ? sort_column_obj.joined_table.to_s.classify.constantize : base_class
|
20
|
+
ar_class.arel_table[sort_by].send(dir)
|
21
|
+
else
|
22
|
+
{def_sort_col => dir} if def_sort_col
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def sortable_column(col)
|
27
|
+
#returns nil if no matching sortable columns
|
28
|
+
self.select { |model_sort_config| model_sort_config.send("column") == col.to_s }.first
|
29
|
+
end
|
30
|
+
|
31
|
+
def select_opts
|
32
|
+
return self.inject([]) do |m, sort_col|
|
33
|
+
m + sort_col.select_opts
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class SortColumn
|
2
|
+
attr_reader :column, :joined_table, :show_asc, :show_desc ,:display_text
|
3
|
+
def initialize(column, opts={})
|
4
|
+
@column = column.to_s
|
5
|
+
@joined_table = opts[:joined_table]
|
6
|
+
@display_text = opts[:display_text]
|
7
|
+
@show_asc = (opts[:show_asc].nil? ? true : opts[:show_asc])
|
8
|
+
@show_desc = (opts[:show_desc].nil? ? true : opts[:show_desc])
|
9
|
+
end
|
10
|
+
def name
|
11
|
+
column.to_s
|
12
|
+
end
|
13
|
+
def human_name
|
14
|
+
name.humanize
|
15
|
+
end
|
16
|
+
def select_opts
|
17
|
+
arr = []
|
18
|
+
arr << ["#{display_text || human_name}", "#{name}"] if show_asc
|
19
|
+
arr << ["#{display_text || human_name} [desc]", "#{name} desc"] if show_desc
|
20
|
+
return arr
|
21
|
+
end
|
22
|
+
end
|
@@ -1,5 +1,8 @@
|
|
1
1
|
module SqlSearchableSortable
|
2
2
|
|
3
|
+
#NOTES:
|
4
|
+
#Comment.joins(:article).order(Article.arel_table[:headline]).to_sql=> "SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline`"
|
5
|
+
|
3
6
|
def self.extended(base)
|
4
7
|
base.class_eval do
|
5
8
|
attr_accessor :ssns_sortable
|
@@ -17,7 +20,7 @@ module SqlSearchableSortable
|
|
17
20
|
scope :sql_sort, ->(scope_sort_col=nil, scope_sort_dir=nil) do
|
18
21
|
scope_sort_col ||= default_sort_col #use model's default sort col if no args present
|
19
22
|
scope_sort_dir ||= default_sort_dir || :asc #same for direction
|
20
|
-
order(sort_config.get_order(scope_sort_col, scope_sort_dir, default_sort_col))
|
23
|
+
order(sort_config.get_order(scope_sort_col, scope_sort_dir, default_sort_col, self))
|
21
24
|
end
|
22
25
|
end
|
23
26
|
end
|
@@ -56,58 +59,5 @@ module SqlSearchableSortable
|
|
56
59
|
sort_config.select_opts
|
57
60
|
end
|
58
61
|
|
59
|
-
class ModelSortConfig < Array
|
60
|
-
|
61
|
-
def initialize(*cols)
|
62
|
-
cols.each do |col|
|
63
|
-
if col.is_a? Hash
|
64
|
-
h = col.fetch(col.keys.first)
|
65
|
-
self << SortColumn.new(col.keys.first, h[:display_text], h.fetch(:show_asc, true), h.fetch(:show_desc, true))
|
66
|
-
else
|
67
|
-
self << SortColumn.new(col)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def get_order(sort_by, dir, def_sort_col)
|
73
|
-
if self.contains_column(sort_by)
|
74
|
-
{sort_by => dir}
|
75
|
-
else
|
76
|
-
{def_sort_col => dir} if def_sort_col
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def contains_column(col)
|
81
|
-
self.any? { |sc| sc.column == col }
|
82
|
-
end
|
83
|
-
|
84
|
-
def select_opts
|
85
|
-
return self.inject([]) do |m, sort_col|
|
86
|
-
m + sort_col.select_opts
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class SortColumn
|
92
|
-
attr_reader :column, :show_asc, :show_desc ,:display_text
|
93
|
-
def initialize(column, display_text=nil, show_asc=true, show_desc=true)
|
94
|
-
@column = column
|
95
|
-
@display_text = display_text
|
96
|
-
@show_asc = show_asc
|
97
|
-
@show_desc = show_desc
|
98
|
-
end
|
99
|
-
def name
|
100
|
-
column.to_s
|
101
|
-
end
|
102
|
-
def human_name
|
103
|
-
name.humanize
|
104
|
-
end
|
105
|
-
def select_opts
|
106
|
-
arr = []
|
107
|
-
arr << ["#{display_text || human_name}", "#{name}"] if show_asc
|
108
|
-
arr << ["#{display_text || human_name} [desc]", "#{name} desc"] if show_desc
|
109
|
-
return arr
|
110
|
-
end
|
111
|
-
end
|
112
62
|
|
113
63
|
end
|
data/lib/sql_search_n_sort.rb
CHANGED
@@ -10559,3 +10559,40 @@ Mysql2::Error: Table 'ssns_development.members' doesn't exist: SHOW FULL FIELDS
|
|
10559
10559
|
[1m[35mActiveRecord::SchemaMigration Load (0.2ms)[0m SELECT `schema_migrations`.* FROM `schema_migrations`
|
10560
10560
|
[1m[36mActiveRecord::SchemaMigration Load (0.3ms)[0m [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
|
10561
10561
|
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT `schema_migrations`.* FROM `schema_migrations`
|
10562
|
+
[1m[36mActiveRecord::SchemaMigration Load (35.8ms)[0m [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
|
10563
|
+
Migrating to CreateComments (20160121020023)
|
10564
|
+
[1m[35m (62.5ms)[0m CREATE TABLE `comments` (`id` int(11) auto_increment PRIMARY KEY, `ctext` text, `commentator` varchar(255)) ENGINE=InnoDB
|
10565
|
+
Mysql2::Error: Table 'comments' already exists: CREATE TABLE `comments` (`id` int(11) auto_increment PRIMARY KEY, `ctext` text, `commentator` varchar(255)) ENGINE=InnoDB
|
10566
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
|
10567
|
+
Migrating to CreateComments (20160121020023)
|
10568
|
+
[1m[35m (130.4ms)[0m CREATE TABLE `comments` (`id` int(11) auto_increment PRIMARY KEY, `ctext` text, `commentator` varchar(255)) ENGINE=InnoDB
|
10569
|
+
[1m[36m (0.2ms)[0m [1mBEGIN[0m
|
10570
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO `schema_migrations` (`version`) VALUES ('20160121020023')
|
10571
|
+
[1m[36m (0.5ms)[0m [1mCOMMIT[0m
|
10572
|
+
Migrating to AddTimestampsToComments (20160121020232)
|
10573
|
+
[1m[35m (217.9ms)[0m ALTER TABLE `comments` ADD `created_at` datetime
|
10574
|
+
[1m[36m (175.7ms)[0m [1mALTER TABLE `comments` ADD `updated_at` datetime[0m
|
10575
|
+
[1m[35m (0.2ms)[0m BEGIN
|
10576
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO `schema_migrations` (`version`) VALUES ('20160121020232')[0m
|
10577
|
+
[1m[35m (0.5ms)[0m COMMIT
|
10578
|
+
Migrating to AddArticleIdToComments (20160121020730)
|
10579
|
+
[1m[36m (210.6ms)[0m [1mALTER TABLE `comments` ADD `article_id` int(11)[0m
|
10580
|
+
[1m[35m (0.2ms)[0m BEGIN
|
10581
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO `schema_migrations` (`version`) VALUES ('20160121020730')[0m
|
10582
|
+
[1m[35m (28.0ms)[0m COMMIT
|
10583
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
|
10584
|
+
[1m[36mComment Load (1.3ms)[0m [1mSELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC[0m
|
10585
|
+
[1m[35mComment Load (1.8ms)[0m SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
|
10586
|
+
[1m[36mComment Load (0.6ms)[0m [1mSELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC[0m
|
10587
|
+
Mysql2::Error: Unknown column 'articles.headline' in 'order clause': SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
|
10588
|
+
[1m[35mComment Load (0.5ms)[0m SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
|
10589
|
+
Mysql2::Error: Unknown column 'articles.headline' in 'order clause': SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
|
10590
|
+
[1m[36mComment Load (1.7ms)[0m [1mSELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC[0m
|
10591
|
+
[1m[36mComment Load (0.4ms)[0m [1mSELECT `comments`.* FROM `comments`[0m
|
10592
|
+
[1m[36mComment Load (0.4ms)[0m [1mSELECT `comments`.* FROM `comments`[0m
|
10593
|
+
[1m[35mComment Load (0.4ms)[0m SELECT `comments`.* FROM `comments`
|
10594
|
+
[1m[36mComment Load (0.3ms)[0m [1mSELECT `comments`.* FROM `comments`[0m
|
10595
|
+
[1m[36mComment Load (0.4ms)[0m [1mSELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC[0m
|
10596
|
+
Mysql2::Error: Unknown column 'articles.headline' in 'order clause': SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
|
10597
|
+
[1m[35mComment Load (1.3ms)[0m SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
|
10598
|
+
[1m[36mComment Load (1.7ms)[0m [1mSELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC[0m
|