Skip to content

Commit 817196f

Browse files
committed
improve batch actions
1 parent f5f9fe9 commit 817196f

File tree

9 files changed

+87
-17
lines changed

9 files changed

+87
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- `batch_action_selector`, `click_batch_action` finds element by link text
10+
11+
### Added
12+
- `select_table_row`, `open_batch_action_menu` actions
13+
- `have_batch_action` matcher
14+
- tests for batch actions
815

916
## [0.3.2] - 2020-04-16
1017
### Changed

lib/capybara/active_admin/actions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
module Capybara
99
module ActiveAdmin
1010
module Actions
11-
# Actions are interactions with page that change something.
12-
# Click, scroll, fill, clear, switch - all these are interactions.
11+
# Actions are interactions with page that change something (click button, fill field, etc).
12+
# Good method names starts with *click_*, *scroll_*, *fill_*, *clear_*, *switch_*, *open_*.
1313

1414
include Actions::Layout
1515
include Actions::Table

lib/capybara/active_admin/actions/layout.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ def switch_tab(tab_name, options = {})
1616
find(tab_header_link_selector, opts).click
1717
end
1818

19-
def click_batch_action(title)
20-
find(batch_actions_button_selector).click
19+
def click_batch_action(title, exact: true)
20+
open_batch_action_menu
2121
within(dropdown_list_selector) do
22-
selector = batch_action_selector(title)
23-
find(selector).click
22+
selector = batch_action_selector
23+
opts = Util.options_with_text(title, exact: exact)
24+
find(selector, opts).click
2425
end
2526
end
2627

28+
def open_batch_action_menu
29+
return if find_all(dropdown_list_selector).present?
30+
31+
find(batch_actions_button_selector).click
32+
end
33+
2734
def confirm_modal_dialog
2835
within_modal_dialog { click_button 'OK' }
2936
end

lib/capybara/active_admin/actions/table.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ module Capybara
44
module ActiveAdmin
55
module Actions
66
module Table
7+
def select_table_row(id: nil, index: nil)
8+
raise ArgumentError, "can't use both :id and :index" if id && index
9+
raise ArgumentError, 'must provide :id or :index' if id.nil? && index.nil?
10+
11+
if id
12+
find("input#batch_action_item_#{id}").click
13+
return
14+
end
15+
16+
selector = %(input[id^="batch_action_item_"])
17+
find_all(selector, minimum: index + 1)[index].click
18+
end
719
end
820
end
921
end

lib/capybara/active_admin/matchers/layout.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def have_sidebar(title, options = {})
3838
opts = Util.options_with_text(title, options)
3939
have_selector(title_selector, opts)
4040
end
41+
42+
def have_batch_action(title, exact: true)
43+
selector = "#{dropdown_list_selector} #{batch_action_selector}"
44+
opts = Util.options_with_text(title, exact: exact)
45+
have_selector(selector, opts)
46+
end
4147
end
4248
end
4349
end

lib/capybara/active_admin/selectors/layout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def dropdown_list_selector
7373
end
7474

7575
# @return [String] selector.
76-
def batch_action_selector(title)
77-
"li a[data-action='#{title}']"
76+
def batch_action_selector
77+
'li a[data-action]'
7878
end
7979

8080
# @return [String] selector.

spec/dummy/test_application.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,12 @@ def common?
122122
# it allover the place
123123
config.authentication_method = false
124124
config.current_user_method = false
125+
config.batch_actions = true
125126
end
126127

127128
Rails.application.initialize!
129+
# to execute prepare hooks
130+
ActiveSupport::Reloader.prepare!
128131

129132
ActiveAdmin.register_page 'Dashboard' do
130133
menu priority: 1, label: proc { I18n.t('active_admin.dashboard') }
@@ -141,9 +144,16 @@ def common?
141144
permit_params :full_name, :salary, duties_attributes: [:id, :name, :duty_type]
142145
includes :duties
143146

147+
batch_action :update_salary, form: -> { { salary: :text } } do |ids, inputs|
148+
batch_action_collection.where(id: ids).each { |r| r.update!(salary: inputs['salary']) }
149+
flash[:notice] = 'Salary was updated successfully'
150+
redirect_back(fallback_location: admin_root_path)
151+
end
152+
144153
index do
145154
actions
146155
id_column
156+
selectable_column
147157
column :full_name
148158
column(:salary) { |r| number_to_currency(r.salary) }
149159
column(:common_duties) { |r| r.duties.select(&:common?).map(&:name).join(', ') }

