table_cloth 0.3.0.beta3 → 0.3.1.alpha1

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.
data/lib/table_cloth.rb CHANGED
@@ -14,6 +14,7 @@ module TableCloth
14
14
 
15
15
  module Presenters
16
16
  autoload :Default, "table_cloth/presenters/default"
17
+ autoload :Sortable, "table_cloth/presenters/sortable"
17
18
  end
18
19
 
19
20
  module Extensions
@@ -15,8 +15,12 @@ module TableCloth
15
15
  end
16
16
  end
17
17
 
18
- def human_name
19
- options[:label] || name.to_s.humanize
18
+ def human_name(view)
19
+ if options[:label].kind_of? Proc
20
+ view.instance_exec(&options[:label])
21
+ else
22
+ options[:label] || name.to_s.humanize
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -12,7 +12,7 @@ module TableCloth
12
12
 
13
13
  class << self
14
14
  def configure(&block)
15
- block.arity > 0 ? block.call(self) : yield
15
+ yield TableCloth.config
16
16
  end
17
17
  end
18
18
 
@@ -10,10 +10,6 @@ module TableCloth
10
10
  @table = table_definition.new(objects, view)
11
11
  end
12
12
 
13
- def v
14
- view_context
15
- end
16
-
17
13
  def render_table
18
14
  raise NoMethodError, "You must override the .render method"
19
15
  end
@@ -40,7 +36,7 @@ module TableCloth
40
36
 
41
37
  def column_names
42
38
  @column_names ||= columns.each_with_object([]) do |column, names|
43
- names << column.human_name
39
+ names << column.human_name(view_context)
44
40
  end
45
41
  end
46
42
 
@@ -70,10 +66,20 @@ module TableCloth
70
66
 
71
67
  def tag_options(type, options={})
72
68
  options = options.dup
73
- options.merge!(table.config.config_for(type))
74
- options.merge!(TableCloth.config.config_for(type))
69
+ if TableCloth.config.respond_to?(type)
70
+ options.merge!(table.config.config_for(type))
71
+ options.merge!(TableCloth.config.config_for(type))
72
+ end
75
73
 
76
74
  options
77
75
  end
76
+
77
+ def v
78
+ view_context
79
+ end
80
+
81
+ def params
82
+ v.params
83
+ end
78
84
  end
79
85
  end
@@ -0,0 +1,49 @@
1
+ module TableCloth
2
+ module Presenters
3
+ class Sortable < Default
4
+ def render_header
5
+ wrapper_tag :thead do
6
+ wrapper_tag :tr, v.raw(row_headers.join("\n"))
7
+ end
8
+ end
9
+
10
+ def render_sortable(column)
11
+ query_string = sort_query(sort_params_for(column))
12
+ wrapper_tag(:a, column.human_name, href: "?#{query_string}")
13
+ end
14
+
15
+ def row_headers
16
+ columns.map do |column|
17
+ if !!column.options[:sortable]
18
+ wrapper_tag :th, render_sortable(column), class: "sortable-column"
19
+ else
20
+ column.human_name
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def sort_params_for(column)
28
+ direction = params[:direction]
29
+ new_direction = (direction == "asc") ? "desc" : "asc"
30
+
31
+ params.update({sort_by: column.name.to_s, direction: new_direction})
32
+ end
33
+
34
+ def sort_query(params)
35
+ params.stringify_keys.map{|k,v| "#{CGI.escape(k)}=#{CGI.escape(v)}" }.join("&")
36
+ end
37
+
38
+ def header_values
39
+ columns.map do |column|
40
+ if !!column.options[:sortable]
41
+ render_sortable(column)
42
+ else
43
+ column.human_name
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module TableCloth
2
- VERSION = "0.3.0.beta3"
2
+ VERSION = "0.3.1.alpha1"
3
3
  end
@@ -25,12 +25,17 @@ describe TableCloth::Column do
25
25
  context "human name" do
26
26
  it "returns the label when set" do
27
27
  column = FactoryGirl.build(:column, options: { label: "Whatever" })
28
- expect(column.human_name).to eq("Whatever")
28
+ expect(column.human_name(view_context)).to eq("Whatever")
29
29
  end
30
30
 
31
31
  it "humanizes the symbol if no label is set" do
