Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open the ContactFormController to model subclasses #4374

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/controllers/hyrax/contact_form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class ContactFormController < ApplicationController
before_action :build_contact_form
layout 'homepage'

class_attribute :model_class
self.model_class = Hyrax::ContactForm

def new; end

def create
Expand All @@ -12,7 +15,7 @@ def create
ContactMailer.contact(@contact_form).deliver_now
flash.now[:notice] = 'Thank you for your message!'
after_deliver
@contact_form = ContactForm.new
@contact_form = model_class.new
else
flash.now[:error] = 'Sorry, this message was not sent successfully. ' +
@contact_form.errors.full_messages.map(&:to_s).join(", ")
Expand All @@ -36,7 +39,7 @@ def after_deliver; end
private

def build_contact_form
@contact_form = Hyrax::ContactForm.new(contact_form_params)
@contact_form = model_class.new(contact_form_params)
end

def contact_form_params
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/hyrax/contact_form_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@

before { sign_in(user) }

describe '.model_class' do
let(:fake_contact_form_class) { Class.new(Hyrax::ContactForm) }

it 'is Hyrax::ContactForm by default' do
expect(described_class.model_class).to eq Hyrax::ContactForm
end

it 'builds an instance of the class for assigns' do
allow(described_class).to receive(:model_class).and_return(fake_contact_form_class)

get :new

expect(assigns[:contact_form]).to be_a fake_contact_form_class
end
end

describe "#new" do
subject { response }

Expand Down