Skip to content

Instantly share code, notes, and snippets.

@caged
Last active January 8, 2025 15:28
Show Gist options
  • Save caged/46514305de8514f3ca4c38107434c6d1 to your computer and use it in GitHub Desktop.
Save caged/46514305de8514f3ca4c38107434c6d1 to your computer and use it in GitHub Desktop.
Getting started with spina pro message inbox.

Spina Pro Message Inbox Setup

As of 01/07/2025, the Spina Pro Messages guide is unpublished, making it a little difficult getting up and running with message inboxes. Below is information I've peiced together while trying to get it running. Hopefully it can help the next person.

Prerequisites

  • Spina pro installed using this guide.
  • You have config.plugins = [ "Pro" ] set in your config/spina.rb config.

Generate your Message object

bin/rails g spina:pro:message SampleMessage email:string first_name:string last_name:string age:integer something:string

Message objects are a container for the user input you'd like to store. They are ActiveRecord objects with a few special properties and various methods for managing their states like read, unread, archived, etc.

Some attributes are "special" in that they affect how messages are displayed in the inbox. Those appear to be first_name, last_name, name and email.

You can also specify different inboxes for different message types by setting inbox on the class.

# Example message object
class SampleMessage < Spina::Pro::Message
  inbox :my_inbox, "My Inbox"

  field :email, :string, index: true
  field :first_name, :string
  field :last_name, :string
  field :age, :integer
  field :something, :text
end

Create your own controller to submit messages

The code below curtousey of Bram. The spam detection is a custom 3rd party service and the captcha is a 3rd party gem. They aren't required, but probably a good idea to consider them.

The way that I did this was to setup a contact view_template and custom_page in my theme config file and use that page to embed the message form. This allows me to add additional dynamic content on the page.

<div>
  <%= turbo_frame_tag :customer_message_form do %>
    <%= render "spina/sample_messages/form", message: SampleMessage.new %>
  <% end %>
</div>
module Spina
  class SampleMessagesController < ApplicationController
    # This is from ttps://github.com/markets/invisible_captcha
    invisible_captcha only: [:create], on_spam: :render_success_message, on_timestamp_spam: :render_success_message
    
    def create
      @message = SampleMessage.new(message_params)
      @message.spam_score = get_spam_score # Spam or ham!
      
      if @message.save
        render_success_message
      else
        render turbo_stream: turbo_stream.update("message_form", partial: "form")
      end
    end
    
    private
    
      def render_success_message
        render turbo_stream: turbo_stream.update("message_form", partial: "success")
      end
      
      def message_params
        params.require(:message).permit(:first_name, :last_name, :email, :phone, :body, :subject)
      end
      
      # Use a rescue-block so that it's not marked as spam if there's an exception
      # (like when we are over the API usage limit)
      def get_spam_score
        OOPSpam.detect_spam({
          content: @message.body,
          email: @message.email,
          senderIP: request.remote_ip,
          urlFriendly: false,
          logIt: true,
          blockedCountries: ["ru", "cn"]
        })&.dig("Score").to_i
      rescue
        0
      end
    
  end
end

And in your routes, add a route to this area below where you mount the Spina::Engine.

Spina::Engine.routes.draw do
  post "sample_messages/create", as: :sample_messages
end

And your views in spina/sample_messages

_form.html.erb

<%= form_with model: message, url: sample_messages_path, method: :post do |f| %>
  <%= f.label :first_name %>
  <%= f.text_field :first_name %>

  <div>
    <%= f.submit %>
  </div>
<% end %>

_success.html.erb

Thanks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment