User
Concerns to include in the host app User model, associating it to a Sender and/or a Subscriber. See the guide for usage.
| Concern | Includes |
|---|---|
Missive::User | both concerns below |
Missive::UserAsSender | sender associations and init_sender |
Missive::UserAsSubscriber | subscriber associations and init_subscriber |
The model must have an email attribute.
Missive::UserAsSender
Associations
| Key | Default name | Definition |
|---|---|---|
sender | missive_sender | has_one Sender, dependent: :nullify |
sent_dispatches | missive_sent_dispatches | has_many Dispatch through the sender |
sent_lists | missive_sent_lists | has_many List through the sender |
sent_messages | missive_sent_messages | has_many Message through the sender |
init_sender
user.init_sender(attributes = {}) # => Missive::SenderFinds 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
| Key | Default name | Definition |
|---|---|---|
subscriber | missive_subscriber | has_one Subscriber, dependent: :destroy |
dispatches | missive_dispatches | has_many Dispatch through the subscriber |
subscriptions | missive_subscriptions | has_many Subscription through the subscriber |
subscribed_lists | missive_subscribed_lists | has_many List through the subscriptions, including suppressed ones |
unsubscribed_lists | missive_unsubscribed_lists | has_many List through suppressed subscriptions |
init_subscriber
user.init_subscriber(attributes = {}) # => Missive::SubscriberFinds 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.
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.