* Add migrations for subscriber_email column * Add new schema * Update relationships, specs, and factory * Use unless index_exists? over if !index_exists? * Fix typo in last fix for migration * Rename authored_user_subscriptions - to source_authored_user_subcriptions * Add factory methods and specs - UserSubscription.make - UserSubscription.make_new - Article.new_user_subscription - Article.create_user_subscription * Remove self and & * Change make_new to build * new_user_subscription --> build_user_subscription * Come on, Travis
33 lines
880 B
Ruby
33 lines
880 B
Ruby
module UserSubscriptionSourceable
|
|
extend ActiveSupport::Concern
|
|
|
|
# This all assumes there's an association with User under the column user_id.
|
|
|
|
included do
|
|
has_many :user_subscriptions, as: :user_subscription_sourceable
|
|
has_many :sourced_subscribers,
|
|
class_name: "User",
|
|
through: :user_subscriptions,
|
|
source: :subscriber,
|
|
foreign_key: :user_id
|
|
end
|
|
|
|
def build_user_subscription(subscriber)
|
|
UserSubscription.new(user_subscription_attributes(subscriber))
|
|
end
|
|
|
|
def create_user_subscription(subscriber)
|
|
UserSubscription.create(user_subscription_attributes(subscriber))
|
|
end
|
|
|
|
private
|
|
|
|
def user_subscription_attributes(subscriber)
|
|
{
|
|
user_subscription_sourceable: self,
|
|
author_id: user_id,
|
|
subscriber_id: subscriber&.id,
|
|
subscriber_email: subscriber&.email
|
|
}
|
|
end
|
|
end
|