mighty_grid 0.6.1 → 0.7.0

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: 74b8144f9e7e58cdc96ca3d0b807c64480fb1358
4
- data.tar.gz: 3df768d2465fef24ef982f16b48c1f63c9835f39
3
+ metadata.gz: 4cee3ab6ecd36d2c2204e7e0aeedaf98815f1c0b
4
+ data.tar.gz: 2457d2990260bb2a78977943ff6bc620b9c09ccf
5
5
  SHA512:
6
- metadata.gz: c198e16b636905e23ecdf31a2c9fc76c2275a4db88aee84df62ebf5e7532a78a1e25c925af505413c4886dafa1aff02562fb9f294a89815e2d16132765972717
7
- data.tar.gz: c5425779bef23b7b99d6b7b61531cc26990c73a8e86ba9e95ed652af42433645ab2f75dd09a0db460b4dd89dc34a5cfa47d3cc29d3c21ad0d6725539d8d4c510
6
+ metadata.gz: 888803988c9136627192a26fa6ea7c5420b02a6bb70d993a8e0f800e231c4711e787ac97ccb7304f5c7a19c3bb03038a295f5034e1f096e4ba0fc167e44ca7e3
7
+ data.tar.gz: 92d5a885185a8113182ea51eafb4d67e54d4e36d98ba37bae08de8845d13d46ffeeea1a3b79edd8f8235aa9ee44541c2dc06ac2a74d707baf935dea219a35bb0
data/README.md CHANGED
@@ -45,12 +45,19 @@ end
45
45
  You can configure the following default values by overriding these values using <tt>MightyGrid.configure</tt> method.
46
46
 
47
47
  ```
48
- per_page # 15 by default
49
- order_direction # 'asc' by default
50
- grid_name # 'grid' by default
51
- table_class # '' by default
52
- header_tr_class # '' by default
53
- pagination_theme # 'mighty_grid' by default
48
+ per_page # 15 by default
49
+ order_direction # 'asc' by default
50
+ order_type # 'single' by default
51
+ order_asc # '&uarr;' by default
52
+ order_desc # '&darr;' by default
53
+ order_asc_link_class # '' by default
54
+ order_desc_link_class # '' by default
55
+ order_active_link_class # 'mg-order-active' by default
56
+ order_wrapper_class # '' by default
57
+ grid_name # 'grid' by default
58
+ table_class # '' by default
59
+ header_tr_class # '' by default
60
+ pagination_theme # 'mighty_grid' by default
54
61
  ```
55
62
 
56
63
  There's a handy generator that generates the default configuration file into config/initializers directory.
@@ -58,6 +65,17 @@ Run the following generator command, then edit the generated file.
58
65
 
59
66
  $ rails g mighty_grid:install
60
67
 
68
+ ## Running tests
69
+
70
+ To run the tests you need specify database and Rails version.
71
+
72
+ * List of available Rails versions: 3.2, 4.0, 4.1.
73
+ * List of DB: sqlite, postgresql, mysql.
74
+
75
+ Example run:
76
+
77
+ $ DB=postgresql appraisal rails_32 rake spec cucumber
78
+
61
79
  ## Contributing
62
80
 
63
81
  1. Fork it ( http://github.com/jurrick/mighty_grid/fork )
@@ -1,5 +1,6 @@
1
1
  en:
2
2
  mighty_grid:
3
+ display_entries: "<strong>%{first} &ndash; %{last} of %{total}</strong>"
3
4
  filters:
4
5
  submit: Apply changes
5
- reset: Reset changes
6
+ reset: Reset changes
@@ -11,5 +11,5 @@ MightyGrid.configure do |config|
11
11
  # config.grid_name = 'grid'
12
12
  # config.table_class = ''
13
13
  # config.header_tr_class = ''
14
- # config.pagination_template = 'mighty_grid'
14
+ # config.pagination_theme = 'mighty_grid'
15
15
  end
@@ -15,7 +15,8 @@ module MightyGrid
15
15
  include: nil,
16
16
  joins: nil,
17
17
  conditions: nil,
18
- group: nil
18
+ group: nil,
19
+ order: nil
19
20
  }
20
21
 
21
22
  opts.assert_valid_keys(@options.keys)
@@ -32,7 +33,12 @@ module MightyGrid
32
33
 
33
34
  def read
34
35
  apply_filters
