table_sortable 1.0.0.pre.alpha.3 → 1.0.0.pre.alpha.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/table_sortable/column.rb +1 -1
- data/lib/table_sortable/columns.rb +4 -0
- data/lib/table_sortable/controller.rb +1 -78
- data/lib/table_sortable/core.rb +88 -0
- data/lib/table_sortable/query_params.rb +1 -1
- data/lib/table_sortable/version.rb +1 -1
- data/lib/table_sortable.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b98db7d33d20b683075edac94fe5ee1df23a3816
|
4
|
+
data.tar.gz: 30fc8c7ba54dbd07f5fe83d5319d0e474cb8acf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 572e3c065b21961613949494af13ac3f1b3f41352afa9ade5268bcf9b50cdcaad85afd4d69a2bed7597eb2ab407242b13582f6f26ba550f8d153d2324d24a271
|
7
|
+
data.tar.gz: 965022220b77acccc058d1f1e21df373847b1448289050f008c66905f4cd9d32daa0005af1b2d2f735db53595d3781b08166cbd038e7c260075f822643018599
|
@@ -15,7 +15,7 @@ module TableSortable
|
|
15
15
|
template = options[:template] || col_name
|
16
16
|
column_options = options[:options] || {}
|
17
17
|
|
18
|
-
@name = col_name
|
18
|
+
@name = col_name.to_sym
|
19
19
|
@value = value.respond_to?(:call) ? value : -> (record) { record.send(value) }
|
20
20
|
@content = content.respond_to?(:call) ? content : -> (record) { record.send(content) }
|
21
21
|
@label = label
|
@@ -3,6 +3,7 @@ module TableSortable
|
|
3
3
|
module Controller
|
4
4
|
|
5
5
|
extend ActiveSupport::Concern
|
6
|
+
include TableSortable::Core
|
6
7
|
|
7
8
|
included do
|
8
9
|
helper_method :columns
|
@@ -49,84 +50,6 @@ module TableSortable
|
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
|
-
def define_columns(*args)
|
53
|
-
options = args.extract_options!
|
54
|
-
column_offset = options[:offset] || 0
|
55
|
-
translation_key = options[:translation_key]
|
56
|
-
template_path = options[:template_path]
|
57
|
-
columns = args
|
58
|
-
define_translation_key translation_key
|
59
|
-
define_template_path template_path
|
60
|
-
define_column_offset column_offset
|
61
|
-
columns.each do |column|
|
62
|
-
define_column column, translation_key: translation_key
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def define_column(col_name, *options)
|
67
|
-
options = default_column_options.merge(options.extract_options!)
|
68
|
-
@columns.add(col_name, options.merge(controller: self))
|
69
|
-
end
|
70
|
-
|
71
|
-
def define_column_order(*order)
|
72
|
-
@column_order = order
|
73
|
-
end
|
74
|
-
|
75
|
-
def define_column_offset(offset)
|
76
|
-
@column_offset = offset
|
77
|
-
end
|
78
|
-
|
79
|
-
def define_translation_key(key)
|
80
|
-
@translation_key = key
|
81
|
-
end
|
82
|
-
|
83
|
-
def define_template_path(path)
|
84
|
-
@template_path = path.blank? ? nil : File.join(path, "")
|
85
|
-
end
|
86
|
-
|
87
|
-
def columns
|
88
|
-
@columns.sort_by(column_order)
|
89
|
-
end
|
90
|
-
|
91
|
-
private
|
92
|
-
|
93
|
-
def default_column_options
|
94
|
-
{translation_key: @translation_key, template_path: @template_path}
|
95
|
-
end
|
96
|
-
|
97
|
-
def filter_and_sort(scope, params = nil)
|
98
|
-
populate_params(params)
|
99
|
-
|
100
|
-
actions = [->(records) { records }]
|
101
|
-
ordered_actions(scope.first).reverse.each_with_index do |action, i|
|
102
|
-
actions << ->(records) { action.used? ? (actions[i].call(action.run(records))) : actions[i].call(records) }
|
103
|
-
end
|
104
|
-
scope = actions.last.call(scope) unless scope.blank?
|
105
|
-
if @query_params.page
|
106
|
-
scope = Result.new(scope, @query_params.page, @query_params.page_size)
|
107
|
-
end
|
108
|
-
|
109
|
-
scope
|
110
|
-
end
|
111
|
-
|
112
|
-
def initialize_table_sortable
|
113
|
-
@columns = TableSortable::Columns.new
|
114
|
-
define_column_offset 0
|
115
|
-
end
|
116
|
-
|
117
|
-
def ordered_actions(record = nil)
|
118
|
-
filter_actions = @columns.map{|col| col.filter }
|
119
|
-
sort_actions = @columns.map{|col| col.sorter }
|
120
|
-
(filter_actions+sort_actions).sort{ |a,b| (a.method(record) && b.method(record)) ? (a.method(record) <=> b.method(record)) : b.method(record) ? 1 : -1 }
|
121
|
-
end
|
122
|
-
|
123
|
-
def populate_params(params = nil)
|
124
|
-
@query_params = QueryParams.new(params || self.params, columns, column_offset)
|
125
|
-
end
|
126
|
-
|
127
|
-
public
|
128
|
-
|
129
|
-
attr_reader :column_order, :column_offset, :translation_key, :template_path
|
130
53
|
|
131
54
|
end
|
132
55
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'table_sortable/columns'
|
2
|
+
|
3
|
+
module TableSortable
|
4
|
+
module Core
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def define_columns(*args)
|
8
|
+
options = args.extract_options!
|
9
|
+
column_offset = options[:offset] || 0
|
10
|
+
translation_key = options[:translation_key]
|
11
|
+
template_path = options[:template_path]
|
12
|
+
columns = args
|
13
|
+
define_translation_key translation_key
|
14
|
+
define_template_path template_path
|
15
|
+
define_column_offset column_offset
|
16
|
+
columns.each do |column|
|
17
|
+
define_column column, translation_key: translation_key
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def define_column(col_name, *options)
|
22
|
+
options = default_column_options.merge(options.extract_options!)
|
23
|
+
@columns.add(col_name, options.merge(controller: self))
|
24
|
+
end
|
25
|
+
|
26
|
+
def define_column_order(*order)
|
27
|
+
@column_order = order
|
28
|
+
end
|
29
|
+
|
30
|
+
def define_column_offset(offset)
|
31
|
+
@column_offset = offset
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_translation_key(key)
|
35
|
+
@translation_key = key
|
36
|
+
end
|
37
|
+
|
38
|
+
def define_template_path(path)
|
39
|
+
@template_path = path.blank? ? nil : File.join(path, "")
|
40
|
+
end
|
41
|
+
|
42
|
+
def columns
|
43
|
+
@columns.sort_by(column_order)
|
44
|
+
end
|
45
|
+
|
46
|
+
# private
|
47
|
+
|
48
|
+
def default_column_options
|
49
|
+
{translation_key: @translation_key, template_path: @template_path}
|
50
|
+
end
|
51
|
+
|
52
|
+
def filter_and_sort(scope, params = nil)
|
53
|
+
populate_params(params)
|
54
|
+
|
55
|
+
actions = [->(records) { records }]
|
56
|
+
ordered_actions(scope.first).reverse.each_with_index do |action, i|
|
57
|
+
actions << ->(records) { action.used? ? (actions[i].call(action.run(records))) : actions[i].call(records) }
|
58
|
+
end
|
59
|
+
scope = actions.last.call(scope) unless scope.blank?
|
60
|
+
if @query_params.page
|
61
|
+
scope = Result.new(scope, @query_params.page, @query_params.page_size)
|
62
|
+
end
|
63
|
+
|
64
|
+
scope
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize_table_sortable
|
68
|
+
@columns = TableSortable::Columns.new
|
69
|
+
define_column_offset 0
|
70
|
+
end
|
71
|
+
|
72
|
+
def ordered_actions(record = nil)
|
73
|
+
filter_actions = @columns.map{|col| col.filter }
|
74
|
+
sort_actions = @columns.map{|col| col.sorter }
|
75
|
+
(filter_actions+sort_actions).sort{ |a,b| (a.method(record) && b.method(record)) ? (a.method(record) <=> b.method(record)) : b.method(record) ? 1 : -1 }
|
76
|
+
end
|
77
|
+
|
78
|
+
def populate_params(params = nil)
|
79
|
+
@query_params = QueryParams.new(params || self.params, columns, column_offset)
|
80
|
+
end
|
81
|
+
|
82
|
+
public
|
83
|
+
|
84
|
+
attr_reader :column_order, :column_offset, :translation_key, :template_path
|
85
|
+
# attr_accessor :params if defined?(self.class.params)
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
data/lib/table_sortable.rb
CHANGED
@@ -9,7 +9,7 @@ module TableSortable
|
|
9
9
|
PAGE = 'page'
|
10
10
|
PAGESIZE = 'pagesize'
|
11
11
|
|
12
|
-
class Engine < Rails::Engine; end
|
12
|
+
class Engine < Rails::Engine; end if defined?(Rails)
|
13
13
|
|
14
14
|
end
|
15
15
|
|
@@ -20,8 +20,8 @@ require 'table_sortable/column/sorter'
|
|
20
20
|
require 'table_sortable/column/filter'
|
21
21
|
require 'table_sortable/column'
|
22
22
|
require 'table_sortable/result'
|
23
|
-
require 'table_sortable/columns'
|
24
23
|
require 'table_sortable/version'
|
24
|
+
require 'table_sortable/core'
|
25
25
|
require 'table_sortable/controller'
|
26
26
|
require 'table_sortable/query_params'
|
27
27
|
require 'table_sortable/railtie' if defined?(Rails)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_sortable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.alpha.
|
4
|
+
version: 1.0.0.pre.alpha.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oded Davidov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -171,6 +171,7 @@ files:
|
|
171
171
|
- lib/table_sortable/columns.rb
|
172
172
|
- lib/table_sortable/concerns/proc.rb
|
173
173
|
- lib/table_sortable/controller.rb
|
174
|
+
- lib/table_sortable/core.rb
|
174
175
|
- lib/table_sortable/process.rb
|
175
176
|
- lib/table_sortable/query_params.rb
|
176
177
|
- lib/table_sortable/railtie.rb
|