Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added `Spree::Contact` for contact form page

Damian Legawiec3a3713647e06
diff --git a/storefront/app/models/spree/contact.rb b/storefront/app/models/spree/contact.rb
new file mode 100644
index 0000000000..a653ec1f0a
--- /dev/null
+++ b/storefront/app/models/spree/contact.rb
@@ -0,0 +1,24 @@
+module Spree
+ class Contact < MailForm::Base
+ attribute :name, validate: true
+ attribute :email, validate: /\A([\w.%+\-]+)@([\w\-]+\.)+(\w{2,})\z/i
+ attribute :message, validate: true
+ attribute :customer_support_email, validate: :customer_support_email_bug?
+
+ def headers
+ {
+ subject: 'Contact Us',
+ to: customer_support_email.to_s,
+ from: %("#{name}" <#{email}>)
+ }
+ end
+
+ def customer_support_email_bug?
+ errors.add(:customer_support_email, 'is not there') if customer_support_email.nil? || customer_support_email.empty?
+ end
+
+ def [](key)
+ send(key)
+ end
+ end
+end
diff --git a/storefront/spec/models/spree/contact_spec.rb b/storefront/spec/models/spree/contact_spec.rb
new file mode 100644
index 0000000000..84b5f6e6ae
--- /dev/null
+++ b/storefront/spec/models/spree/contact_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe Spree::Contact, type: :model do
+ let!(:store) { Spree::Store.default }
+ subject { Spree::Contact.new(name: 'José', email: '[email protected]', message: 'Cool!', customer_support_email: '[email protected]') }
+
+ context 'contact form' do
+ it 'valid attributes' do
+ expect(subject.name).to eq('José')
+ expect(subject.valid?).to eq(true)
+ expect(subject.deliver).to eq(true)
+ end
+
+ it 'invalid customer support email' do
+ subject.customer_support_email = nil
+ expect(subject.valid?).to eq(false)
+ expect(subject.errors.full_messages).to eq(["Customer support email is not there"])
+ end
+ end
+end
\ No newline at end of file