Class: Subscription

Inherits:
ApplicationRecord show all
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

Instance Method Summary collapse

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

Parameters:

  • user (User)

    user to check type access for

Returns:

  • (Array<String>)

    list of available types



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)?

Returns:

  • (Boolean)

    check result



52
53
54
# File '/var/apps/qpixel/app/models/subscription.rb', line 52

def qualified?
  QUALIFIED_TYPES.include?(type)
end

#qualifier_entityCategory, ...

Gets entity bound to the subscription through qualifier, if any

Returns:



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_nameString

Gets name of the entity bound to the subscription through qualifier, if any

Returns:

  • (String)


67
68
69
# File '/var/apps/qpixel/app/models/subscription.rb', line 67

def qualifier_name
  qualifier_entity&.name || qualifier
end

#questionsObject



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