mighty_grid 0.2.0 → 0.2.1

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: 4844d991f9821e4d7f8c255c34eda1e0e162cf5b
4
- data.tar.gz: fa07a1f87c95c2ac6d7fafa723d89c3d7010c7b4
3
+ metadata.gz: 4b86bd1ed6d0734fa6d924b5b462469828a65ee0
4
+ data.tar.gz: 40241a4547e70b2829b4abe1e579cb757ea44fb2
5
5
  SHA512:
6
- metadata.gz: a7cfed903a2b101ce0fcf90451588fd9162ca5285720e8645c25bfa5535a452e2e37820d3a59c3b36085cbc188ce997387a61edd98bcb6a204a910684e276bf5
7
- data.tar.gz: 8a26a1320f48f36084caee47be29ceeb490a03dfb170846ae2b792071f76a5ce44197354ac71b0a7ef190ae423f3cb76b77a8140f708f3359cc0e4cbf31a2a28
6
+ metadata.gz: 7b364645a807c3268e3f073118f30832c852e9a312f9cb21e554122579a2d5ef27f3480ad43237178c253677f5fa05fceb0336b1b9103d67e98836a98ffba7d2
7
+ data.tar.gz: 3c25be9911a6eb1905a455a53c08f5e4419c97d1ce2fcebe06ca04f53e976d7c9ebabd2fc51ae6a0a413f55a3fb449cb9a336222e28afaa1cd10e7299a5ff201
@@ -77,6 +77,10 @@ module MightyGrid
77
77
  params[name] || {}
78
78
  end
79
79
 
80
+ def order_params(attribute)
81
+ {@name => {order: attribute, order_direction: order_direction}}
82
+ end
83
+
80
84
  def order_direction
81
85
  (current_grid_params.has_key?('order_direction')) ? (['asc', 'desc'] - [current_grid_params['order_direction'].to_s]).first : MightyGrid.config.order_direction
82
86
  end
@@ -1,31 +1,30 @@
1
1
  module MightyGrid
2
2
  class Column
3
3
 
4
- attr_reader :display_property, :attribute, :attrs, :th_attrs, :options, :title
4
+ attr_reader :attribute, :attrs, :th_attrs, :options, :title
5
5
  attr_accessor :render_value
6
6
 
7
7
  def initialize(attr_or_options=nil, options=nil, &block)
8
8
  @attrs = {}
9
+ @th_attrs = {}
9
10
  if block_given?
10
- @options = attr_or_options
11
- @display_property = :block
11
+ @options = attr_or_options || {}
12
12
  @render_value = block
13
13
  else
14
- @options = options
14
+ @options = options || {}
15
15
  @attribute = attr_or_options
16
- @display_property = :attr
17
- @render_value = attribute
16
+ @render_value = @attribute
18
17
  end
19
- @attrs = options[:html] if options && options.has_key?(:html)
20
- @th_attrs = options[:th_html] if options && options.has_key?(:th_html)
21
- @title = options[:title] || ''
18
+ @attrs = @options[:html] if @options.has_key?(:html)
19
+ @th_attrs = @options[:th_html] if @options.has_key?(:th_html)
20
+ @title = @options.has_key?(:title) && @options[:title] || ''
22
21
  end
23
22
 
24
23
  def render(record)
25
- case @display_property
26
- when :attr
24
+ case @render_value
25
+ when String, Symbol
27
26
  return record[@render_value]
28
- when :block
27
+ when Proc
29
28
  value = @render_value.call(record)
30
29
  return ERB::Util.h(value).to_s.html_safe
31
30
  else
@@ -56,8 +56,8 @@ module MightyGrid
56
56
  content_tag :tr, header_tr_html do
