table_cloth 0.3.0.beta2 → 0.3.0.beta3

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.
@@ -51,7 +51,7 @@ module TableCloth
51
51
  end
52
52
 
53
53
  def config
54
- @config ||= TableCloth::Configuration.new
54
+ @config ||= Configuration.new
55
55
  end
56
56
  end
57
57
  end
@@ -1,20 +1,24 @@
1
1
  module TableCloth
2
2
  class Configuration
3
- include ConfigurableElements
3
+ OPTIONS = %w(table thead th tbody tr td).map(&:to_sym)
4
+
5
+ OPTIONS.each do |option|
6
+ class_eval <<-OPTION, __FILE__, __LINE__+1
7
+ def #{option}
8
+ @#{option}_option ||= ActiveSupport::OrderedOptions.new
9
+ end
10
+ OPTION
11
+ end
4
12
 
5
13
  class << self
6
14
  def configure(&block)
7
15
  block.arity > 0 ? block.call(self) : yield
8
16
  end
9
-
10
- def config_for(type)
11
- self.send(type).to_hash
12
- end
13
- alias [] config_for
14
17
  end
15
18
 
16
- def [](key)
17
- self.send(key)
19
+ def config_for(type)
20
+ send(type).to_hash
18
21
  end
22
+ alias [] config_for
19
23
  end
20
24
  end
@@ -70,8 +70,10 @@ module TableCloth
70
70
 
71
71
  def tag_options(type, options={})
72
72
  options = options.dup
73
- options.merge!(table.config[type])
74
- options.merge!(TableCloth.config_for(type))
73
+ options.merge!(table.config.config_for(type))
74
+ options.merge!(TableCloth.config.config_for(type))
75
+
76
+ options
75
77
  end
76
78
  end
77
79
  end
@@ -1,3 +1,3 @@
1
1
  module TableCloth
2
- VERSION = "0.3.0.beta2"
2
+ VERSION = "0.3.0.beta3"
3
3
  end
data/lib/table_cloth.rb CHANGED
@@ -2,9 +2,9 @@ require "action_view"
2
2
  require "active_support/core_ext/class"
3
3
  require "table_cloth/version"
4
4
  require "table_cloth/configurable_elements"
5
- require "table_cloth/base"
6
5
 
7
6
  module TableCloth
7
+ autoload :Base, "table_cloth/base"
8
8
  autoload :Configuration, "table_cloth/configuration"
9
9
  autoload :Builder, "table_cloth/builder"
10
10
  autoload :Column, "table_cloth/column"
@@ -20,10 +20,11 @@ module TableCloth
20
20
  autoload :Actions, "table_cloth/extensions/actions"
21
21
  end
22
22
 
23
- def config_for(type)
24
- Configuration.config_for(type).dup
23
+ class << self
24
+ def config
25
+ @config ||= Configuration.new
26
+ end
25
27
  end
26
- module_function :config_for
27
28
  end
28
29
 
29
30
  TableCloth::Base.presenter ::TableCloth::Presenters::Default
@@ -1,23 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe TableCloth::Configuration do
4
- subject { Class.new(TableCloth::Configuration) }
4
+ subject { TableCloth::Configuration.new }
5
+ let(:options) { TableCloth::Configuration::OPTIONS }
5
6
 
6
- context "options" do
7
- TableCloth::Configuration::OPTIONS.each do |option|
8
- it "configures #{option} options" do
9
- subject.send(option).classes = "option_value"
10
- expect(subject.send(option).classes).to eq("option_value")
11
- end
12
- end
13
- end
14
-
15
- context "[] accessor" do
16
- let(:option) { TableCloth::Configuration::OPTIONS.first }
17
-
18
- it "returns the option" do
19
- subject.send(option).classes = "something"
20
- expect(subject.new[option].classes).to eq("something")
7
+ it "has accessors for all options" do
8
+ options.each do |option|
9
+ expect(subject).to respond_to option
21
10
  end
22
11
  end
23
12
  end
@@ -57,17 +57,46 @@ describe TableCloth::Presenter do
57
57
  expect(subject.rows).to eq(expected)
58
58
  end
59
59
 
60
- context 'tags' do
61
- it '.wrapper_tag includes config for a tag in block form' do
62
- TableCloth::Configuration.table.should_receive(:to_hash).once.and_return(class: "stuff")
63
- table = subject.wrapper_tag(:table) { "Hello "}
64
- Nokogiri::HTML(table).at_xpath('//table')[:class].should == 'stuff'
60
+ context '.wrapper_tag' do
61
+ it "creates a tag with a block" do
62
+ table = subject.wrapper_tag(:table) { "Hello" }
63
+ element = to_element(table, "table")
64
+ expect(element).to be_present
65
+ expect(element.text).to eq("Hello")
65
66
  end
66
67
 
