Skip to content
Browse files

refactoring: rewrite the table extensions to extend the row

  • Loading branch information...
1 parent a0cd3cc commit e62396c40a200c873fe108143c8d18506e6de6e8 @senny committed
View
7 lib/corner_stones/table.rb
@@ -26,11 +26,16 @@ def row(options)
def rows
within @scope do
all('tbody tr').map do |row|
- row = Row.new(row, attributes_for_row(row))
+ build_row(row)
end
end
end
+ def build_row(node)
+ Row.new(node, attributes_for_row(node))
+ end
+ protected :build_row
+
def headers
@options[:headers] || detect_table_headers
end
View
25 lib/corner_stones/table/deletable_rows.rb
@@ -3,16 +3,25 @@ class Table
module DeletableRows
def delete_row(options)
- row = row(options)
- if row['Delete-Link']
- row['Delete-Link'].click
- else
- raise "The row matching '#{options}' does not have a delete-link"
- end
+ warn "[DEPRECATION] `delete_row` is deprecated. Please use `row(row_spec).delete` instead."
+ row(options).delete
+ end
+
+ def build_row(node)
+ row = super
+ row.extend RowMethods
+ row
end
- def attributes_for_row(row)
- super.merge('Delete-Link' => row.first('td .delete-action'))
+ module RowMethods
+ def delete
+ delete_link = node.first('td .delete-action')
+ if delete_link
+ delete_link.click
+ else
+ raise "The row '#{attributes}' does not have a delete-link"
+ end
+ end
end
end
View
15 lib/corner_stones/table/selectable_rows.rb
@@ -3,11 +3,20 @@ class Table
module SelectableRows
def select_row(options)
- visit row(options)['Selected-Link']
+ warn "[DEPRECATION] `select_row` is deprecated. Please use `row(row_spec).select` instead."
+ row(options).select
end
- def attributes_for_row(row)
- super.merge('Selected-Link' => row['data-selected-url'])
+ def build_row(node)
+ row = super
+ row.extend RowMethods
+ row
+ end
+
+ module RowMethods
+ def select
+ visit node['data-selected-url']
+ end
end
end
View
46 spec/integration/corner_stones/table_spec.rb
@@ -179,24 +179,31 @@
subject.extend(CornerStones::Table::DeletableRows)
end
- it 'it includes the "Delete-Link" object in the data' do
- subject.rows.each do |row|
- unless row['Delete-Link'].nil?
- row['Delete-Link'].must_be_kind_of(Capybara::Node::Element)
- end
+ it 'allows you to trigger a deletion with a row selector' do
+ subject.row('Title' => 'Domain Driven Design').delete
+ current_path.must_equal '/delete/domain_driven_design'
+ end
+
+ it 'raises an error when a the target row can not be found' do
+ begin
+ subject.row('ID' => '3').delete
+ rescue => e
+ e.message.must_match /^The row '.*' does not have a delete-link$/
end
end
- it 'allows you to trigger a deletion with a row selector' do
+ it 'the deprecated method #delete_row still works' do
+ errors = StringIO.new
+ original_stderr = $stderr
+ $stderr = errors
+
subject.delete_row('Title' => 'Domain Driven Design')
+
+ $stderr = original_stderr
current_path.must_equal '/delete/domain_driven_design'
+ errors.string.must_equal "[DEPRECATION] `delete_row` is deprecated. Please use `row(row_spec).delete` instead.\n"
end
- it 'raises an error when a the target row can not be found' do
- lambda do
- subject.delete_row('ID' => '3')
- end.must_raise(RuntimeError)
- end
end
describe 'selectable rows' do
@@ -229,15 +236,21 @@
subject.extend(CornerStones::Table::SelectableRows)
end
- it 'it includes the "Selected-Link" object in the data' do
- subject.rows.map do |row|
- row['Selected-Link']
- end.must_equal ['/articles/clean_code', '/articles/domain_driven_design']
+ it 'allows you to select a row' do
+ subject.row('ID' => '1').select
+ current_path.must_equal '/articles/clean_code'
end
- it 'allows you to select a row' do
+ it 'the deprecated method #select_row still works' do
+ errors = StringIO.new
+ original_stderr = $stderr
+ $stderr = errors
+
subject.select_row('ID' => '1')
+
+ $stderr = original_stderr
current_path.must_equal '/articles/clean_code'
+ errors.string.must_equal "[DEPRECATION] `select_row` is deprecated. Please use `row(row_spec).select` instead.\n"
end
end
@@ -271,7 +284,6 @@
it 'strips whitespace from cell content' do
subject.rows.map { |r| r['Author']}.must_equal ["Robert C. Martin", "Eric Evans"]
end
-
end
end
end

0 comments on commit e62396c

Please sign in to comment.
Something went wrong with that request. Please try again.