Sha256: 360a0f6e21f5e7dc494ed1a132d6a46a0ed66f76436fc709bf72cb28c06145bb

Contents?: true

Size: 1.13 KB

Versions: 32

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

class WCC::Contentful::Model::SectionContactForm < WCC::Contentful::Model
  def send_email(data)
    save_contact_form(data)

    ::WCC::Contentful::App::ContactMailer.contact_form_email(data[:notification_email], data).deliver
  end

  def page
    ::WCC::Contentful::Model::Page.find_by(sections: { id: id })
  end

  def to_address(email_object_id: nil)
    return email_address(email_model(email_object_id)) if email_object_id.present?

    notification_email
  end

  private

  def email_address(entry)
    return entry.email if defined?(entry.email)

    raise ArgumentError, 'email is not defined on this entry'
  end

  def email_model(email_object_id)
    raise ArgumentError, 'contentful entry does not exist' unless
      entry = ::WCC::Contentful::Model.find(email_object_id, options: sys.context.to_h)

    entry
  end

  def save_contact_form(data)
    return unless ::WCC::Contentful::App.db_connected?
    return unless ::ActiveRecord::Base.connection
      .table_exists? 'wcc_contentful_app_contact_form_submissions'

    ::WCC::Contentful::App::ContactFormSubmission.create!(form_id: id, data: data)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
wcc-contentful-app-1.0.8 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.7 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.6 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.5 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.4 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.3 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.2 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.1 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.0 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.0.pre.rc3 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.0.pre.rc2 lib/wcc/contentful/model/section_contact_form.rb
wcc-contentful-app-1.0.0.pre.rc1 lib/wcc/contentful/model/section_contact_form.rb