32
32
  column = FactoryGirl.build(:column, name: :email)
33
- expect(column.human_name).to eq("Email")
33
+ expect(column.human_name(view_context)).to eq("Email")
34
+ end
35
+
36
+ it "runs with a proc" do
37
+ column = FactoryGirl.build(:column, options: { label: Proc.new{ tag :span }} )
38
+ expect(column.human_name(view_context)).to eq("<span />")
34
39
  end
35
40
  end
36
41
  end
@@ -9,4 +9,10 @@ describe TableCloth::Configuration do
9
9
  expect(subject).to respond_to option
10
10
  end
11
11
  end
12
+
13
+ specify ".configure returns the TableCloth global config" do
14
+ config = double("config")
15
+ TableCloth.stub config: config
16
+ expect {|b| described_class.configure(&b) }.to yield_with_args(config)
17
+ end
12
18
  end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe TableCloth::Presenters::Sortable do
4
+ let(:dummy_table) { FactoryGirl.build(:dummy_table, name: {sortable: true} ) }
5
+ let(:dummy_model) { FactoryGirl.build(:dummy_model) }
6
+ let(:objects) { FactoryGirl.build_list(:dummy_model, 3) }
7
+ let(:view_context) { ActionView::Base.new }
8
+ subject { TableCloth::Presenters::Sortable.new(objects, dummy_table, view_context) }
9
+ let(:params) { {sort_by: "", direction: "asc"} }
10
+ let(:column) { subject.columns.first }
11
+
12
+ before do
13
+ dummy_table.presenter(described_class)
14
+ subject.stub params: params
15
+ column.options[:sortable] = true
16
+ end
17
+
18
+ it "inherits from default" do
19
+ expect(described_class).to be < TableCloth::Presenters::Default
20
+ end
21
+
22
+ context ".render_sortable" do
23
+ before do
24
+ column.stub human_name: "Header"
25
+ end
26
+
27
+ it "renders an a tag" do
28
+ element = to_element(subject.render_sortable(column), "a")
29
+ expect(element.node_name).to eq("a")
30
+ end
31
+
32
+ it "renders an a tag with href direction keys" do
33
+ params[:direction] = "desc"
34
+ element = to_element(subject.render_sortable(column), "a")
35
+ query_string = element[:href]
36
+
37
+ expect(query_string).to include "direction=asc"
38
+ end
39
+
40
+ it "renders an a tag with href sort by keys" do
41
+ params[:sort_by] = "desc"
42
+ element = to_element(subject.render_sortable(column), "a")
43
+ query_string = element[:href]
44
+
45
+ expect(query_string).to include "sort_by=#{column.name}"
46
+ end
47
+ end
48
+
49
+ context "thead" do
50
+ it "includes a css class for a sortable column" do
51
+ header = to_element(subject.render_header, "thead")
52
+ expect(header.at_css("th.sortable-column")).to be_present
53
+ end
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_cloth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.beta3
4
+ version: 0.3.1.alpha1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-04 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -203,6 +203,7 @@ files:
203
203
  - lib/table_cloth/extensions/actions/jury.rb
204
204
  - lib/table_cloth/presenter.rb
205
205
  - lib/table_cloth/presenters/default.rb
206
+ - lib/table_cloth/presenters/sortable.rb
206
207
  - lib/table_cloth/version.rb
207
208
  - spec/factories/actions.rb
208
209
  - spec/factories/columns.rb
@@ -221,6 +222,7 @@ files:
221
222
  - spec/lib/extensions/actions_spec.rb
222
223
  - spec/lib/presenter_spec.rb
223
224
  - spec/lib/presenters/default_spec.rb
225
+ - spec/lib/presenters/sortable_spec.rb
224
226
  - spec/spec_helper.rb
225
227
  - spec/support/dummy_model.rb
226
228
  - spec/support/dummy_table.rb
@@ -273,6 +275,7 @@ test_files:
273
275
  - spec/lib/extensions/actions_spec.rb
274
276
  - spec/lib/presenter_spec.rb
275
277
  - spec/lib/presenters/default_spec.rb
278
+ - spec/lib/presenters/sortable_spec.rb
276
279
  - spec/spec_helper.rb
277
280
  - spec/support/dummy_model.rb
278
281
  - spec/support/dummy_table.rb