lib/activity_notification/models/concerns/subscriber.rb in activity_notification-2.0.0 vs lib/activity_notification/models/concerns/subscriber.rb in activity_notification-2.1.0
- old
+ new
@@ -44,12 +44,23 @@
end
# Creates new subscription of the target.
#
# @param [Hash] subscription_params Parameters to create subscription record
+ # @raise [ActivityNotification::RecordInvalidError] Failed to save subscription due to model validation
# @return [Subscription] Created subscription instance
def create_subscription(subscription_params = {})
+ subscription = build_subscription(subscription_params)
+ raise RecordInvalidError, subscription.errors.full_messages.first unless subscription.save
+ subscription
+ end
+
+ # Builds new subscription of the target.
+ #
+ # @param [Hash] subscription_params Parameters to build subscription record
+ # @return [Subscription] Built subscription instance
+ def build_subscription(subscription_params = {})
created_at = Time.current
if subscription_params[:subscribing] == false && subscription_params[:subscribing_to_email].nil?
subscription_params[:subscribing_to_email] = subscription_params[:subscribing]
end
subscription = Subscription.new(subscription_params)
@@ -72,10 +83,10 @@
Subscription.to_optional_target_key(optional_target_name) => false,
Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(created_at)
)
end
subscription.assign_attributes(optional_targets: optional_targets)
- subscription.save ? subscription : nil
+ subscription
end
# Gets configured subscription index of the target.
#
# @example Get configured subscription index of the @user
\ No newline at end of file