diff --git a/Rakefile b/Rakefile index 7398a90..e318f19 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,13 @@ # frozen_string_literal: true require 'bundler/gem_tasks' + +begin + require 'rspec/core/rake_task' + + RSpec::Core::RakeTask.new(:spec) + + task default: :spec +rescue LoadError + puts '! LoadError: no RSpec available' +end diff --git a/spec/dummy/app/admin/authors.rb b/spec/dummy/app/admin/authors.rb index 1839574..2af9bce 100644 --- a/spec/dummy/app/admin/authors.rb +++ b/spec/dummy/app/admin/authors.rb @@ -1,9 +1,12 @@ # frozen_string_literal: true ActiveAdmin.register Author do - menu priority: 1 - - permit_params :name, :email, :age, :avatar, profile_attributes: %i[id description _destroy] + permit_params :name, + :email, + :age, + :avatar, + profile_attributes: %i[id description _destroy], + posts_attributes: %i[id title description] index do selectable_column @@ -28,6 +31,7 @@ ActiveAdmin.register Author do row :created_at row :updated_at row :profile + row :posts end active_admin_comments end @@ -44,6 +48,10 @@ ActiveAdmin.register Author do f.has_many :profile, allow_destroy: true do |ff| ff.input :description end + f.has_many :posts do |fp| + fp.input :title + fp.input :description, as: :quill_editor + end f.actions end end diff --git a/spec/dummy/app/admin/posts.rb b/spec/dummy/app/admin/posts.rb index 6f55d22..9d1cb11 100644 --- a/spec/dummy/app/admin/posts.rb +++ b/spec/dummy/app/admin/posts.rb @@ -25,6 +25,14 @@ ActiveAdmin.register Post do row :tags row :created_at row :updated_at + row :images do |resurce| + resurce.images.each do |image| + div do + link_to image.filename, image, target: '_blank' + end + end + nil + end end active_admin_comments end diff --git a/spec/dummy/app/models/author.rb b/spec/dummy/app/models/author.rb index 9f26d99..9ac73cd 100644 --- a/spec/dummy/app/models/author.rb +++ b/spec/dummy/app/models/author.rb @@ -12,6 +12,7 @@ class Author < ApplicationRecord has_one_attached :avatar accepts_nested_attributes_for :profile, allow_destroy: true + accepts_nested_attributes_for :posts, allow_destroy: true validates :email, format: { with: /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\z/i, message: 'Invalid email' } diff --git a/spec/dummy/app/models/post.rb b/spec/dummy/app/models/post.rb index c285325..f80363f 100644 --- a/spec/dummy/app/models/post.rb +++ b/spec/dummy/app/models/post.rb @@ -10,6 +10,10 @@ class Post < ApplicationRecord has_many :post_tags, inverse_of: :post, dependent: :destroy has_many :tags, through: :post_tags + has_many_attached :images + + accepts_nested_attributes_for :post_tags, allow_destroy: true + validates :title, allow_blank: false, presence: true scope :published, -> { where(published: true) } diff --git a/spec/system/quill_editor_spec.rb b/spec/system/quill_editor_spec.rb index 7a338a6..6de97cb 100644 --- a/spec/system/quill_editor_spec.rb +++ b/spec/system/quill_editor_spec.rb @@ -9,7 +9,7 @@ RSpec.describe 'Quill editor', type: :system do end after do - post.destroy + Post.destroy_all author.destroy end @@ -18,6 +18,7 @@ RSpec.describe 'Quill editor', type: :system do visit "/admin/posts/#{post.id}/edit" expect(page).to have_css('#post_description_input .ql-editor', text: 'Some content...') + find('#post_description_input .ql-editor').click find('#post_description_input .ql-editor').base.send_keys('more text') find('[type="submit"]').click @@ -25,4 +26,21 @@ RSpec.describe 'Quill editor', type: :system do expect(post.reload.description).to eq '

Some content...more text

' end end + + context 'with a Quill editor in a nested resource' do + it 'updates some HTML content of a new nested resource' do + visit "/admin/authors/#{author.id}/edit" + + expect(page).to have_css('.posts.has_many_container .ql-editor', text: 'Some content...') + find('.posts.has_many_container .has_many_add').click + expect(page).to have_css('.posts.has_many_container .ql-editor', count: 2) + + fill_in('author[posts_attributes][1][title]', with: 'A new post') + find('#author_posts_attributes_1_description_input .ql-editor').base.send_keys('new post text') + find('[type="submit"]').click + + expect(page).to have_content('was successfully updated') + expect(author.posts.last.description).to eq '

new post text

' + end + end end