sql_search_n_sort 2.1.2 → 2.1.3

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: c9b55c9a2d00587e07696f93ff19868e14d6a6c9
4
- data.tar.gz: e5c0d59ba01b3545fc8a35d0bdce2a06fe262f0e
3
+ metadata.gz: 1920514b23a356d4fc6c486d7ac4044816fbcc2d
4
+ data.tar.gz: 703b1cd1000b7eb683c44f619a939b31d0409754
5
5
  SHA512:
6
- metadata.gz: 9bf247d3abf1332d504d9c13894de7af486a060339ea91fa008aac62007f2845f917a669ff2b4c8a4295c86991d3232de7c42b1df1b7b81925d4b2cba90f080c
7
- data.tar.gz: baa89ed908b98744a5ee90fae20d5fa36287bc99c875bdfd1a47520f0a4f6c63aa21956412352703f300e3a6b5fa6576ce9757627508a9c28cb0c7b868996003
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
@@ -1,3 +1,3 @@
1
1
  module SqlSearchNSort
2
- VERSION = "2.1.2"
2
+ VERSION = "2.1.3"
3
3
  end
@@ -1,4 +1,6 @@
1
1
  module SqlSearchNSort
2
+ require 'sql_search_n_sort/sort_column'
3
+ require 'sql_search_n_sort/model_sort_config'
2
4
  require 'sql_search_n_sort/sql_searchable_sortable'
3
5
  require "sql_search_n_sort/sql_sort_setup"
4
6
  require 'sql_search_n_sort/railtie' if defined?(Rails)
@@ -1,5 +1,6 @@
1
1
  class Article < ActiveRecord::Base
2
2
  extend SqlSearchableSortable
3
+ has_one :comments
3
4
 
4
5
  sql_sortable :by_line, :headline, :body, :date_pub => { display_text: "Date Published" }
5
6
 
@@ -0,0 +1,6 @@
1
+ class Comment < ActiveRecord::Base
2
+ extend SqlSearchableSortable
3
+ belongs_to :article
4
+
5
+ sql_sortable :commentator, {updated_at: { display_text: "Last Update" }}, {headline: { joined_table: :article }}, {created_at: { joined_table: :article }}
6
+ end
@@ -0,0 +1,8 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments do |t|
4
+ t.text :ctext
5
+ t.string :commentator
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class AddTimestampsToComments < ActiveRecord::Migration
2
+ def change
3
+ add_timestamps(:comments)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddArticleIdToComments < ActiveRecord::Migration
2
+ def change
3
+ add_column :comments, :article_id, :integer
4
+ end
5
+ end
@@ -10559,3 +10559,40 @@ Mysql2::Error: Table 'ssns_development.members' doesn't exist: SHOW FULL FIELDS
10559
10559
  ActiveRecord::SchemaMigration Load (0.2ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10560
10560
  ActiveRecord::SchemaMigration Load (0.3ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10561
10561
  ActiveRecord::SchemaMigration Load (0.3ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10562
+ ActiveRecord::SchemaMigration Load (35.8ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10563
+ Migrating to CreateComments (20160121020023)
10564
+  (62.5ms) 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
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10567
+ Migrating to CreateComments (20160121020023)
10568
+  (130.4ms) CREATE TABLE `comments` (`id` int(11) auto_increment PRIMARY KEY, `ctext` text, `commentator` varchar(255)) ENGINE=InnoDB
10569
+  (0.2ms) BEGIN
10570
+ SQL (0.3ms) INSERT INTO `schema_migrations` (`version`) VALUES ('20160121020023')
10571
+  (0.5ms) COMMIT
10572
+ Migrating to AddTimestampsToComments (20160121020232)
10573
+  (217.9ms) ALTER TABLE `comments` ADD `created_at` datetime
10574
+  (175.7ms) ALTER TABLE `comments` ADD `updated_at` datetime
10575
+  (0.2ms) BEGIN
10576
+ SQL (0.2ms) INSERT INTO `schema_migrations` (`version`) VALUES ('20160121020232')
10577
+  (0.5ms) COMMIT
10578
+ Migrating to AddArticleIdToComments (20160121020730)
10579
+  (210.6ms) ALTER TABLE `comments` ADD `article_id` int(11)
10580
+  (0.2ms) BEGIN
10581
+ SQL (0.2ms) INSERT INTO `schema_migrations` (`version`) VALUES ('20160121020730')
10582
+  (28.0ms) COMMIT
10583
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT `schema_migrations`.* FROM `schema_migrations`
10584
+ Comment Load (1.3ms) SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
10585
+ Comment Load (1.8ms) SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
10586
+ Comment Load (0.6ms) SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
10587
+ Mysql2::Error: Unknown column 'articles.headline' in 'order clause': SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
10588
+ Comment Load (0.5ms) 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
+ Comment Load (1.7ms) SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
10591
+ Comment Load (0.4ms) SELECT `comments`.* FROM `comments`
10592
+ Comment Load (0.4ms) SELECT `comments`.* FROM `comments`
10593
+ Comment Load (0.4ms) SELECT `comments`.* FROM `comments`
10594
+ Comment Load (0.3ms) SELECT `comments`.* FROM `comments`
10595
+ Comment Load (0.4ms) SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
10596
+ Mysql2::Error: Unknown column 'articles.headline' in 'order clause': SELECT `comments`.* FROM `comments` ORDER BY `articles`.`headline` ASC
10597
+ Comment Load (1.3ms) SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC
10598
+ Comment Load (1.7ms) SELECT `comments`.* FROM `comments` INNER JOIN `articles` ON `articles`.`id` = `comments`.`article_id` ORDER BY `articles`.`headline` ASC