mighty_grid 0.4.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db143f79e6b1f6002945861e8da4884867dda3b8
4
- data.tar.gz: c2164ac042c84c00515e03dd41dcb5ddddd3c2f3
3
+ metadata.gz: b11eef1b69fe270d7e786eadf6f0758d085980ae
4
+ data.tar.gz: 54957782d3271cb70a00f0fc482cfc98052275aa
5
5
  SHA512:
6
- metadata.gz: 8a36b57f6971bb4e3437555f56cccfe0dde9349d5cf5ef809ebcb29dd6db09899ab85e23b78d1fc4ecc7c5aa21bb20f3e2540318de0b78b52db2eb9b8c00ee18
7
- data.tar.gz: 753d836d4737f542a101362b177c6d0762e260a9851b868ecc04bc830c22594e70d3df890428ffe1c8683b65ee2f3d83eb02ba33ab64a67cbc9b8bb364e14430
6
+ metadata.gz: 90b83e534bea92905baa622567bfac8218d3e469d3596029f869958f747377fccec7e80dac99653712aa100e8e26815238a2ec1becae65259a2faaa65781a3c2
7
+ data.tar.gz: 2c586fccd7d33ae48fa9d00b628524497d785507ed9944d0b6d8d1e3d50c08b6069645cbba368a6e4fa731db6ccca61665f5e244c3ee72cc43a8583ba465c774
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  - 2.1.1
6
5
 
@@ -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 = ''
@@ -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 = order == current_grid_params['order'] ? another_order_direction : 'asc'
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
@@ -8,7 +8,13 @@ module MightyGrid
8
8
  @attrs = {}
9
9
  @th_attrs = {}
10
10
  if block_given?
11
- @options = attr_or_options || {}
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 || {}
@@ -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 block_given?
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
- opts = {
20
- title: @grid.klass.human_attribute_name(attribute),
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
- @columns << MightyGrid::Column.new(attribute, opts)
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, options={}, &block)
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
- table_html_attrs = options[:html] || {}
15
- table_html_attrs = MightyGrid::MgHTML.join_html_classes(table_html_attrs, 'mighty-grid', MightyGrid.config.table_class)
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 header_grid_html(rendering, grid, options)
53
- header_tr_html = options[:header_tr_html] || {}
54
- header_tr_html = MightyGrid::MgHTML.join_html_classes(header_tr_html, MightyGrid.config.header_tr_class)
55
-
56
- content_tag :thead do
57
- content_tag :tr, header_tr_html do
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
- def body_grid_html(rendering, grid)
72
- content_tag :tbody do
73
- html_record = ''
74
- grid.relation.each do |rel|
75
- html_record += content_tag :tr do
76
- rendering.columns.map{|column| content_tag :td, column.render(rel), column.attrs}.join.html_safe
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
- def footer_grid_html(rendering, grid)
84
- content_tag :tfoot do
85
- content_tag :tr do
86
- content_tag :td, colspan: rendering.total_columns do
87
- html_pag = paginate(grid.relation, theme: MightyGrid.config.pagination_theme, param_name: "#{grid.name}[page]")
88
- html_pag += content_tag :strong do
89
- "#{grid.relation.offset_value + 1} &ndash; #{grid.relation.offset_value + grid.relation.size} of #{grid.relation.total_count}".html_safe
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
- html_pag.html_safe
113
+ else
114
+ html_title = link_to(column.title.html_safe, "?#{grid_order_params(grid, column).to_query}")
92
115
  end
93
- end.html_safe
116
+ html_title.html_safe
117
+ else
118
+ column.title.html_safe
119
+ end
94
120
  end
95
- end
96
121
 
97
- def blank_slate_template(rendering)
98
- if rendering.blank_slate_handler.present?
99
- case rendering.blank_slate_handler
100
- when Proc; rendering.blank_slate_handler.call
101
- when String; rendering.blank_slate_handler
102
- when Hash; render(rendering.blank_slate_handler)
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} &ndash; #{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
@@ -1,3 +1,3 @@
1
1
  module MightyGrid
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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 == '&uarr;' }
42
+ end
43
+ context 'configured via config block' do
44
+ before { MightyGrid.configure {|c| c.order_asc = '&and;'} }
45
+ its(:order_asc) { should == '&and;' }
46
+ after { MightyGrid.configure {|c| c.order_asc = '&uarr;'} }
47
+ end
48
+ end
49
+
50
+ describe 'order_desc' do
51
+ context 'by default' do
52
+ its(:order_desc) { should == '&darr;' }
53
+ end
54
+ context 'configured via config block' do
55
+ before { MightyGrid.configure {|c| c.order_desc = '&or;'} }
56
+ its(:order_desc) { should == '&or;' }
57
+ after { MightyGrid.configure {|c| c.order_desc = '&darr;'} }
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' }
@@ -1,3 +1,13 @@
1
1
  .mighty-grid .pagination {
2
2
  margin: 0;
3
+ }
4
+
5
+ .mighty-grid .mg-order-wrapper {
6
+ float: right !important;
7
+ margin-top: 0px;
8
+ margin-bottom: 0px;
9
+ }
10
+
11
+ .mighty-grid .mg-order-active {
12
+ color: #000;
3
13
  }
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.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-07 00:00:00.000000000 Z
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: '0'
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: '0'
54
+ version: 2.14.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: appraisal
57
57
  requirement: !ruby/object:Gem::Requirement