mighty_grid 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/lib/generators/mighty_grid/templates/mighty_grid_config.rb +7 -0
- data/lib/mighty_grid/base.rb +8 -3
- data/lib/mighty_grid/column.rb +7 -1
- data/lib/mighty_grid/config.rb +14 -0
- data/lib/mighty_grid/grid_renderer.rb +17 -6
- data/lib/mighty_grid/helpers/mighty_grid_view_helpers.rb +97 -45
- data/lib/mighty_grid/version.rb +1 -1
- data/mighty_grid.gemspec +1 -1
- data/spec/config_spec.rb +77 -0
- data/vendor/assets/stylesheets/mighty_grid.css +10 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b11eef1b69fe270d7e786eadf6f0758d085980ae
|
4
|
+
data.tar.gz: 54957782d3271cb70a00f0fc482cfc98052275aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90b83e534bea92905baa622567bfac8218d3e469d3596029f869958f747377fccec7e80dac99653712aa100e8e26815238a2ec1becae65259a2faaa65781a3c2
|
7
|
+
data.tar.gz: 2c586fccd7d33ae48fa9d00b628524497d785507ed9944d0b6d8d1e3d50c08b6069645cbba368a6e4fa731db6ccca61665f5e244c3ee72cc43a8583ba465c774
|
data/.travis.yml
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
MightyGrid.configure do |config|
|
2
2
|
# config.per_page = 15
|
3
3
|
# config.order_direction = 'asc'
|
4
|
+
# config.order_type = 'single'
|
5
|
+
# config.order_asc = '↑'
|
6
|
+
# config.order_desc = '↓'
|
7
|
+
# config.order_asc_link_class = ''
|
8
|
+
# config.order_desc_link_class = ''
|
9
|
+
# config.order_active_link_class = ''
|
10
|
+
# config.order_wrapper_class = ''
|
4
11
|
# config.grid_name = 'grid'
|
5
12
|
# config.table_class = ''
|
6
13
|
# config.header_tr_class = ''
|
data/lib/mighty_grid/base.rb
CHANGED
@@ -56,7 +56,7 @@ module MightyGrid
|
|
56
56
|
end
|
57
57
|
|
58
58
|
next if filter_value.blank? || !model.column_names.include?(name)
|
59
|
-
|
59
|
+
|
60
60
|
if model && model.superclass == ActiveRecord::Base
|
61
61
|
filter_type = model.columns_hash[name].type
|
62
62
|
else
|
@@ -119,12 +119,17 @@ module MightyGrid
|
|
119
119
|
end
|
120
120
|
|
121
121
|
# Get order parameters
|
122
|
-
def order_params(attribute, model = nil)
|
122
|
+
def order_params(attribute, model = nil, direction = nil)
|
123
123
|
order = model.present? ? "#{model.table_name}.#{attribute}" : attribute.to_s
|
124
|
-
direction
|
124
|
+
direction ||= order == current_grid_params['order'] ? another_order_direction : 'asc'
|
125
125
|
{@name => {order: order, order_direction: direction}}
|
126
126
|
end
|
127
127
|
|
128
|
+
# Get current order direction if current order parameter coincides with the received parameter
|
129
|
+
def get_active_order_direction(parameters)
|
130
|
+
(parameters[@name]['order'] == current_grid_params['order']) ? current_order_direction : nil
|
131
|
+
end
|
132
|
+
|
128
133
|
# Get current order direction
|
129
134
|
def current_order_direction
|
130
135
|
direction = nil
|
data/lib/mighty_grid/column.rb
CHANGED
@@ -8,7 +8,13 @@ module MightyGrid
|
|
8
8
|
@attrs = {}
|
9
9
|
@th_attrs = {}
|
10
10
|
if block_given?
|
11
|
-
|
11
|
+
if attr_or_options.is_a?(Hash)
|
12
|
+
@options = attr_or_options || {}
|
13
|
+
else
|
14
|
+
@options = options || {}
|
15
|
+
@attribute = attr_or_options if attr_or_options.present?
|
16
|
+
end
|
17
|
+
|
12
18
|
@render_value = block
|
13
19
|
else
|
14
20
|
@options = options || {}
|
data/lib/mighty_grid/config.rb
CHANGED
@@ -18,6 +18,13 @@ module MightyGrid
|
|
18
18
|
include ActiveSupport::Configurable
|
19
19
|
config_accessor :per_page
|
20
20
|
config_accessor :order_direction
|
21
|
+
config_accessor :order_type
|
22
|
+
config_accessor :order_asc
|
23
|
+
config_accessor :order_desc
|
24
|
+
config_accessor :order_asc_link_class
|
25
|
+
config_accessor :order_desc_link_class
|
26
|
+
config_accessor :order_active_link_class
|
27
|
+
config_accessor :order_wrapper_class
|
21
28
|
config_accessor :grid_name
|
22
29
|
config_accessor :header_tr_class
|
23
30
|
config_accessor :table_class
|
@@ -27,6 +34,13 @@ module MightyGrid
|
|
27
34
|
configure do |config|
|
28
35
|
config.per_page = 15
|
29
36
|
config.order_direction = 'asc'
|
37
|
+
config.order_type = 'single'
|
38
|
+
config.order_asc = '↑'
|
39
|
+
config.order_desc = '↓'
|
40
|
+
config.order_asc_link_class = ''
|
41
|
+
config.order_desc_link_class = ''
|
42
|
+
config.order_active_link_class = 'mg-order-active'
|
43
|
+
config.order_wrapper_class = ''
|
30
44
|
config.grid_name = 'grid'
|
31
45
|
config.table_class = ''
|
32
46
|
config.header_tr_class = ''
|
@@ -10,18 +10,29 @@ module MightyGrid
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def column(attr_or_options = {}, options=nil, &block)
|
13
|
-
if
|
13
|
+
if attr_or_options.is_a?(Hash)
|
14
14
|
options = attr_or_options.symbolize_keys
|
15
|
-
@columns << MightyGrid::Column.new(options, &block)
|
16
15
|
else
|
17
16
|
attribute = attr_or_options.to_sym
|
18
17
|
options = {} unless options.is_a?(Hash)
|
19
|
-
|
20
|
-
|
18
|
+
end
|
19
|
+
|
20
|
+
if attribute.present?
|
21
|
+
options = {
|
21
22
|
ordering: true,
|
22
|
-
attribute: attribute
|
23
|
+
attribute: attribute,
|
24
|
+
title: @grid.klass.human_attribute_name(attribute)
|
23
25
|
}.merge!(options)
|
24
|
-
|
26
|
+
end
|
27
|
+
|
28
|
+
if block_given?
|
29
|
+
if attribute.present?
|
30
|
+
@columns << MightyGrid::Column.new(attribute, options, &block)
|
31
|
+
else
|
32
|
+
@columns << MightyGrid::Column.new(options, &block)
|
33
|
+
end
|
34
|
+
else
|
35
|
+
@columns << MightyGrid::Column.new(attribute, options)
|
25
36
|
end
|
26
37
|
@total_columns = @columns.count
|
27
38
|
end
|
@@ -5,14 +5,31 @@ module MightyGrid
|
|
5
5
|
render_grid(grid)
|
6
6
|
end
|
7
7
|
|
8
|
-
def define_grid(grid,
|
8
|
+
def define_grid(grid, opts={}, &block)
|
9
9
|
|
10
10
|
rendering = GridRenderer.new(grid, self)
|
11
11
|
|
12
12
|
block.call(rendering)
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
options = {
|
15
|
+
html: {},
|
16
|
+
header_tr_html: {},
|
17
|
+
order_type: MightyGrid.config.order_type,
|
18
|
+
order_asc: MightyGrid.config.order_asc,
|
19
|
+
order_desc: MightyGrid.config.order_desc,
|
20
|
+
order_asc_link_class: MightyGrid.config.order_asc_link_class,
|
21
|
+
order_desc_link_class: MightyGrid.config.order_desc_link_class,
|
22
|
+
order_active_link_class: MightyGrid.config.order_active_link_class,
|
23
|
+
order_wrapper_class: MightyGrid.config.order_wrapper_class
|
24
|
+
}
|
25
|
+
|
26
|
+
opts.assert_valid_keys(options.keys)
|
27
|
+
|
28
|
+
options.merge!(opts)
|
29
|
+
|
30
|
+
options[:order_wrapper_class] = MightyGrid::MgHTML.join_html_classes({}, 'mg-order-wrapper', options[:order_wrapper_class])[:class]
|
31
|
+
|
32
|
+
table_html_attrs = MightyGrid::MgHTML.join_html_classes(options[:html], 'mighty-grid', MightyGrid.config.table_class)
|
16
33
|
|
17
34
|
grid.read
|
18
35
|
|
@@ -49,62 +66,97 @@ module MightyGrid
|
|
49
66
|
form_tag(options[:url] || {}, html_options){ output }
|
50
67
|
end
|
51
68
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
rendering.columns.map { |column|
|
59
|
-
content_tag :th, column.th_attrs do
|
60
|
-
if column.options[:ordering] && column.attribute.present?
|
61
|
-
link_to(column.title, "?#{MightyGrid::MgHash.rec_merge(grid.params, grid.order_params(column.attribute, column.model)).except('controller', 'action').to_query}").html_safe
|
62
|
-
else
|
63
|
-
column.title.html_safe
|
64
|
-
end
|
65
|
-
end
|
66
|
-
}.join.html_safe
|
69
|
+
def blank_slate_template(rendering)
|
70
|
+
if rendering.blank_slate_handler.present?
|
71
|
+
case rendering.blank_slate_handler
|
72
|
+
when Proc; rendering.blank_slate_handler.call
|
73
|
+
when String; rendering.blank_slate_handler
|
74
|
+
when Hash; render(rendering.blank_slate_handler)
|
67
75
|
end
|
76
|
+
else
|
77
|
+
content_tag :div, 'No records found'
|
68
78
|
end
|
69
79
|
end
|
70
80
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
81
|
+
private
|
82
|
+
|
83
|
+
def header_grid_html(rendering, grid, options)
|
84
|
+
header_tr_html = MightyGrid::MgHTML.join_html_classes(options[:header_tr_html], MightyGrid.config.header_tr_class)
|
85
|
+
|
86
|
+
content_tag :thead do
|
87
|
+
content_tag :tr, header_tr_html do
|
88
|
+
rendering.columns.map { |column|
|
89
|
+
content_tag(:th, column.th_attrs){ grid_title_html(grid, column, options) }
|
90
|
+
}.join.html_safe
|
77
91
|
end
|
78
92
|
end
|
79
|
-
html_record.html_safe
|
80
93
|
end
|
81
|
-
end
|
82
94
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
95
|
+
def grid_title_html(grid, column, options)
|
96
|
+
if column.options[:ordering] && column.attribute.present?
|
97
|
+
order_asc = column.options[:order_asc] || options[:order_asc]
|
98
|
+
order_desc = column.options[:order_desc] || options[:order_desc]
|
99
|
+
|
100
|
+
case options[:order_type]
|
101
|
+
when 'pair'
|
102
|
+
order_active = grid.get_active_order_direction(grid_order_params(grid, column))
|
103
|
+
order_asc_class = MightyGrid::MgHTML.join_html_classes({}, (order_active == 'asc' ? options[:order_active_link_class] : nil), options[:order_asc_link_class])[:class]
|
104
|
+
order_desc_class = MightyGrid::MgHTML.join_html_classes({}, (order_active == 'desc' ? options[:order_active_link_class] : nil), options[:order_desc_link_class])[:class]
|
105
|
+
|
106
|
+
html_title = column.title
|
107
|
+
html_title += content_tag :div, class: options[:order_wrapper_class] do
|
108
|
+
html_order = get_order_html(grid, column, order_active, order_asc, 'asc', class: order_asc_class)
|
109
|
+
html_order += ' '
|
110
|
+
html_order += get_order_html(grid, column, order_active, order_desc, 'desc', class: order_desc_class)
|
111
|
+
html_order.html_safe
|
90
112
|
end
|
91
|
-
|
113
|
+
else
|
114
|
+
html_title = link_to(column.title.html_safe, "?#{grid_order_params(grid, column).to_query}")
|
92
115
|
end
|
93
|
-
|
116
|
+
html_title.html_safe
|
117
|
+
else
|
118
|
+
column.title.html_safe
|
119
|
+
end
|
94
120
|
end
|
95
|
-
end
|
96
121
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
122
|
+
def get_order_html(grid, column, order_active, order_html, direction = nil, html_options = {})
|
123
|
+
if order_active == direction
|
124
|
+
html = content_tag :span, order_html.html_safe, html_options
|
125
|
+
else
|
126
|
+
html = link_to(order_html.html_safe, "?#{grid_order_params(grid, column, direction).to_query}", html_options)
|
127
|
+
end
|
128
|
+
html.html_safe
|
129
|
+
end
|
130
|
+
|
131
|
+
def grid_order_params(grid, column, direction = nil)
|
132
|
+
MightyGrid::MgHash.rec_merge(grid.params, grid.order_params(column.attribute, column.model, direction)).except('controller', 'action')
|
133
|
+
end
|
134
|
+
|
135
|
+
def body_grid_html(rendering, grid)
|
136
|
+
content_tag :tbody do
|
137
|
+
html_record = ''
|
138
|
+
grid.relation.each do |rel|
|
139
|
+
html_record += content_tag :tr do
|
140
|
+
rendering.columns.map{|column| content_tag :td, column.render(rel), column.attrs}.join.html_safe
|
141
|
+
end
|
142
|
+
end
|
143
|
+
html_record.html_safe
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def footer_grid_html(rendering, grid)
|
148
|
+
content_tag :tfoot do
|
149
|
+
content_tag :tr do
|
150
|
+
content_tag :td, colspan: rendering.total_columns do
|
151
|
+
html_pag = paginate(grid.relation, theme: MightyGrid.config.pagination_theme, param_name: "#{grid.name}[page]")
|
152
|
+
html_pag += content_tag :strong do
|
153
|
+
"#{grid.relation.offset_value + 1} – #{grid.relation.offset_value + grid.relation.size} of #{grid.relation.total_count}".html_safe
|
154
|
+
end
|
155
|
+
html_pag.html_safe
|
156
|
+
end
|
157
|
+
end.html_safe
|
103
158
|
end
|
104
|
-
else
|
105
|
-
content_tag :div, 'No records found'
|
106
159
|
end
|
107
|
-
end
|
108
160
|
|
109
161
|
end
|
110
162
|
end
|
data/lib/mighty_grid/version.rb
CHANGED
data/mighty_grid.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.5"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "rspec-rails"
|
23
|
+
spec.add_development_dependency "rspec-rails", "~> 2.14.2"
|
24
24
|
spec.add_development_dependency 'appraisal'
|
25
25
|
|
26
26
|
spec.add_dependency 'kaminari', '~> 0.15.0'
|
data/spec/config_spec.rb
CHANGED
@@ -25,6 +25,83 @@ describe MightyGrid::Configuration do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
describe 'order_type' do
|
29
|
+
context 'by default' do
|
30
|
+
its(:order_type) { should == 'single' }
|
31
|
+
end
|
32
|
+
context 'configured via config block' do
|
33
|
+
before { MightyGrid.configure {|c| c.order_type = 'pair'} }
|
34
|
+
its(:order_type) { should == 'pair' }
|
35
|
+
after { MightyGrid.configure {|c| c.order_type = 'single'} }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'order_asc' do
|
40
|
+
context 'by default' do
|
41
|
+
its(:order_asc) { should == '↑' }
|
42
|
+
end
|
43
|
+
context 'configured via config block' do
|
44
|
+
before { MightyGrid.configure {|c| c.order_asc = '∧'} }
|
45
|
+
its(:order_asc) { should == '∧' }
|
46
|
+
after { MightyGrid.configure {|c| c.order_asc = '↑'} }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'order_desc' do
|
51
|
+
context 'by default' do
|
52
|
+
its(:order_desc) { should == '↓' }
|
53
|
+
end
|
54
|
+
context 'configured via config block' do
|
55
|
+
before { MightyGrid.configure {|c| c.order_desc = '∨'} }
|
56
|
+
its(:order_desc) { should == '∨' }
|
57
|
+
after { MightyGrid.configure {|c| c.order_desc = '↓'} }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'order_asc_link_class' do
|
62
|
+
context 'by default' do
|
63
|
+
its(:order_asc_link_class) { should == '' }
|
64
|
+
end
|
65
|
+
context 'configured via config block' do
|
66
|
+
before { MightyGrid.configure {|c| c.order_asc_link_class = 'btn'} }
|
67
|
+
its(:order_asc_link_class) { should == 'btn' }
|
68
|
+
after { MightyGrid.configure {|c| c.order_asc_link_class = ''} }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'order_active_link_class' do
|
73
|
+
context 'by default' do
|
74
|
+
its(:order_active_link_class) { should == 'mg-order-active' }
|
75
|
+
end
|
76
|
+
context 'configured via config block' do
|
77
|
+
before { MightyGrid.configure {|c| c.order_active_link_class = 'btn-primary'} }
|
78
|
+
its(:order_active_link_class) { should == 'btn-primary' }
|
79
|
+
after { MightyGrid.configure {|c| c.order_active_link_class = ''} }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'order_desc_link_class' do
|
84
|
+
context 'by default' do
|
85
|
+
its(:order_desc_link_class) { should == '' }
|
86
|
+
end
|
87
|
+
context 'configured via config block' do
|
88
|
+
before { MightyGrid.configure {|c| c.order_desc_link_class = 'btn'} }
|
89
|
+
its(:order_desc_link_class) { should == 'btn' }
|
90
|
+
after { MightyGrid.configure {|c| c.order_desc_link_class = ''} }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe 'order_wrapper_class' do
|
95
|
+
context 'by default' do
|
96
|
+
its(:order_wrapper_class) { should == '' }
|
97
|
+
end
|
98
|
+
context 'configured via config block' do
|
99
|
+
before { MightyGrid.configure {|c| c.order_wrapper_class = 'btn-group'} }
|
100
|
+
its(:order_wrapper_class) { should == 'btn-group' }
|
101
|
+
after { MightyGrid.configure {|c| c.order_wrapper_class = ''} }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
28
105
|
describe 'grid_name' do
|
29
106
|
context 'by default' do
|
30
107
|
its(:grid_name) { should == 'grid' }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mighty_grid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jurrick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 2.14.2
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 2.14.2
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: appraisal
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|