35
- @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}") if @mg_params[:order].present? && current_order_direction.present?
36
+ if @mg_params[:order].present? && current_order_direction.present?
37
+ @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}")
38
+ else
39
+ @relation = @relation.order(@mg_params[:order])
40
+ end
41
+
36
42
  @relation = @relation
37
43
  .page(@mg_params[:page])
38
44
  .per(@mg_params[:per_page])
@@ -81,6 +87,7 @@ module MightyGrid
81
87
  # Load grid parameters
82
88
  def load_grid_params
83
89
  @mg_params = {}
90
+ @mg_params[filter_param_name.to_sym] = {}
84
91
  @mg_params.merge!(@options)
85
92
  if current_grid_params
86
93
  @mg_params.merge!(current_grid_params.symbolize_keys)
@@ -97,7 +104,7 @@ module MightyGrid
97
104
 
98
105
  # Get filter parameters
99
106
  def filter_params
100
- get_current_grid_param(filter_param_name) || {}
107
+ @mg_params[filter_param_name.to_sym] || {}
101
108
  end
102
109
 
103
110
  # Get filter parameter name
@@ -105,6 +112,11 @@ module MightyGrid
105
112
  'f'
106
113
  end
107
114
 
115
+ # Add param in filters
116
+ def add_filter_param(param, value)
117
+ @mg_params[filter_param_name.to_sym][param] = value unless @mg_params[filter_param_name.to_sym].key?(param)
118
+ end
119
+
108
120
  # Get filter name by field
109
121
  def get_filter_name(field, model = nil)
110
122
  field_name = model.present? ? "#{model.table_name}.#{field}" : field
@@ -29,9 +29,10 @@ module MightyGrid
29
29
  case @render_value
30
30
  when String, Symbol
31
31
  rec = @model ? record.send(@model.to_s.underscore) : record
32
- return rec[@render_value]
32
+ return rec[@render_value.to_sym]
33
33
  when Proc
34
- value = @render_value.call(record)
34
+ value, attrs = @render_value.call(record)
35
+ @attrs.merge!(attrs || {})
35
36
  return ERB::Util.h(value).to_s.html_safe
36
37
  else
37
38
  # raise
@@ -25,11 +25,14 @@ module MightyGrid
25
25
  # Get <tt>select</tt> HTML tag
26
26
  def select(name, option_tags = nil, options = {})
27
27
  @grid.filters[name] = option_tags
28
- selected = nil
29
- selected = options.delete(:selected) if options.key?(:selected)
30
-
31
28
  f_options = filter_options(name, options)
32
29
 
30
+ selected = nil
31
+ if options.key?(:selected)
32
+ selected = options.delete(:selected)
33
+ @grid.add_filter_param(get_filter_param_name(name, f_options[:model]), selected)
34
+ end
35
+
33
36
  selected = get_filter_param(name, f_options[:model]) if get_filter_param(name, f_options[:model])
34
37
  opts = options_for_select(option_tags, selected)
35
38
 
@@ -61,8 +64,12 @@ module MightyGrid
61
64
 
62
65
  private
63
66
 
67
+ def get_filter_param_name(name, model = nil)
68
+ model ? "#{model.table_name}.#{name}" : name.to_s
69
+ end
70
+
64
71
  def get_filter_param(name, model = nil)
65
- filter_name = model ? "#{model.table_name}.#{name}" : name
72
+ filter_name = get_filter_param_name(name, model)
66
73
  @grid.filter_params.key?(filter_name) ? @grid.filter_params[filter_name] : nil
67
74
  end
68
75
 
@@ -1,12 +1,13 @@
1
1
  module MightyGrid
2
2
  class GridRenderer
3
- attr_reader :columns, :th_columns, :blank_slate_handler
3
+ attr_reader :columns, :th_columns, :blank_slate_handler, :row_attributes_handler
4
4
 
5
5
  def initialize(grid, view)
6
6
  @grid = grid
7
7
  @columns = []
8
8
  @th_columns = []
9
9
  @blank_slate_handler = nil
10
+ @row_attributes_handler = proc {}
10
11
  end
11
12
 
12
13
  def column(attr_or_options = {}, options = nil, &block)
@@ -44,6 +45,10 @@ module MightyGrid
44
45
  @columns << MightyGrid::Column.new(title: 'Actions') { |object| @grid.controller.render_to_string(partial: options[:partial], locals: { actions: options[:only], object: object }) }
45
46
  end
46
47
 
48
+ def row_attributes(&block)
49
+ @row_attributes_handler = block if block_given?
50
+ end
51
+
47
52
  def blank_slate(html_or_opts = nil, &block)
48
53
  if (html_or_opts.is_a?(Hash) && html_or_opts.key?(:partial) || html_or_opts.is_a?(String)) && !block_given?
49
54
  @blank_slate_handler = html_or_opts
@@ -8,7 +8,7 @@ module MightyGrid
8
8
  def define_grid(grid, opts = {}, &block)
9
9
  rendering = GridRenderer.new(grid, self)
10
10
 
11
- block.call(rendering)
11
+ yield rendering
12
12
 
13
13
  options = {
14
14
  html: {},
@@ -137,7 +137,8 @@ module MightyGrid
137
137
  content_tag :tbody do
138
138
  html_record = ''
139
139
  grid.relation.each do |rel|
140
- html_record += content_tag :tr do
140
+ row_attributes = rendering.row_attributes_handler.call(rel) || {}
141
+ html_record += content_tag :tr, row_attributes do
141
142
  rendering.columns.map { |column| content_tag :td, column.render(rel), column.attrs }.join.html_safe
142
143
  end
143
144
  end
@@ -150,13 +151,18 @@ module MightyGrid
150
151
  content_tag :tr do
151
152
  content_tag :td, colspan: rendering.total_columns do
152
153
  html_pag = paginate(grid.relation, theme: MightyGrid.config.pagination_theme, param_name: "#{grid.name}[page]")
153
- html_pag += content_tag :strong do
154
- "#{grid.relation.offset_value + 1} &ndash; #{grid.relation.offset_value + grid.relation.size} of #{grid.relation.total_count}".html_safe
155
- end
154
+ html_pag += display_entries(grid)
156
155
  html_pag.html_safe
157
156
  end
158
157
  end.html_safe
159
158
  end
160
159
  end
160
+
161
+ def display_entries(grid)
162
+ first = grid.relation.offset_value + 1
163
+ last = grid.relation.offset_value + grid.relation.length
164
+ total = grid.relation.total_count
165
+ I18n.t("models.#{grid.klass.table_name}.display_entries", default: :'display_entries', scope: 'mighty_grid', first: first, last: last, total: total).html_safe
166
+ end
161
167
  end
162
168
  end
@@ -1,3 +1,3 @@
1
1
  module MightyGrid
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  end
data/mighty_grid.gemspec CHANGED
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rspec-rails', '~> 2.14.2'
24
24
  spec.add_development_dependency 'appraisal'
25
25
 
26
- spec.add_dependency 'kaminari', '~> 0.15.0'
26
+ spec.add_dependency 'kaminari', '~> 0.15'
27
27
  end
@@ -13,8 +13,10 @@ describe MightyGrid::Base do
13
13
  include: nil,
14
14
  joins: nil,
15
15
  conditions: nil,
16
- group: nil
16
+ group: nil,
17
+ order: nil
17
18
  }
19
+ @mg_params = @default_options.merge(f: {})
18
20
  end
19
21
 
20
22
  describe '#new' do
@@ -22,7 +24,7 @@ describe MightyGrid::Base do
22
24
  subject { MightyGrid::Base.new(User, @controller) }
23
25
  its(:params) { should == {} }
24
26
  its(:options) { should == @default_options }
25
- its(:mg_params) { should == @default_options }
27
+ its(:mg_params) { should == @mg_params }
26
28
  its(:filters) { should == {} }
27
29
  its(:name) { should == 'grid' }
28
30
  its(:relation) { should == User }
@@ -45,7 +47,7 @@ describe MightyGrid::Base do
45
47
  before(:all) { @controller.params = { 'grid' => { page: 5, per_page: 30, name: 'grid2' } } }
46
48
  subject { MightyGrid::Base.new(User, @controller) }
47
49
  its(:params) { should == @controller.params }
48
- its(:mg_params) { should == @default_options.merge(page: 5, per_page: 30, name: 'grid2') }
50
+ its(:mg_params) { should == @mg_params.merge(page: 5, per_page: 30, name: 'grid2') }
49
51
  after(:all) { @controller.params = {} }
50
52
  end
51
53
 
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.6.1
4
+ version: 0.7.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-07-28 00:00:00.000000000 Z
11
+ date: 2014-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.15.0
75
+ version: '0.15'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.15.0
82
+ version: '0.15'
83
83
  description: Flexible grid for Rails
84
84
  email:
85
85
  - jurianp@gmail.com