spec/system/business_employees_index_spec.rb

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
expect(page).to have_table(resource_name: 'Business Employees')
3232
within_table_for do
3333
expect(page).to have_table_row(count: 2)
34-
# 2x id, full_name, salary, common_duties, extra_duties, created_at, updated_at, actions
35-
expect(page).to have_table_cell(count: 16)
34+
# 2x9 (id, selectable, full_name, salary, common_duties, extra_duties, created_at, updated_at, actions)
35+
expect(page).to have_table_cell(count: 18)
3636

3737
expect(page).to have_table_cell(text: 'John Doe')
3838
expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name')
3939
expect(page).to_not have_table_cell(text: 'John Doe', column: 'Id')
4040

4141
within_table_row(id: john.id) do
42-
expect(page).to have_table_cell(count: 8)
42+
expect(page).to have_table_cell(count: 9)
4343
expect(page).to have_table_cell(text: 'John Doe')
4444
expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name')
4545
expect(page).to have_table_cell(text: '$100.00', column: 'Salary')
@@ -49,7 +49,7 @@
4949
end
5050

5151
within_table_row(id: jane.id) do
52-
expect(page).to have_table_cell(count: 8)
52+
expect(page).to have_table_cell(count: 9)
5353
expect(page).to have_table_cell(text: jane.id, column: 'Id')
5454
expect(page).to have_table_cell(text: 'Jane Air', column: 'Full Name')
5555
expect(page).to have_table_cell(exact_text: '$101.00', column: 'Salary')
@@ -59,6 +59,33 @@
5959
expect(page).to_not have_table_cell(text: 'John Doe', column: 'Full Name')
6060
end
6161
end
62-
# take_screenshot
62+
end
63+
64+
it 'updates salary via batch action' do
65+
john = Billing::Employee.create!(full_name: 'John Doe', salary: 100)
66+
jane = Billing::Employee.create!(full_name: 'Jane Air', salary: 101)
67+
5.times { |i| Billing::Employee.create!(full_name: "Jack #{i}", salary: 10) }
68+
69+
subject
70+
71+
within_table_for do
72+
select_table_row(id: john.id)
73+
select_table_row(id: jane.id)
74+
end
75+
76+
open_batch_action_menu
77+
expect(page).to have_batch_action('Update Salary Selected', exact: true)
78+
expect(page).to have_batch_action('Delete Selected', exact: true)
79+
80+
click_batch_action('Update Salary Selected', exact: true)
81+
within_modal_dialog do
82+
fill_in 'salary', with: '200.35'
83+
end
84+
confirm_modal_dialog
85+
86+
expect(page).to have_flash_message('Salary was updated successfully', exact: true)
87+
88+
expect(john.reload.salary).to eq 200.35
89+
expect(jane.reload.salary).to eq 200.35
6390
end
6491
end

spec/system/users_index_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,29 @@
3131
expect(page).to have_table(resource_name: 'Users')
3232
within_table_for(User) do
3333
expect(page).to have_table_row(count: 2)
34-
expect(page).to have_table_cell(count: 10) # 2x id, full_name, created_at, updated_at, actions
34+
# 2x6 (selectable, id, full_name, created_at, updated_at, actions)
35+
expect(page).to have_table_cell(count: 12)
3536

3637
expect(page).to have_table_cell(text: 'John Doe')
3738
expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name')
3839
expect(page).to_not have_table_cell(text: 'John Doe', column: 'Id')
3940

4041
within_table_row(id: john.id) do
41-
expect(page).to have_table_cell(count: 5) # id, full_name, created_at, updated_at, actions
42+
# selectable, id, full_name, created_at, updated_at, actions
43+
expect(page).to have_table_cell(count: 6)
4244
expect(page).to have_table_cell(text: 'John Doe')
4345
expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name')
4446
expect(page).to_not have_table_cell(text: 'John Doe', column: 'Id')
4547
end
4648

4749
within_table_row(id: jane.id) do
48-
expect(page).to have_table_cell(count: 5) # id, full_name, created_at, updated_at, actions
50+
expect(page).to have_table_cell(count: 6)
4951
expect(page).to have_table_cell(text: jane.id, column: 'Id')
5052
expect(page).to have_table_cell(text: 'Jane Air', column: 'Full Name')
5153
expect(page).to_not have_table_cell(text: 'John Doe')
5254
expect(page).to_not have_table_cell(text: 'John Doe', column: 'Full Name')
5355
end
5456
end
55-
# take_screenshot
5657
end
5758

5859
# TODO: filters expect to have

0 commit comments

Comments
 (0)