57
57
  rendering.columns.map { |column|
58
58
  content_tag :th, column.th_attrs do
59
- if column.options[:ordering]
60
- link_to(column.title, "?#{MightyGrid::MgHash.rec_merge(grid.params, {grid.name => {order: column.attribute, order_direction: grid.order_direction}}).except('controller', 'action').to_query}").html_safe
59
+ if column.options[:ordering] && column.attribute.present?
60
+ link_to(column.title, "?#{MightyGrid::MgHash.rec_merge(grid.params, grid.order_params(column.attribute)).except('controller', 'action').to_query}").html_safe
61
61
  else
62
62
  column.title.html_safe
63
63
  end
@@ -96,9 +96,9 @@ module MightyGrid
96
96
  def blank_slate_template(rendering)
97
97
  if rendering.blank_slate_handler.present?
98
98
  case rendering.blank_slate_handler
99
- when Proc; return rendering.blank_slate_handler.call
100
- when String; return rendering.blank_slate_handler
101
- when Hash; return render(rendering.blank_slate_handler)
99
+ when Proc; rendering.blank_slate_handler.call
100
+ when String; rendering.blank_slate_handler
101
+ when Hash; render(rendering.blank_slate_handler)
102
102
  end
103
103
  else
104
104
  content_tag :div, 'No records found'
@@ -1,3 +1,3 @@
1
1
  module MightyGrid
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe MightyGrid::Column do
4
+
5
+ describe '#new' do
6
+ describe 'with attribute' do
7
+ context 'without options' do
8
+ subject { MightyGrid::Column.new(:name) }
9
+ context 'parameters' do
10
+ its(:attribute) { should == :name }
11
+ its(:render_value) { should == :name }
12
+ its(:title) { should == '' }
13
+ its(:attrs) { should == {} }
14
+ its(:th_attrs) { should == {} }
15
+ end
16
+ end
17
+
18
+ context 'with html option' do
19
+ let(:options) { {html: {class: 'column'}} }
20
+ subject { MightyGrid::Column.new(:name, options) }
21
+ its(:options) { should == options }
22
+ its(:attrs) { should == options[:html] }
23
+ end
24
+
25
+ context 'with title option' do
26
+ let(:options) { {title: 'Name'} }
27
+ subject { MightyGrid::Column.new(:name, options) }
28
+ its(:options) { should == options }
29
+ its(:title) { should == options[:title] }
30
+ end
31
+
32
+ context 'with th_html option' do
33
+ let(:options) { {th_html: {class: 'active'}} }
34
+ subject { MightyGrid::Column.new(:name, options) }
35
+ its(:options) { should == options }
36
+ its(:th_attrs) { should == {class: 'active'} }
37
+ end
38
+ end
39
+
40
+ describe 'with block' do
41
+ context 'without options' do
42
+ subject { MightyGrid::Column.new {:test} }
43
+ context 'parameters' do
44
+ its(:attribute) { should == nil }
45
+ its(:title) { should == '' }
46
+ its(:render_value) { should be_an_instance_of Proc }
47
+ its(:attrs) { should == {} }
48
+ its(:th_attrs) { should == {} }
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '.render' do
55
+ let(:product){ Product.create(name: 'product name') }
56
+
57
+ describe 'with attribute' do
58
+ subject(:column){ MightyGrid::Column.new(:name) }
59
+ it 'should return attribute value' do
60
+ column.render(product).should == product[:name]
61
+ end
62
+ end
63
+
64
+ describe 'with block' do
65
+ subject(:column){ MightyGrid::Column.new { "#{product.name} 1" } }
66
+ it 'should return attribute value' do
67
+ column.render(product).should == "#{product[:name]} 1"
68
+ end
69
+ end
70
+ end
71
+
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mighty_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jurrick
@@ -126,6 +126,7 @@ files:
126
126
  - spec/fake_app/models/config.rb
127
127
  - spec/fake_app/rails_app.rb
128
128
  - spec/lib/base_spec.rb
129
+ - spec/lib/column_spec.rb
129
130
  - spec/lib/generators/config_generator_spec.rb
130
131
  - spec/requests/products_spec.rb
131
132
  - spec/spec_helper.rb
@@ -163,6 +164,7 @@ test_files:
163
164
  - spec/fake_app/models/config.rb
164
165
  - spec/fake_app/rails_app.rb
165
166
  - spec/lib/base_spec.rb
167
+ - spec/lib/column_spec.rb
166
168
  - spec/lib/generators/config_generator_spec.rb
167
169
  - spec/requests/products_spec.rb
168
170
  - spec/spec_helper.rb