Skip to content

User ​

Concerns to include in the host app User model, associating it to a Sender and/or a Subscriber. See the guide for usage.

ConcernIncludes
Missive::Userboth concerns below
Missive::UserAsSendersender associations and init_sender
Missive::UserAsSubscribersubscriber associations and init_subscriber

The model must have an email attribute.

Missive::UserAsSender ​

Associations ​

KeyDefault nameDefinition
sendermissive_senderhas_one Sender, dependent: :nullify
sent_dispatchesmissive_sent_dispatcheshas_many Dispatch through the sender
sent_listsmissive_sent_listshas_many List through the sender
sent_messagesmissive_sent_messageshas_many Message through the sender

init_sender ​

rb
user.init_sender(attributes = {}) # => Missive::Sender

Finds the sender with the user's email, or initializes a new one, associates it to the user, assigns attributes (e.g. name:) and saves it.

Missive::UserAsSubscriber ​

Associations ​

KeyDefault nameDefinition
subscribermissive_subscriberhas_one Subscriber, dependent: :destroy
dispatchesmissive_dispatcheshas_many Dispatch through the subscriber
subscriptionsmissive_subscriptionshas_many Subscription through the subscriber
subscribed_listsmissive_subscribed_listshas_many List through the subscriptions, including suppressed ones
unsubscribed_listsmissive_unsubscribed_listshas_many List through suppressed subscriptions

init_subscriber ​

rb
user.init_subscriber(attributes = {}) # => Missive::Subscriber

Finds the subscriber with the user's email, or initializes a new one, associates it to the user, assigns attributes and saves it.

Renaming associations: .with ​

Each concern accepts a hash mapping the keys above to custom association names. Unconfigured keys keep their default name.

rb
include Missive::UserAsSender.with(sender: :sender, sent_lists: :lists)
include Missive::UserAsSubscriber.with(subscriber: :subscriber)

# or both at once
include Missive::User.with(
  sender: {sender: :sender},
  subscriber: {subscriber: :subscriber}
)

init_sender and init_subscriber use the configured names. The configuration is exposed as User.missive_sender_config and User.missive_subscriber_config.

Errors ​

Missive::UserAsSender::AssociationAlreadyDefinedError / Missive::UserAsSubscriber::AssociationAlreadyDefinedError is raised when the model already defines an association with the name chosen for sender / subscriber.

Released under the MIT License.