From 4c44d2509d7cd01e477b5faf2a57ff2780e646f8 Mon Sep 17 00:00:00 2001 From: Justin Powell Date: Thu, 5 Mar 2020 16:33:17 -0600 Subject: [PATCH] allow contact form class to be overridden --- .../contentful/app/contact_form_controller.rb | 6 ++++- .../app/contact_form_controller_spec.rb | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/wcc-contentful-app/app/controllers/wcc/contentful/app/contact_form_controller.rb b/wcc-contentful-app/app/controllers/wcc/contentful/app/contact_form_controller.rb index 6a1e63a7..bf334424 100644 --- a/wcc-contentful-app/app/controllers/wcc/contentful/app/contact_form_controller.rb +++ b/wcc-contentful-app/app/controllers/wcc/contentful/app/contact_form_controller.rb @@ -24,11 +24,15 @@ def create def form_model raise ArgumentError, 'missing form ID' unless params[:id] - @form_model ||= WCC::Contentful::Model::SectionContactForm.find( + @form_model ||= form_class.find( params[:id], options: { preview: preview? } ) end + def form_class + WCC::Contentful::Model.resolve_constant('section-contact-form') + end + def form_params params.slice(*form_model.fields.map(&:title)) end diff --git a/wcc-contentful-app/spec/requests/wcc/contentful/app/contact_form_controller_spec.rb b/wcc-contentful-app/spec/requests/wcc/contentful/app/contact_form_controller_spec.rb index 12f79ed1..456c7379 100644 --- a/wcc-contentful-app/spec/requests/wcc/contentful/app/contact_form_controller_spec.rb +++ b/wcc-contentful-app/spec/requests/wcc/contentful/app/contact_form_controller_spec.rb @@ -2,6 +2,9 @@ require 'rails_helper' +class MyContactForm < WCC::Contentful::Model::SectionContactForm +end + RSpec.describe WCC::Contentful::App::ContactFormController, type: :request do let(:mailer) { double(deliver: nil) @@ -44,6 +47,27 @@ } end + context 'section model is extended' do + before do + MyContactForm.register_for_content_type('section-contact-form') + end + + after do + WCC::Contentful::Model.class_variable_get('@@registry').clear + end + + it 'resolves to the descendant contact form override' do + form = contentful_stub('section-contact-form') + + expect(::MyContactForm).to receive(:find) + .with(form.id, { options: { preview: false } }) + .and_return(form) + expect(form).to receive(:send_email) + + post '/contact_form', params: { id: form.id } + end + end + context 'preview: true' do before do expect(WCC::Contentful::Model::SectionContactForm)