Class: Subscription
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Subscription
- Includes:
- CommunityRelated, Timestamped
- Defined in:
- /var/apps/qpixel/app/models/subscription.rb
Constant Summary collapse
- BASE_TYPES =
['all', 'tag', 'user', 'interesting', 'category'].freeze
- MOD_ONLY_TYPES =
['moderators'].freeze
- TYPES =
(BASE_TYPES + MOD_ONLY_TYPES).freeze
- QUALIFIED_TYPES =
['category', 'tag', 'user'].freeze
Class Method Summary collapse
-
.types_accessible_to(user) ⇒ Array<String>
Gets a list of subscription types available to a given user.
Instance Method Summary collapse
-
#qualified? ⇒ Boolean
Is the subscription’s type qualified (bound to an entity)?.
-
#qualifier_entity ⇒ Category, ...
Gets entity bound to the subscription through qualifier, if any.
-
#qualifier_name ⇒ String
Gets name of the entity bound to the subscription through qualifier, if any.
- #questions ⇒ Object
Methods inherited from ApplicationRecord
#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules
Class Method Details
.types_accessible_to(user) ⇒ Array<String>
Gets a list of subscription types available to a given user
22 23 24 |
# File '/var/apps/qpixel/app/models/subscription.rb', line 22 def self.types_accessible_to(user) user.at_least_moderator? ? TYPES : BASE_TYPES end |
Instance Method Details
#qualified? ⇒ Boolean
Is the subscription’s type qualified (bound to an entity)?
52 53 54 |
# File '/var/apps/qpixel/app/models/subscription.rb', line 52 def qualified? QUALIFIED_TYPES.include?(type) end |
#qualifier_entity ⇒ Category, ...
Gets entity bound to the subscription through qualifier, if any
58 59 60 61 62 63 |
# File '/var/apps/qpixel/app/models/subscription.rb', line 58 def qualifier_entity if qualified? && qualifier.present? model = type.singularize.classify.constantize tag? ? model.find_by(name: qualifier) : model.find(qualifier) end end |
#qualifier_name ⇒ String
Gets name of the entity bound to the subscription through qualifier, if any
67 68 69 |
# File '/var/apps/qpixel/app/models/subscription.rb', line 67 def qualifier_name qualifier_entity&.name || qualifier end |
#questions ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File '/var/apps/qpixel/app/models/subscription.rb', line 26 def questions case type when 'all' Question.unscoped.on(community).where(post_type_id: Question.post_type_id) .where(Question.arel_table[:created_at].gteq(last_sent_at || created_at)) when 'tag' Question.unscoped.on(community).where(post_type_id: Question.post_type_id) .where(Question.arel_table[:created_at].gteq(last_sent_at || created_at)) .joins(:tags).where(tags: { name: qualifier }) when 'user' Question.unscoped.on(community).where(post_type_id: Question.post_type_id) .where(Question.arel_table[:created_at].gteq(last_sent_at || created_at)) .where(user_id: qualifier) when 'interesting' Question.unscoped.on(community).where(post_type_id: Question.post_type_id) .where('score >= ?', interesting_threshold) .order(Arel.sql('RAND()')) when 'category' Question.unscoped.on(community).where(post_type_id: Question.post_type_id) .where(Question.arel_table[:created_at].gteq(last_sent_at || created_at)) .where(category_id: qualifier) end&.order(created_at: :desc)&.limit(25) end |