67
- it '.wrapper_tag includes config for a tag without a block' do
68
- TableCloth::Configuration.table.should_receive(:to_hash).once.and_return(class: "stuff")
69
- table = subject.wrapper_tag(:table, 'Hello')
70
- Nokogiri::HTML(table).at_xpath('//table')[:class].should == 'stuff'
68
+ it "creates a tag without a block" do
69
+ table = subject.wrapper_tag(:table, "Hello")
70
+ element = to_element(table, "table")
71
+ expect(element).to be_present
72
+ expect(element.text).to eq("Hello")
73
+ end
74
+
75
+ context "config" do
76
+ let(:config) { double("config", config_for: { class: "table_class" }) }
77
+
78
+ it "inherits options from global config" do
79
+ TableCloth.config.stub config_for: {class: "global_class"}
80
+ tag = subject.wrapper_tag(:table, "Hello")
81
+ element = to_element(tag, "table")
82
+
83
+ expect(element[:class]).to eq("global_class")
84
+ end
85
+
86
+ it "includes config sent to it" do
87
+ tag = subject.wrapper_tag(:table, "Hello", {class: "passed_class"})
88
+ element = to_element(tag, "table")
89
+
90
+ expect(element[:class]).to eq("passed_class")
91
+ end
92
+
93
+ it "includes config from the table instance" do
94
+ dummy_table.stub config: config
95
+ tag = subject.wrapper_tag(:table, "Hello", {class: "passed_class"})
96
+ element = to_element(tag, "table")
97
+
98
+ expect(element[:class]).to eq("table_class")
99
+ end
71
100
  end
72
101
  end
73
102
  end
@@ -60,41 +60,6 @@ describe TableCloth::Presenters::Default do
60
60
  end
61
61
  end
62
62
 
63
- context 'configuration' do
64
- let(:doc) { Nokogiri::HTML(subject.render_table) }
65
-
66
- it 'tables have a class attached' do
67
- TableCloth.stub config_for: {class: "table"}
68
- doc.at_xpath('//table')[:class].should include 'table'
69
- end
70
-
71
- it 'thead has a class attached' do
72
- TableCloth.stub config_for: {class: "thead"}
73
- doc.at_xpath('//thead')[:class].should include 'thead'
74
- end
75
-
76
- it 'th has a class attached' do
77
- TableCloth.stub config_for: {class: "th"}
78
- doc.at_xpath('//th')[:class].should include 'th'
79
- end
80
-
81
- it 'tbody has a class attached' do
82
- TableCloth.stub config_for: {class: "tbody"}
83
- doc.at_xpath('//tbody')[:class].should include 'tbody'
84
- end
85
-
86
- it 'tr has a class attached' do
87
- TableCloth.stub config_for: {class: "tr"}
88
- doc.at_xpath('//tr')[:class].should include 'tr'
89
- end
90
-
91
- it 'td has a class attached' do
92
- TableCloth.stub config_for: {class: "td"}
93
- doc.at_xpath('//td')[:class].should include 'td'
94
- end
95
-
96
- end
97
-
98
63
  context "column configuration" do
99
64
  let(:column) { FactoryGirl.build(:column, options: {td_options: {class: "email_column"}}) }
100
65
  let(:model) { FactoryGirl.build(:dummy_model) }
@@ -117,43 +82,4 @@ describe TableCloth::Presenters::Default do
117
82
  end
118
83
  end
119
84
  end
120
-
121
- context "table configuration" do
122
- let(:doc) { Nokogiri::HTML(subject.render_table) }
123
- let(:dummy_table) do
124
- Class.new(TableCloth::Base) do
125
- column :email
126
-
127
- config.table.class = "table2"
128
- config.thead.class = "thead2"
129
- config.th.class = "th2"
130
- config.tbody.class = "tbody2"
131
- config.tr.class = "tr2"
132
- config.td.class = "td2"
133
- end
134
- end
135
-
136
- include_examples "table configuration"
137
-
138
- context "is extendable" do
139
- let(:parent_table) do
140
- Class.new(TableCloth::Base) do
141
- column :email
142
-
143
- config.table.class = "table2"
144
- config.thead.class = "thead2"
145
- config.th.class = "th2"
146
- config.tbody.class = "tbody2"
147
- config.tr.class = "tr2"
148
- config.td.class = "td2"
149
- end
150
- end
151
-
152
- let(:dummy_table) do
153
- Class.new(parent_table)
154
- end
155
-
156
- include_examples "table configuration"
157
- end
158
- end
159
85
  end
data/spec/spec_helper.rb CHANGED
@@ -21,4 +21,5 @@ RSpec.configure do |config|
21
21
  config.treat_symbols_as_metadata_keys_with_true_values = true
22
22
  config.run_all_when_everything_filtered = true
23
23
  config.filter_run :focus
24
+ config.include ElementHelpers
24
25
  end
@@ -0,0 +1,5 @@
1
+ module ElementHelpers
2
+ def to_element(string, type)
3
+ Nokogiri::HTML(string).at_xpath(".//#{type}")
4
+ end
5
+ 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.beta2
4
+ version: 0.3.0.beta3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -225,6 +225,7 @@ files:
225
225
  - spec/support/dummy_model.rb
226
226
  - spec/support/dummy_table.rb
227
227
  - spec/support/dummy_table_with_value_options.rb
228
+ - spec/support/element_helpers.rb
228
229
  - spec/support/matchers/element_matchers.rb
229
230
  - spec/support/shared_config_examples.rb
230
231
  - spec/support/view_mocks.rb
@@ -276,6 +277,7 @@ test_files:
276
277
  - spec/support/dummy_model.rb
277
278
  - spec/support/dummy_table.rb
278
279
  - spec/support/dummy_table_with_value_options.rb
280
+ - spec/support/element_helpers.rb
279
281
  - spec/support/matchers/element_matchers.rb
280
282
  - spec/support/shared_config_examples.rb
281
283
  - spec/support/view_mocks.rb