docbrown/app/services/user_subscriptions/is_subscribed_cache_checker.rb
Alex 9ee024f9d0
UserSubscription backend refactor (#9060)
* Refactor controller validations to model

* Update specs and move current email validation back

* Refactor #subscribed

* Cleanup UserSubscriptions::Create service

* Refactor service

* Move source inside of call for clarity

* Specs for UserSubscription::Create

* Cleanup spec

* Fix Article spec

* Updated for subscriber_email

* Add comment for explicitness of subscriber_email

* Fix reference in comment...oops!

* I know what I'm doing...sometimes...maybe

* Rename CachChecker - Subscription to IsSubscribed

* Use include vs eq in specs for error messages

* attr_accessor --> attr_reader

* Don't explicitly set nil

* Rename Create service to CreateFromService

* Use errors_as_sentence

* Rename service to CreateFromControllerParams

* Assign Struct to a new constant
2020-07-02 12:08:37 -04:00

28 lines
849 B
Ruby

module UserSubscriptions
# This checks if the provided user is subscribed to the provided source
# (returns boolean).
class IsSubscribedCacheChecker
attr_accessor :user, :source_type, :source_id
def self.call(*args)
new(*args).call
end
def initialize(user, params)
@user = user
@source_type = params[:source_type]
@source_id = params[:source_id]
end
def call
cache_key = "user-#{user.id}-#{user.updated_at.rfc3339}-#{user.subscribed_to_user_subscriptions_count}/is_subscribed_#{source_type}_#{source_id}"
Rails.cache.fetch(cache_key, expires_in: 24.hours) do
UserSubscription.where(
subscriber_id: user.id,
user_subscription_sourceable_type: source_type,
user_subscription_sourceable_id: source_id,
).any?
end
end
end
end