Skip to content

Commit add0129

Browse files
committed
Add minimum specs
1 parent 471fb53 commit add0129

File tree

6 files changed

+175
-3
lines changed

6 files changed

+175
-3
lines changed

spec/dummy/app/admin/authors.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
# frozen_string_literal: true
22

33
ActiveAdmin.register Author do
4-
menu priority: 1
5-
64
permit_params :name, :email, :age, :avatar, profile_attributes: %i[id description _destroy]
75

6+
member_action :dialog do
7+
record = resource
8+
context = Arbre::Context.new do
9+
dl do
10+
%i[name age created_at].each do |field|
11+
dt Author.human_attribute_name(field) + ':'
12+
dd record[field]
13+
end
14+
end
15+
end
16+
render plain: context
17+
end
18+
819
index do
920
selectable_column
1021
id_column

spec/dummy/app/admin/posts.rb

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11
# frozen_string_literal: true
22

3-
ActiveAdmin.register Post do
3+
ActiveAdmin.register Post do # rubocop:disable Metrics/BlockLength
4+
permit_params :author_id, :title, :description, :category, :dt, :position, :published, tag_ids: []
5+
6+
member_action :save, method: [:post] do
7+
render ActiveAdmin::DynamicFields.update(resource, params)
8+
end
9+
10+
index do
11+
selectable_column
12+
id_column
13+
column :title
14+
column :author
15+
column :published do |row|
16+
status_tag row.published, ActiveAdmin::DynamicFields.edit_boolean(:published, save_admin_post_path(row.id), row.published)
17+
end
18+
column :created_at
19+
actions
20+
end
21+
22+
show do |record|
23+
attributes_table do
24+
row :author do
25+
link_to record.author.name, dialog_admin_author_path(record.author), title: record.author.name, 'data-df-dialog': true, 'data-df-icon': true
26+
end
27+
row :title
28+
row :description
29+
row :category
30+
row :dt
31+
row :position
32+
row :published
33+
row :tags
34+
row :created_at
35+
row :updated_at
36+
end
37+
active_admin_comments
38+
end
39+
40+
form do |f|
41+
f.inputs 'Post' do
42+
f.input :author
43+
f.input :title
44+
f.input :description, input_html: { data: { if: 'blank', action: 'setValue no title', target: '#post_category' } }
45+
f.input :category
46+
f.input :published, input_html: { data: { if: 'not_checked', action: 'hide', target: '.group1' } }
47+
f.input :dt, wrapper_html: { class: 'group1' }
48+
f.input :position, wrapper_html: { class: 'group1' }
49+
end
50+
51+
f.inputs 'Tags' do
52+
f.input :tags
53+
end
54+
55+
f.actions
56+
end
457
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
//= require active_admin/base
2+
3+
//= require activeadmin/dynamic_fields

spec/system/dialog_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'Dialog', type: :system do
4+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
5+
let(:post) { Post.create!(title: 'Test', author: author, description: '') }
6+
7+
before do
8+
post
9+
end
10+
11+
after do
12+
post.destroy
13+
author.destroy
14+
end
15+
16+
context 'with a dialog' do
17+
subject(:author_link) { '.attributes_table .row-author a[data-df-dialog]' }
18+
19+
it 'opens the dialog' do
20+
visit "/admin/posts/#{post.id}"
21+
22+
expect(page).to have_css(author_link)
23+
expect(page).not_to have_css('.ui-dialog')
24+
find(author_link).click
25+
expect(page).to have_css('.ui-dialog', visible: :visible)
26+
expect(page).to have_css('#df-dialog dd', text: author.name)
27+
expect(page).to have_css('#df-dialog dd', text: author.age)
28+
end
29+
end
30+
end

spec/system/dynamic_fields_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'Dynamic fields', type: :system do
4+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
5+
let(:post) { Post.create!(title: 'Test', author: author, description: '') }
6+
7+
before do
8+
post
9+
end
10+
11+
after do
12+
post.destroy
13+
author.destroy
14+
end
15+
16+
context 'with some dynamic fields' do
17+
it 'toggles the .group1 elements when clicking on the checkbox' do
18+
visit "/admin/posts/#{post.id}/edit"
19+
20+
expect(page).to have_css('#post_published[data-if="not_checked"][data-action="hide"][data-target=".group1"]')
21+
22+
expect(find('#post_published')).not_to be_checked
23+
expect(page).to have_css('#post_dt_input', visible: :hidden)
24+
expect(page).to have_css('#post_position_input', visible: :hidden)
25+
26+
find('#post_published').set(true)
27+
expect(page).to have_css('#post_dt_input', visible: :visible)
28+
expect(page).to have_css('#post_position_input', visible: :visible)
29+
30+
find('#post_published').set(false)
31+
expect(page).to have_css('#post_dt_input', visible: :hidden)
32+
expect(page).to have_css('#post_position_input', visible: :hidden)
33+
end
34+
35+
it 'changes the value of target when the source element is blank' do
36+
visit "/admin/posts/#{post.id}/edit"
37+
38+
expect(page).to have_css('#post_description[data-if="blank"][data-action="setValue no title"][data-target="#post_category"]')
39+
expect(find('#post_category').value).to eq 'no title'
40+
find('#post_category').set('...')
41+
find('#post_description').set('...')
42+
expect(find('#post_category').value).to eq '...'
43+
find('#post_description').set('')
44+
expect(find('#post_category').value).to eq 'no title'
45+
end
46+
end
47+
end

spec/system/inline_editing_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'Inline editing', type: :system do
4+
let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) }
5+
let(:post) { Post.create!(title: 'Test', author: author, description: '') }
6+
7+
before do
8+
post
9+
end
10+
11+
after do
12+
post.destroy
13+
author.destroy
14+
end
15+
16+
context 'with a column set for inline editing' do
17+
let(:editing_widget) { '.index_content .status_tag[data-field="published"][data-field-type="boolean"]' }
18+
19+
it 'includes the editing widget' do
20+
visit "/admin/posts"
21+
22+
expect(post.reload.published).to be_falsey
23+
expect(page).to have_css(editing_widget, text: 'NO')
24+
find(editing_widget).click
25+
expect(page).to have_css(editing_widget, text: 'YES')
26+
expect(post.reload.published).to be_truthy
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)